fetch
calls fetch a single ROW of data. You're just assigning column #3 of each row, over and over again.
You probably want:
$postcodes = array();
while ($row = ...) {
$postcodes[] = $row[3];
}
var_dump($postcodes);
Pergunta
The following mysql query is getting the last 4 records from the 'residential' table but I'm trying to assign a php variable ($postcode) to each row (the 3rd [3] column in particular) for use in on another page. The following doesn't seem to be split the rows out correctly to assign to each variable?
<?php
$con=mysqli_connect("example.com","peter","abc123","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM residential ORDER BY My_id DESC LIMIT 4");
$postcodes = array();
while ($row = mysqli_fetch_array($result))
{
$postcodes[] = $row[3];
}
echo "location one " . $postcodes[0];
echo "<br>";
echo "location two " . $postcodes[1];
echo "<br>";
echo "location three " . $postcodes[2];
mysqli_close($con);
?>
Solução
fetch
calls fetch a single ROW of data. You're just assigning column #3 of each row, over and over again.
You probably want:
$postcodes = array();
while ($row = ...) {
$postcodes[] = $row[3];
}
var_dump($postcodes);