0

I have a simple MySQL query designed to get a single value from a MySQL database:

$cr = 'copyright';
$stmt = $con->prepare("SELECT ${_COOKIE['lang']} FROM lang WHERE page = ?");
$stmt->bind_param("s", $cr);
$stmt->execute();
$stmt->bind_result($result);
$stmt->fetch();
$copyright = $result;

I have another simple MySQL query to get a different single value from the same table in the same database:

$cb = 'contactblurb';
$stmt = $con->prepare("SELECT ${_COOKIE['lang']} FROM lang WHERE page = ?");
$stmt->bind_param("s", $cb);
$stmt->execute();
$stmt->bind_result($result);
$stmt->fetch();
$blurb = $result;

They both work independently of each other and retrieve the required data. However, when placed one after another, like so...

$cr = 'copyright';
$stmt = $con->prepare("SELECT ${_COOKIE['lang']} FROM lang WHERE page = ?");
$stmt->bind_param("s", $cr);
$stmt->execute();
$stmt->bind_result($result);
$stmt->fetch();
$copyright = $result;

$cb = 'contactblurb';
$stmt = $con->prepare("SELECT ${_COOKIE['lang']} FROM lang WHERE page = ?");
$stmt->bind_param("s", $cb);
$stmt->execute();
$stmt->bind_result($result);
$stmt->fetch();
$blurb = $result;

I get the following error for the third line of the second query.

Uncaught Error: Call to a member function bind_param() on bool...

What am I missing? I'm not closing any connections between the two queries? Is there something I'm missing?

The statements have to be called separately as one is calling data that will be used site-wide and the other is for a specific page only.

Dharman
  • 30,962
  • 25
  • 85
  • 135

2 Answers2

0

Uncaught Error: Call to a member function bind_param() on bool...

Sounds like $con->prepare returns false.

https://www.php.net/manual/en/pdo.prepare.php

Make sure $con->prepare returns a PDOStatement object and it will work.

Blackbam
  • 17,496
  • 26
  • 97
  • 150
  • Please note: "They both work independently of each other and retrieve the required data." If either are written independently of the other, the required result is retrieved. –  Apr 02 '20 at 09:58
  • My answer is correct anyway. Why does $con->prepare return false? Are you calling them instantly one after another? Is ${_COOKIE['lang']} available and is this correct syntax? – Blackbam Apr 02 '20 at 10:44
  • I understand that the error I get is an indication that $con->prepare returns false. My query is relating to the fact that when these queries are run instantly one after another (as you ask), I get this error. I'm relatively new to this and am curious if there is code I am missing between the two queries. ${_COOKIE['lang']} is correct and works when the queries stand alone :) –  Apr 02 '20 at 11:14
  • Well I guess it is because the query is already prepared? Have you tried to delete the second `$stmt = $con->prepare("SELECT ${_COOKIE['lang']} FROM lang WHERE page = ?");`? It is the same statement, just with different parameters. – Blackbam Apr 02 '20 at 12:20
  • OK, I'm understanding what you're saying. Apologies for being a little slow, there. I wasn't aware a statement could only be prepared once in a page. I'm marking your answer as correct, but will post my solution in an answer also, in case it helps others later on. Thanks, @Blackbam ! –  Apr 02 '20 at 13:28
  • @James Yes. I would recommend you to read more on prepared statements here: https://www.php.net/manual/en/pdo.prepared-statements.php – Blackbam Apr 02 '20 at 13:53
  • How it's even possible this answer is accepted? – Your Common Sense Apr 03 '20 at 05:48
0

I was trying to prepare the same statement twice in one page. All I had to do was remove the second prepare statement, making the code as follows:

// First statement
$cr = 'copyright';
$stmt = $con->prepare("SELECT ${_COOKIE['lang']} FROM lang WHERE page = ?");
$stmt->bind_param("s", $cr);
$stmt->execute();
$stmt->bind_result($result);
$stmt->fetch();
$copyright = $result;

// Second statement
$cb = 'contactblurb';
$stmt->bind_param("s", $cb);
$stmt->execute();
$stmt->bind_result($result);
$stmt->fetch();
$blurb = $result;

Cheers to @Blackbam for the hint!