从MySQL转换为mysqli的(mysql_fetch_array)(Converting fro

2019-07-29 08:13发布

我有一些PHP代码是这样的:

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

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

我不知道是什么的例子是指“$结果”。

这是我已经转换我的代码至今:

<?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'"));
?>

有可能是很多毛病,因为我学习mysqli的,但现在就可以了错误

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

Answer 1:

UPDATE查询不返回结果对象,则返回TRUE (假设成功)。 那么你想打电话TRUE->fetch_array()这显然是行不通的。

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

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


Answer 2:

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

$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();
?>

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



文章来源: Converting from mysql to mysqli (mysql_fetch_array)