Php mysqli获取结果作为assoc数组
有人能帮我解释一下这个语句/语法吗?我希望结果为关联数组(按字段名索引),不希望编写此“Php mysqli获取结果作为assoc数组,php,mysql,Php,Mysql,有人能帮我解释一下这个语句/语法吗?我希望结果为关联数组(按字段名索引),不希望编写此“$stmt->bind_result($name,$code);”语句。谢谢因为您实际上没有使用任何参数,例如?那么您就不需要做->准备 因此,您可以使用->query()和->fetch\u assoc()机制 $mysqli = new mysqli("localhost", "my_user", "my_password", "world"); /* check connection */ if (m
$stmt->bind_result($name,$code);
”语句。谢谢因为您实际上没有使用任何参数,例如?
那么您就不需要做->准备
因此,您可以使用->query()
和->fetch\u assoc()
机制
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 150,5";
if ($stmt = $mysqli->prepare($query)) {
/* execute statement */
$stmt->execute();
/* bind result variables */
$stmt->bind_result($name, $code);
/* fetch values */
while ($stmt->fetch()) {
printf ("%s (%s)\n", $name, $code);
}
/* close statement */
$stmt->close();
}
/* close connection */
$mysqli->close();
在你的评论之后
这是另一种选择,如果您必须保持准备状态
$query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 150,5";
$result = $mysqli->query($query)
if ( $result === FALSE ) {
echo $mysqli->error;
exit;
}
/* fetch values */
while ($row = $result->fetch_assoc()) {
printf ("%s (%s)\n", $row['name'], $row['code']);
}
调用
$stmt->get_result()
基本上将mysql_stmt
对象转换为mysql_result
对象,因为您实际上没有使用任何参数,例如?
,所以您实际上不需要执行->prepare
因此,您可以使用->query()
和->fetch\u assoc()
机制
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 150,5";
if ($stmt = $mysqli->prepare($query)) {
/* execute statement */
$stmt->execute();
/* bind result variables */
$stmt->bind_result($name, $code);
/* fetch values */
while ($stmt->fetch()) {
printf ("%s (%s)\n", $name, $code);
}
/* close statement */
$stmt->close();
}
/* close connection */
$mysqli->close();
在你的评论之后
这是另一种选择,如果您必须保持准备状态
$query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 150,5";
$result = $mysqli->query($query)
if ( $result === FALSE ) {
echo $mysqli->error;
exit;
}
/* fetch values */
while ($row = $result->fetch_assoc()) {
printf ("%s (%s)\n", $row['name'], $row['code']);
}
调用$stmt->get_result()
基本上将mysql_stmt
对象转换为mysql_result
对象
$query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 150,5";
// there seems to be some confusion in the manual as to
// whether this next statement is needed or not
// see if that helps
$stmt = $mysqli->stmt_init();
if ($stmt = $mysqli->prepare($query)) {
/* execute query */
if (!$stmt->execute()) {
echo $stmt->error;
exit;
}
$result = $stmt->get_result();
/* fetch values */
while ($row = $result->fetch_assoc()) {
printf ("%s (%s)\n", $row['name'], $row['code']);
}
} else {
echo 'Failed to prepare statement';
exit;
}
试试这个
$query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 150,5";
// there seems to be some confusion in the manual as to
// whether this next statement is needed or not
// see if that helps
$stmt = $mysqli->stmt_init();
if ($stmt = $mysqli->prepare($query)) {
/* execute query */
if (!$stmt->execute()) {
echo $stmt->error;
exit;
}
$result = $stmt->get_result();
/* fetch values */
while ($row = $result->fetch_assoc()) {
printf ("%s (%s)\n", $row['name'], $row['code']);
}
} else {
echo 'Failed to prepare statement';
exit;
}
试试这个:
$stmt = $mySqli->query($query);
$countries = mysqli_fetch_all($stmt, MYSQLI_ASSOC);
$stmt->close();
试试这个:
$stmt = $mySqli->query($query);
$countries = mysqli_fetch_all($stmt, MYSQLI_ASSOC);
$stmt->close();
我需要使用'->prepare'Sir Riggs..需要过滤我在本例中忘记的条件stmt。我的不好…谢谢…顺便说一句,如何在注释中为代码键入斜体“”?请在我的键盘上的
1
键的左侧打勾..谢谢…现在我明白了<代码>注释使用小标记格式:[链接](http://example.com)斜体**粗体**code。您的评论将始终通知帖子作者。要同时通知之前的评论者,请提及他们的用户名:@peter或@PeterSmith都可以。了解更多信息…
此框没有出现在我以前的评论中,我以前的评论是用来复制和粘贴符号的..如果您需要保留准备,他选择了另一个选项我已经这样做了,先生….get\u result()
和fetch\u assoc()
似乎未知…我在服务器上安装了LAMP…我需要使用'->prepare'Sir Riggs..需要过滤我在本例中忘记的条件stmt。我的不好…谢谢…顺便说一句,如何在注释中为代码键入斜体“”?请在我的键盘上的1
键的左侧打勾..谢谢…现在我明白了<代码>注释使用小标记格式:[链接](http://example.com)斜体**粗体**code。您的评论将始终通知帖子作者。要同时通知之前的评论者,请提及他们的用户名:@peter或@PeterSmith都可以。了解更多信息…
此框没有出现在我以前的评论中,我以前的评论是用来复制和粘贴符号的..如果您需要保留准备,他选择了另一个选项我已经这样做了,先生….get\u result()
和fetch\u assoc()
似乎不知道….我在我的服务器上安装了LAMP…你只是把PDO
和mysqli
混在一起了,看看PDO
(忘记mysqli
)@szencPDO是windows版的,先生….没有linux的驱动程序这根本不是真的,我在linux PC和服务器上一直使用PDO。请参阅,听起来您好像还没有安装新的mysqlnd
驱动程序。看到你在使用旧版本的PHP了吗?@RiggsFolly我用的是最新版本的PHP,先生..你只是把PDO
和mysqli
混在一起了,看看PDO
(忘记mysqli
)@szencPDO是用于windows的,先生..没有Linux的驱动程序这根本不是真的,我一直在linux PC和服务器上使用PDO。请参阅,听起来您好像还没有安装新的mysqlnd
驱动程序。看到了吗?你使用的是旧版本的PHP吗?@RiggsFolly我使用的是最新版本的PHP,先生。。