mysql - php, using a SELECT statement in prepare, how do I get the fetch_assoc array? -
i have been playing around prepare , bind_param in php. trying work select statement , display results. when try statement here 0 results search. if use query, works fine , results displayed. not possible using prepare? or logic wrong using $result = $stmt->execute();
result?
<?php $dbhost = 'localhost'; //default $dbuser = 'root'; $dbpass = 'somepassword'; //create 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 database call method out conn object $conn->select_db("tutorial"); $stmt=$conn->prepare("select tutorial_author tutorial_info tutorial_title=?;"); $stmt->bind_param("s",$tutorial_title); $tutorial_title="math"; $result = $stmt->execute(); //we false if 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 database $conn->close(); ?>
$stmt->execute()
returns boolean indicating success. in code, try read $result->num_rows
, other properties of $result, while $result should true
@ point (not object).
use $stmt->fetch()
fetch records 1 one in bind result parameters, or use $stmt->get_result()
mysqli_result object has more functionality fetch data @ once or 1 row @ time.
Comments
Post a Comment