Can anyone tell me why row[0]
has data, but row[1]
to row[3]
are undefined
(empty)?
I have 4 rows in my database having bookid = '35'
.
<?php
$mysqli = new mysqli('localhost', 'root', '', 'assignment3');
$query = "select image from images where BookId='35'";
$result = $mysqli->query($query);
$row = $result->fetch_array(MYSQLI_NUM);
echo $row[0];
echo $row[1];
echo $row[2];
echo $row[3];
?>
You need to use fetch_array
every time you want to extend to the next row. Using the numeric indices ( 0, 1, 2 ... n
) only retrieve the columns by number in the row.
Try this:
$row = $result->fetch_array(MYSQLI_NUM);
print_r($row); // row 1
$row = $result->fetch_array(MYSQLI_NUM);
print_r($row); // row 2
$row = $result->fetch_array(MYSQLI_NUM);
print_r($row); // row 3
$row = $result->fetch_array(MYSQLI_NUM);
print_r($row); // row 4
If this makes sense to you, you can even abstract this into a for
loop:
for ($i = 0; $i < 4; $i++) {
$row = $result->fetch_array(MYSQLI_NUM);
print_r($row);
}
It is much convenient to use mysqli_fetch_all()
, since it returns all the rows as an array in a single step. But it may consume much memory in case of large result-set.
Eg.
<?php
$mysqli = new mysqli('localhost', 'root', '', 'assignment3');
$query = "select image from images where BookId='35'";
$result = $mysqli->query($query);
//MYSQLI_NUM for numerical array,
//This will return both associative and numerical, if not specify any parameters.
$row = $result->mysqli_fetch_all(MYSQLI_NUM);
//this will print all rows
print_r($row);
//now it is valid to call
echo $row[0];
echo $row[1];
echo $row[2];
echo $row[3];
?>
select image from images where BookId='35'
This only returns one column so this is why $row[0] works and $row[1]
You need this:
SELECT * FROM images WHERE BookId = '35'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.