0

I've got this code:

$conn = new mysqli($servername, $username, $password, $dbname);
$jsonvprencode = json_encode($jsonvpr);
$stmt = $conn->prepare("UPDATE `wp_posts` SET  `post_content` = :val WHERE `ID` = $idvpr");
$stmt->bind_param(":val", $jsonvprencode);
$stmt->execute();

When trying to execute it it returns Fatal error: Call to a member function bind_param() on a non-object in /var/www/html/crawlnew.php on line 432

I can't understand what is going on here -_-

6
  • I'm guessing the prepare failed Commented Apr 21, 2015 at 11:40
  • You are using mysqli and you used named placeholder. Can't do. Commented Apr 21, 2015 at 11:47
  • I supose, but I don't understand why. Replacing the parameter with a string, and ID with a number, the query works right. Variables $jsonvprencode and $idvpr are valid strings as well :S Commented Apr 21, 2015 at 11:50
  • you are mixing mysqli and PDO. Commented Apr 21, 2015 at 11:51
  • If I remember correctly mysqli only support ? as placeholder. If you want to use :val as a placeholder. It is pretty much like @niyou told you. Commented Apr 21, 2015 at 11:54

3 Answers 3

1

MySQLi with PreparedStatements

$stmt = $conn->prepare("UPDATE `wp_posts` SET  `post_content` = ? WHERE `ID` = ?");
$stmt->bind_param("si", $jsonvprencode,$idvpr);

PDO with Prepared Statements

$stmt = $conn->prepare("UPDATE `wp_posts` SET  `post_content` = :jsonvprencode WHERE `ID` = :idvpr");

$stmt->bindParam(':jsonvprencode', $jsonvprencode);

$stmt->bindParam(':idvpr', $idvpr);
Sign up to request clarification or add additional context in comments.

3 Comments

This give me another error -_- Warning: mysqli_stmt::bind_param(): Number of elements in type definition string doesn't match number of bind variables in /var/www/html/crawlnew.php on line 435
imo this is maybe correct for mysqli...but he is asking about PDO.
@CornezuelodelCenteno see edits now ... there are 2 ways to bind parameters ... with MySQLi/PS and PDO/PS
0

You have to open a PDO connection

$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);

and then use this object

5 Comments

instead of my $conn one?
your solution gives me Fatal error: Call to undefined method PDOStatement::bind_param() in /var/www/html/crawlnew.php on line 432
I was using your method before, and the error was diferent, but still were errors, check my answer!
exactly - the method is bindParam
btw - you should aesily read the documentation at php.net: php.net/manual/en/book.pdo.php
0

I think you have an error in your statement.

printf("Errormessage: %s\n", $conn->error);

try to echo the error if you have one. If you have an error in your statement you didn't get an proper object.

1 Comment

Using printf("Errormessage: %s\n", $conn->error); (I was suposed to change $mysqli for my connection variable, right?) don't returns any sql error :S

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.