简体   繁体   English

如何在mysqli中做mysql_fetch_assoc?

[英]How to do mysql_fetch_assoc in mysqli?

How can I do the following in mysqli prepare statement?如何在 mysqli prepare 语句中执行以下操作?

$result = mysql_query($sql);
$data = mysql_fetch_assoc($result);
return $data:

You need to use the MySQLi version of the functions:您需要使用 MySQLi 版本的函数:

$result = mysqli_query($sql);
$data = mysqli_fetch_assoc($result);
return $data;

That should do it, you also might want to take a look at:应该这样做,您可能还想看看:

That's pretty simple after you've created the connection to mysql somewhere:在某处创建到 mysql 的连接后,这非常简单:

<?php
// create the connection to mysql.
$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db');
?>

The refer to the mysqli object when you need to do a query or do something else.当您需要执行查询或执行其他操作时,请引用 mysqli 对象。

<?php
// get the result object.
$result = $mysqli->query($sql);
// fetch the result row.
$data = $result->fetch_assoc();

return $data;
?>

Then at some point (if you have a class, you can write a destructor) you should close the connection to mysql if you won't need it anymore.然后在某个时候(如果你有一个类,你可以编写一个析构函数)如果你不再需要它,你应该关闭与 mysql 的连接。

<?php
$mysqli->kill($mysqli->thread_id);
$mysqli->close();
?>

You can do much more with the result object you have.你可以用你拥有的结果对象做更多的事情。 So read more about the MySQLi_Result here:因此,请在此处阅读有关MySQLi_Result更多信息:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM