Search code examples
phpmysqliprepared-statement

Issue binding parameters in mysqli statement


The following code throws no error neither it fetches the data. Note: $url variable contains a string.

$stmt2 = $conn->stmt_init();
$stmt2->prepare("SELECT 'image', 'name' FROM events WHERE 'eventurl'='?'");
$stmt2->bind_param("s",$url)
$stmt2->execute();
$stmt2->bind_result($image, $name);

When I write the following code, I get the output as "Binding error : 0"

if(!$stmt2->bind_param("s",$url)){echo "Binding error : ".mysqli_errno($conn)."<br>";}

I have realized that there is some issue binding the data, because, when I directly mention the value in the query the data is fetched without any issues.

$stmt2->prepare("SELECT 'image', 'name' FROM events WHERE 'eventurl'='latest-event-url'");

Can someone please help me identify what could be the issue here with binding data?


Solution

  • I suspect your double and single quotes are causing a problem. Can you try the following:

    $url = "www.google.com" //added this just as a test
    
    $stmt2 = $conn->stmt_init();
    $stmt2->prepare('SELECT image, name FROM events WHERE eventurl = ?');
    $stmt2->bind_param('s',$url)
    $stmt2->execute();
    $stmt2->bind_result($image, $name);