Whats wrong with this code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
indianyogi
Forum Newbie
Posts: 7
Joined: Tue Jan 20, 2009 9:16 am

Whats wrong with this code

Post 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.
User avatar
Shiki
Forum Newbie
Posts: 9
Joined: Sat Nov 29, 2008 4:35 am

Re: Whats wrong with this code

Post 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);
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Whats wrong with this code

Post 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.
Post Reply