2012-05-09 61 views
3

我有一些PHP代碼是這樣的:從MySQL轉換爲mysqli的(mysql_fetch_array)

$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面向對象的風格)

我不確定「$ result」是什麼意思。

這是我已經轉換我的代碼至今:

<?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了很多,但現在它的錯誤上

Fatal error: Call to a member function fetch_array()

回答

4

UPDATE查詢不返回結果對象,它返回TRUE(假設成功)。你然後試圖撥打TRUE->fetch_array(),這顯然不會工作。

現在,做你真正想做的事,試試這個:

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

貌似你試圖使用舊的結果對象(您的更新的結果),以獲得一個新的查詢結果。您需要先運行新的查詢,將其結果分配給一個對象,然後從對象中獲取結果。退房的最後三行腳本的重新編寫:

$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