Page 1 of 1

Whats wrong with this code

Posted: Tue Jan 27, 2009 1:16 pm
by indianyogi
pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


I have following code

Code: Select all

/* check connection */
                 if (mysqli_connect_errno()) {
                     printf("Connect failed: %s\n", mysqli_connect_error());
                         exit();
                 }
                 
                 /* Prepare an insert statement */
                 $stmt = $mysqli->prepare("INSERT INTO table1 (userEmail, userData, userIP, userCode, userDate) VALUES (?,?,INET_ATON($_SERVER [ 'REMOTE_ADDR' ]),?,now())");
 
                 $stmt->bind_param('sds', $userEmail, $userData, $userCode); //LINE : 15
                 $userEmail    = (string) $_userEmail;
                 $userData   = (double) $_userDate;
                 $userCode = 'aspdotnet';

an error is thrown : Call to a member function bind_param() on a non-object in yourFile.php on line 15.

What is it, which is causing error ?

pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.

Re: Whats wrong with this code

Posted: Tue Jan 27, 2009 4:23 pm
by Shiki
I don't see it in your code but I'm going to guess $mysqli already exists since you're using it and its prepare() function. The error may have been because $stmt is FALSE and is not an object. Most probably because there's an error in your sql statement. You could check if this is the cause by putting this before the "$stmt->bind_param .. " line:

Code: Select all

var_dump($stmt);

Re: Whats wrong with this code

Posted: Tue Jan 27, 2009 5:08 pm
by requinix
Shiki wrote:Most probably because there's an error in your sql statement

Code: Select all

INET_ATON($_SERVER [ 'REMOTE_ADDR' ])
turns into

Code: Select all

INET_ATON(Array [ 'REMOTE_ADDR' ])
because you don't know how to embed variables into strings properly.

Code: Select all

INET_ATON('$_SERVER[REMOTE_ADDR]')
No spaces, can't put the 's inside the array key, and you need 's outside the variable so MySQL knows it's a string and not an invalid number.