php 从 mysql 转换为 mysqli (mysql_fetch_array)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10518090/
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-24 22:22:43  来源:igfitidea点击:

Converting from mysql to mysqli (mysql_fetch_array)

phpmysqloopmysqli

提问by ???

I have some php code that was like this:

我有一些像这样的php代码:

$row = mysql_fetch_array ( mysql_query("SELECT * FROM `tblFacilityHrs` WHERE `uid` = '$uid'")); 

I'm now trying to convert it to mysqli_fetch_arrayas shown here http://php.net/manual/en/mysqli-result.fetch-array.php(Example #1 Object oriented style)

我现在正在尝试将其转换mysqli_fetch_array为如下所示http://php.net/manual/en/mysqli-result.fetch-array.php(示例 #1 面向对象的样式)

I'm not sure what the example means by "$result".

我不确定示例中的“$result”是什么意思。

This is what I've converted my code to so far:

到目前为止,这是我将代码转换为的内容:

<?php 
include('../config.php'); 
if (isset($_GET['uid']) ) { 
$uid = $_GET['uid'];
$id = $_GET['id'];  
if (isset($_POST['submitted'])) { 
foreach($_POST AS $key => $value) { $_POST[$key] = mysqli_real_escape_string($value); } 

//Query for tblFacilityHrs
$sql = " UPDATE tblFacilityHrs SET `title`='{$_POST['title']}',`description`='{$_POST['description']}' WHERE `uid` = '$uid' "; 
$result = $mysqli->query($sql) or die($mysqli->error);

//Query for tblFacilityHrsDateTimes
$sql2 = "UPDATE tblFacilityHrsDateTimes SET `startEventDate`='{$_POST['startEventDate']}',`endEventDate`='{$_POST['endEventDate']}', `startTime`='{$_POST['startTime']}',`endTime`='{$_POST['endTime']}',`days`='{$_POST['days']}',`recurrence`='{$_POST['recurrence']},`finalDate`='{$_POST['finalDate']}' WHERE `id` = '$id' "; print $sql2;
$result2 = $mysqli->query($sql2) or die($mysqli->error);

echo ($mysqli->affected_rows) ? "Edited row.<br />" : "Nothing changed. <br />"; 
echo "<a href='list.php'>Back</a>";
} 
$row = $result->fetch_array($mysqli->query("SELECT * FROM `tblFacilityHrs` WHERE `uid` = '$uid'"));
$row2 = $result2->fetch_array($mysqli->query("SELECT * FROM `tblFacilityHrsDateTimes` WHERE `id` = '$id'"));
?>

There is probably a lot wrong with as I am learning mysqli, but right now it errors on

我在学习 mysqli 时可能有很多问题,但现在它出错了

Fatal error: Call to a member function fetch_array()

致命错误:调用成员函数 fetch_array()

回答by Niet the Dark Absol

UPDATEqueries do not return result objects, it returns TRUE(assuming success). You're then trying to call TRUE->fetch_array(), which obviously won't work.

UPDATE查询不返回结果对象,它返回TRUE(假设成功)。然后你试图调用TRUE->fetch_array(),这显然是行不通的。

Now, for doing what you actually want to do, try this:

现在,为了做你真正想做的事情,试试这个:

$row = $mysqli->query("SELECT.....")->fetch_array();

回答by abelito

Looks like you are trying to use an old result object (result of your UPDATE) to get results for a new query. You need to run the new query first, assign its result to an object, and then fetch the results from object. Check out the last three lines of your script re-written:

看起来您正在尝试使用旧的结果对象(更新的结果)来获取新查询的结果。您需要先运行新查询,将其结果分配给一个对象,然后从对象中获取结果。查看重写脚本的最后三行:

$facilityHoursQueryResult = $mysqli->query("SELECT * FROM `tblFacilityHrs` WHERE `uid` = '$uid'");
$facilityHoursDateTimesQueryResult = $mysqli->query("SELECT * FROM `tblFacilityHrsDateTimes` WHERE `id` = '$id'");

$facilityHoursRow = $facilityHoursQueryResult == FALSE ? NULL : $facilityHoursQueryResult->fetch_array();
$facilityDateTimesRow = $facilityHoursDateTimesQueryResult == FALSE ? NULL : $facilityHoursDateTimesQueryResult->fetch_array();
?>

Here is a helpful page with other result object methods: http://www.php.net/manual/en/class.mysqli-result.php

这是一个包含其他结果对象方法的有用页面:http: //www.php.net/manual/en/class.mysqli-result.php