php 如何在mysqli中做mysql_fetch_assoc?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5689564/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:13:09  来源:igfitidea点击:

How to do mysql_fetch_assoc in mysqli?

phpmysqli

提问by dev

How can I do the following in mysqli prepare statement?

如何在 mysqli prepare 语句中执行以下操作?

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

回答by Rihards

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_Resulthere:

你可以用你拥有的结果对象做更多的事情。所以阅读更多关于MySQLi_Result这里的信息:

回答by alizahid

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:

应该这样做,您可能还想看看: