php MySqli : How can i rewrite fetch to fetch_assoc? LIKE CONCAT

家住魔仙堡 提交于 2019-12-30 11:11:16

问题


Helo, I using "bind_result", "LIKE CONCAT" ... to reach full text search and pagination by two query string.

but how can I change bind_result methods to fetch_assoc?

<?php
$mysqli = new mysqli("localhost", "", "", "test");


$query_string="hello"; //keywords

$sqltxt="SELECT * FROM `table` WHERE text1 LIKE CONCAT('%', ?, '%')";


//first query : for get the total number of data
$stmt = $mysqli->prepare($sqltxt);
$stmt->bind_param("s",$query_string);
$stmt->execute();
$stmt->store_result();
$total =$stmt->num_rows;

$lastpage = ceil($total/20);//obtain the last page

$page=$_GET["page"];
$startpoint = ($page * 20) - 20;

//second query : for get the true data
$stmt = $mysqli->prepare($sqltxt ." LIMIT {$startpoint}, 20");
$stmt->bind_param("s",$query_string);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($id,$title,$tel); //<= hits!! I want to using fetch_assoc methods

while($stmt->fetch()){ //<= hits!! I want to using fetch_assoc methods


echo $id;
echo $atitle;
echo $tel;

}


?>

Thanks!


回答1:


From mysql documentation examples:

<?php

// ... document's example code:

/* bind parameters for markers */
$stmt->bind_param("s", $city);

/* execute query */
$stmt->execute();

/* instead of bind_result: */
$result = $stmt->get_result(); //with get_result ;)

/* now you can fetch the results into an array - NICE */
while ($myrow = $result->fetch_assoc()) { //now you can use fetch_assoc

    // use your $myrow array as you would with any other fetch
    printf("%s is in district %s\n", $city, $myrow['district']);

}
?>

-----------EDITED----------------------

Please read the user notes for this method:

http://php.net/manual/en/mysqli-stmt.get-result.php

It requires the mysqlnd driver... if it isn't installed on your webspace you will have to work with BIND_RESULT & FETCH!

http://www.php.net/manual/en/mysqli-stmt.bind-result.php

http://www.php.net/manual/en/mysqli-stmt.fetch.php

Function MySQL Native Driver Only

Available only with mysqlnd: http://www.php.net/manual/en/book.mysqlnd.php

Note: mysqli_stmt::get_result() is only available at PHP v5.3.0 or above

I'm gonna search for another alternative.

Saludos ;)



来源:https://stackoverflow.com/questions/15659326/php-mysqli-how-can-i-rewrite-fetch-to-fetch-assoc-like-concat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!