PHP编写的语句-传递给bindu result()的变量数未定义
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP编写的语句-传递给bindu result()的变量数未定义相关的知识,希望对你有一定的参考价值。
When retrieving table rows with php and prepared statements, you must use a method called bind_result() and pass in a variable name for each column that is being returned from the database. For example, if you are selecting three columns (say, id, title, body), bind_result must be passed three variables names: bind_result($id, $title, $body). This becomes a pain in the butt when, for example, if it is within a class file, where you will not always know how many columns are being selected from the table. This code gets around that.
class Database { function __construct($host, $username, $password, $db) { } /** * Simple query method. Pass in your SQL query as a parameter, and it'll do the rest * and return an array of the returned rows * * @param string $query Should be equal to a SQL query for querying the database. * @return array Returns an array of the returned rows from the db. */ public function query($query) { $stmt = $this->mysql->prepare($query); $stmt->execute(); $meta = $stmt->result_metadata(); while( $field = $meta->fetch_field() ) { $parameters[] = &$row[$field->name]; } while($stmt->fetch()) { foreach($row as $key => $val ) { // This next line isn't necessary for your project. // It can be removed. I use it to ensure // that the "excerpt" of the post doesn't end in the middle // of a word. if ( $key === 'excerpt') $val = $this->cleanExcerpt($row[$key]); $x[$key] = $val; } $results[] = $x; } return $results; } } $db = new Database('host', 'username' 'password', 'databaseName'); $items = $db->query("Your SQL query here");
以上是关于PHP编写的语句-传递给bindu result()的变量数未定义的主要内容,如果未能解决你的问题,请参考以下文章
mysqli_stmt::bind_result():绑定变量的数量与准备好的语句中的字段数量不匹配
使用 PHP json_encode() 和 MySQL 返回一个 JSON 对象以传递给 jQuery 函数 [重复]