I have been playing around with PREPARE and bind_param in php. I am trying to get it to work for a SELECT statement and display the results. When I try the statement here I get 0 results for the search. If I use query, it works fine and the results are displayed. Is it not possible to do this using prepare? Or is my logic wrong using $result = $stmt->execute();
to get the result?
<?php
$dbhost = 'localhost'; //default
$dbuser = 'root';
$dbpass = 'somepassword';
//Create a connection object
$conn = new mysqli($dbhost, $dbuser, $dbpass);
if($conn->connect_error )
{
die('Could not connect: %s' . $conn->connect_error);
}
echo "Connected successfully<br>";
//Select the database call the method from out conn object
$conn->select_db("TUTORIAL");
$stmt=$conn->prepare("SELECT tutorial_author FROM tutorial_info WHERE tutorial_title=?;");
$stmt->bind_param("s",$tutorial_title);
$tutorial_title="Math";
$result = $stmt->execute();
//We get a false if it fails
if($result===FALSE) {
echo "Select failed <br>";
}
else {
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "Tutorial Title: " . $row["tutorial_title"]. " - Name: " . $row["tutorial_author"]. "<br>";
}
} else {
echo "0 results";
}
}
//Close the database
$conn->close();
?>