Arb*_*ter 8 php mysqli associative-array prepared-statement fetch
下面是一些写得不好且严重误解的PHP代码,没有错误检查.说实话,我正在努力让我的脑袋绕着PHP-> MySQLi函数的迷宫!有人可以提供一个示例,说明如何使用预准备语句收集关联数组中的结果,同时从$ stmt获取行数?下面的代码是我正在玩的东西.我认为让我失望的是使用$stmt后的值store_result,然后尝试收集一个关联数组,我不太清楚为什么......
$mysqli = mysqli_connect($config['host'], $config['user'], $config['pass'], $config['db']);
$stmt = $mysqli->prepare("SELECT * FROM licences WHERE generated = ?");
$stmt->bind_param('i', $core['id']);
$result = $stmt->execute();
$stmt->store_result();
if ($stmt->num_rows >= "1") {
while($data = $result->fetch_assoc()){
//Loop through results here $data[]
}
}else{
echo "0 records found";
}
Run Code Online (Sandbox Code Playgroud)
我觉得有点厚颜无耻只是要求代码,但它是我的情况的工作演示,我觉得我需要最终了解实际发生了什么.太感谢了!
我搜索了很长时间但从未找到需要正确回复的文档,但我做了我的研究.
$stmt->get_result()替换$stmt->store_result()为此目的.所以,如果我们看到
$stmt_result = $stmt->get_result();
var_dump($stmt_result);
Run Code Online (Sandbox Code Playgroud)
我们得到
object(mysqli_result)[3]
public 'current_field' => int 0
public 'field_count' => int 10
public 'lengths' => null
public 'num_rows' => int 8 #That we need!
public 'type' => int 0
Run Code Online (Sandbox Code Playgroud)
因此,我提出以下通用解决方案.(我包括我使用的错误报告)
#Prepare stmt or reports errors
($stmt = $mysqli->prepare($query)) or trigger_error($mysqli->error, E_USER_ERROR);
#Execute stmt or reports errors
$stmt->execute() or trigger_error($stmt->error, E_USER_ERROR);
#Save data or reports errors
($stmt_result = $stmt->get_result()) or trigger_error($stmt->error, E_USER_ERROR);
#Check if are rows in query
if ($stmt_result->num_rows>0) {
# Save in $row_data[] all columns of query
while($row_data = $stmt_result->fetch_assoc()) {
# Action to do
echo $row_data['my_db_column_name_or_ALIAS'];
}
} else {
# No data actions
echo 'No data here :(';
}
$stmt->close();
Run Code Online (Sandbox Code Playgroud)
$result = $stmt->execute(); /* function returns a bool value */
Run Code Online (Sandbox Code Playgroud)
参考: http: //php.net/manual/en/mysqli-stmt.execute.php
所以编写$stmt->execute();查询执行就足够了。
基本思想是遵循以下顺序:
1. 建立连接。(现在,在使用 sqli 或 PDO 方法时,您只需一步即可建立连接并与数据库连接)
2. 准备查询模板
3. 将参数与变量绑定
4. (如果未设置或您已设置变量,则设置变量的值希望更改值),然后执行您的查询。
5. 现在获取数据并开始工作。
6. 关闭连接。
/*STEP 1*/
$mysqli = mysqli_connect($servername,$usrname,$pswd,$dbname);
/*STEP 2*/
$stmt = $mysqli->prepare("SELECT * FROM licences WHERE generated = ?");
/*Prepares the SQL query, and returns a statement handle to be used for further operations on the statement.*/
//mysqli_prepare() returns a statement object(of class mysqli_stmt) or FALSE if an error occurred.
/* STEP 3*/
$stmt->bind_param('i', $core['id']);//Binds variables to a prepared statement as parameters
/* STEP 4*/
$result = $stmt->execute();//Executes a prepared Query
/* IF you wish to count the no. of rows only then you will require the following 2 lines */
$stmt->store_result();//Transfers a result set from a prepared statement
$count=$stmt->num_rows;
/*STEP 5*/
//The best way is to bind result, its easy and sleek
while($data = $stmt->fetch()) //use fetch() fetch_assoc() is not a member of mysqli_stmt class
{ //DO what you wish
//$data is an array, one can access the contents like $data['attributeName']
}
Run Code Online (Sandbox Code Playgroud)
如果想要缓冲客户端的完整结果集,则必须调用 mysqli_stmt_store_result() for (SELECT, SHOW, DESCRIBE, EXPLAIN),以便后续 mysqli_stmt_fetch() 调用返回缓冲数据。
对于其他查询,没有必要调用 mysqli_stmt_store_result(),但如果这样做,在所有情况下都不会损害或导致任何显着的性能。
--reference:php.net/manual/en/mysqli-stmt.store-result.php
和http://www.w3schools.com/php/php_mysql_prepared_statements.asp
遇到与此相关问题的人必须查找上述参考资料,我的答案可能不完美,欢迎大家改进我的答案...