Convert from mysql to mysqli (mysql_fetch_array)

I have php code that was something like this:

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

Now I'm trying to convert it to mysqli_fetch_array, as shown here http://php.net/manual/en/mysqli-result.fetch-array.php (Example # 1 Object Oriented Style)

I am not sure if the example means "$ result".

This is what I have so far converted the code to:

<?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 are probably a lot of errors since I am learning mysqli, but right now these are errors on

Fatal error: calling member function fetch_array ()

+3
source share
2 answers

UPDATEqueries do not return result objects, it returns TRUE(if successful). Then you try to call TRUE->fetch_array(), which obviously will not work.

, , , :

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

, ( UPDATE) . , . script, :

$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

+1

All Articles