php - mysqli_fetch_array(), prepared statement, and LIKE statement -
i'm trying use mysqli prepared statements statement query , wildcard operators. after debugging sprinkling echo statements throughout code, can see while statement not executing. can see i'm doing wrong here?
this first time asking on forum, apologize if isn't question; i've spent 6 hours trying prepared statement section of code work , can't find threads addressing question don't go on head (e.g. how can put results of mysqli prepared statement associative array?). 2 closest found were:
using wildcards in prepared statement - mysqli , combine php prepared statments like.
here's relevant excerpt of code:
//set , execute queries $titlequery = "select keyframeurl, videoid, title, creationyear, sound, color, duration, genre openvideo title concat ('%', ?, '%') order $order"; if($stmt = mysqli_prepare($db, $titlequery)){ //bind parameters mysqli_stmt_bind_param($stmt, 's', $trimmedtitlesearch); //execute query mysqli_stmt_execute($stmt); //bind results mysqli_stmt_bind_result($stmt, $keyframeurl, $videoid, $title, $year, $sound, $color, $duration, $genre); //store result num rows can counted $result = mysqli_stmt_store_result($stmt); //fetch results while ($row = mysqli_fetch_array($result, mysql_assoc)) { echo "<tr>"; echo "<td><a href=\"".$row['keyframeurl']."\">".$row['videoid']."</a></td>"; echo "<td>" . $row['title'] . "</td>"; echo "<td>" . $row['year'] . "</td>"; echo "<td>" . $row['sound'] . "</td>"; echo "<td>" . $row['color'] . "</td>"; echo "<td>" . $row['duration'] . "</td>"; echo "<td>" . $row['genre'] . "</td>"; echo "</tr>"; } } else { // error printf("prepared statement error: %s\n", $db->error); }
thanks advice!
you mixing 2 styles of fetching results. either use ugly bind_result way (and data using fetch()
then), or try use get_result()
- so, you'll able use fetch_array()
(not guaranteed though).
anyway, rid of mess , use pdo.
$titlequery = "select keyframeurl, videoid, title, creationyear, sound, color, duration, genre openvideo title concat ('%', ?, '%') order $order"; $stmt = $pdo->prepare($titlequery); $stmt->execute(array($trimmedtitlesearch)); $data = $stmt->fetchall(); foreach ($data $row ) { // rest same yours
i hope properly sanitized $order variable. best way apparently add via placeholder, so, need library allows it, safemysql example:
$sql = "select * openvideo title concat ?s order ?n"; $data = $db->getall($sql,"%$trimmedtitlesearch%", $order); foreach ($data $row ) { // rest same yours
note amount of code , compare load of raw api calls using @ moment
Comments
Post a Comment