QiZhi QiZhi - 2 months ago 7
PHP Question

Getting a specific column and row from a fetched array

I have seen the answer for MYSQL - Select specific value from a fetched array but it is showing an error for me.

It says "Notice: Undefined offset: 1" at the echo line. I can't figure out what have i done wrong.

$qry = "Select Address, Phone from People where name='david'";

mysqli_connect('localhost' , 'root' , '' ,'database');

$result = mysqli_query(mysqli_connect('localhost' , 'root' , '' ,'database'), $qry);

mysqli_close(mysqli_connect('localhost' , 'root' , '' ,'database'));

$row = array();

while( $row[] = mysqli_fetch_array( $result ) );

echo $row[1]['Address'];
echo $row[1]['Phone'];

Answer

You're opening 3 times the connection. PHP may read the wrong link to get your query. Try this :

$qry = "Select Address, Phone from People where name='david'";

$ptr = mysqli_connect('localhost' , 'root' , '' ,'database');

$result = mysqli_query($ptr, $qry);

$row = array();

while( $row[] = mysqli_fetch_array( $result ) );

mysqli_close($ptr);

echo $row[1]['Address'];
echo $row[1]['Phone'];