下面是一些写得不好且严重误解的
PHP代码,没有错误检查.说实话,我正在努力让我的脑袋绕着PHP-> MysqLi函数的迷宫!有人可以提供一个示例,说明如何使用预准备语句收集关联数组中的结果,同时从$stmt获取行数?下面的代码是我正在玩的东西.我认为让我失望的是在store_result之后使用$stmt值然后尝试收集一个关联数组,我不太清楚为什么……
$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"; }
我觉得有点厚颜无耻只是要求代码,但它是我的情况的工作演示,我觉得我需要最终了解实际发生了什么.太感谢了!
没错,数据库功能有点奇怪.你会到达那里.
代码看起来有点不确定,但是它是如何工作的:
建立连接,准备一个语句,绑定参数并执行它,一切都很好.
$result = $stmt->execute(); //execute() tries to fetch a result set. Returns true on succes,false on failure. $stmt->store_result(); //store_result() "binds" the last given answer to the statement-object for... reasons. Now we can use it! if ($stmt->num_rows >= "1") { //Uses the stored result and counts the rows. while($data = $result->fetch_assoc()){ //And here,the answer-object is turned into an array-(object) // which can be worked with nicely. //It loops trough all entries in the array. } }else{ echo "0 records found"; }