0

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'];
3
  • I'm trying to store the result set in an array, then get the data i want by setting the row and column. Like $row[rowNumber][columnName] Commented Sep 19, 2016 at 15:27
  • I removed my comment, your while() loop is indeed correct, I didn't notice this statement, $row = array();. The problem is, you're closing the connection before while() loop. And there's no need to create a separate connection in each and every places, just create a connection and use the returned handler in the subsequent statements. Commented Sep 19, 2016 at 15:34
  • Don't assign to the array variable in the while() condition. You'll end up adding an extra element to the array containing the false value when you reach the end of the results. Use the normal idiom while ($row = mysqli_fetch_array($result)) { $rows[] = $row; }. Commented Sep 19, 2016 at 16:03

2 Answers 2

1

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'];
Sign up to request clarification or add additional context in comments.

6 Comments

Threw mysqli_close to the last line but still getting the same result.
@QiZhi May be it's returning one row from the result set, because of this ... where name='david'. Do var_dump($row); and see what you're getting.
array (size=1) 0 => null. However I did : while($row2 = mysqli_fetch_array($result)) { echo $row2['Address']."<br>"; } and it returns 4 result.
So i guess there is something wrong in this line "while( $row[] = mysqli_fetch_array( $result ) );" because there isn't 4 rows in the array?
maybe $row is a protected word in the first place !
|
0

Index of php's array starts at 0. If there is only one result, or you want the first one, it will be at $row[0]

//displays the first line's result
echo $row[0]['Address'];
echo $row[0]['Phone'];

1 Comment

There is more than 1 row and even when i use $row[0] there is no output.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.