PHP PHP准备语句 - 带有未定义的变量数传递给bind_result()

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP PHP准备语句 - 带有未定义的变量数传递给bind_result()相关的知识,希望对你有一定的参考价值。

class Database {

	private $mysql;
	
	function __construct($host, $username, $password, $db) {
		$this->mysql = new mysqli($host, $username, $password, $db) or die('There was a problem connecting to the database');
	}
	
        /**
         * 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) {
            
                $parameters = array();
                $results = array();
	
		$stmt = $this->mysql->prepare($query);
		$stmt->execute();
                
                $meta = $stmt->result_metadata();
                
                while( $field = $meta->fetch_field() ) {
                    $parameters[] = &$row[$field->name];
                }
                
               call_user_func_array(array($stmt, 'bind_result'), $parameters);
                
		while($stmt->fetch()) {
                    $x = array();
                    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");

print_r($items); // $items contains the returned rows

以上是关于PHP PHP准备语句 - 带有未定义的变量数传递给bind_result()的主要内容,如果未能解决你的问题,请参考以下文章

运行准备好的语句 (MySQL/PHP)

PHP Foreach 和未设置变量

使用准备好的语句的 PHP 注册脚本

如何将php变量传递给twig模板

php mysqi bind_param 变量数与准备好的语句中的参数数不匹配[重复]

Codeigniter:将数据传递给查看未定义的变量