0

I am trying to display a single row data using mysqli. The query is

$getfield = mysqli_query($con,"select name from as_users where user_id=34");
if (mysqli_num_rows($getfield) > 0) 
{
     while($rowpwd = mysqli_fetch_array($getfield))
        {
           echo $rowpwd['name'];
        }
}

if I print then i get

echo '<pre>';
print_r(mysqli_fetch_array($getfield));
echo '</pre>';

Array
(
    [0] => abc
    [name] => abc
)

But getting the name inside the while loop doesn't work.

Any help is highly appreciated.

6
  • 1
    Print your data inside while Commented Jun 1, 2017 at 7:26
  • you can store them in separate variable inside while loop. Commented Jun 1, 2017 at 7:27
  • use mysqli_fetch_assoc instead of mysqli_fetch_array Commented Jun 1, 2017 at 7:31
  • 1
    Why you use WHILE loop for that but you want only a single row result. Commented Jun 1, 2017 at 7:35
  • @AdhanTimothyYounes Exactly. That's what I am looking for. It will always return a single row. Commented Jun 1, 2017 at 7:37

3 Answers 3

1

Just try this:

$getfield = mysqli_query($con,"select name from as_users where user_id=34");
if (mysqli_num_rows($getfield) > 0) 
{
     $rowpwd = mysqli_fetch_array($getfield)['name'];
     echo $rowpwd;
}

OR

$getfield = mysqli_query($con,"select name from as_users where user_id=34");
if (mysqli_num_rows($getfield) > 0) 
{
   $rowpwd = mysqli_fetch_array($getfield);
   echo $rowpwd['name'];
}
Sign up to request clarification or add additional context in comments.

1 Comment

Brilliant. Thanks a lot mate. This is exactly what I was looking for.
1

Try this:

$getfield = mysqli_query($con,"select name from as_users where user_id=34");
if (mysqli_num_rows($getfield) > 0) 
$whatYouWant = array();
{
     while($rowpwd = mysqli_fetch_array($getfield))
        {
           //echo $rowpwd['name'];
           $whatYouWant[] = $rowpwd['name'];
        }
}



echo '<pre>';
print_r($whatYouWant);
echo '</pre>';

11 Comments

Let me try with this.
I did var_dump($whatYouWant); but it says NULL. I am sure the value is there.
Thanks @VK321. Help me out
try echo mysqli_num_rows($getfield); and tell me what you get?
@VK321 It prints 1.
|
0

Change:

mysqli_fetch_array

To: If you want key of row

mysqli_fetch_assoc 

To:If you want index of row

mysqli_fetch_row

3 Comments

How does this help?
No error but it's not printing the value inside the loop.
inside loop what you get when you do print_r($rowpwd);

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.