1

I have read a-lot of answers on this but they don't seem to be working.

I have the following code:

$amountoflikes=mysql_query("SELECT * FROM  `uc_likes` WHERE `dwable` =  '372'");

This returns the following:

enter image description here

If I wanted to echo the value of dwable in the 2nd row for instance (not involving the initial query).

I've tried:

while($row3 = mysql_fetch_assoc($amountoflikes)){
    $json[] = $row3;
    }
    echo json_encode($json);

But this returns null.

I'm currently using PHP 5.5 (native).

I'm not using MySQLi or MySQL PDO.

Can someone tell me where I'm going wrong. Ideally I'd prefer not to use a loop but I don't know if that's possible.

Thanks!

6
  • Have you tried echo $json[1]['dwable'] in place of your existing echo? Commented Sep 16, 2016 at 17:00
  • @Craig just tried it now and nothing appeared. Commented Sep 16, 2016 at 17:02
  • You need the loop if there are multiple records. Have you tried echo $row3['dwable'];? If you are doing `dwable` = '372' why even execute the query you know dwable will be 372. Commented Sep 16, 2016 at 17:04
  • @chris85 it turns out I think my initial query wasn't returning results with the variables (even though I echoed them out to check). Commented Sep 16, 2016 at 17:07
  • @Ben So issue resolved? Commented Sep 16, 2016 at 17:09

2 Answers 2

1

Try declaring $json as an array above the while:

$json = array();
Sign up to request clarification or add additional context in comments.

1 Comment

It turns out my initial query wasn't returning results with the variables in it (even though I echoed them out to check). However since I added this it changed from Null to [] which made me think this was the case. So thanks!
1

declare your array as follows

$json = array();

and see if you have results before your result

if ($amountoflikes)
{
  while(){...}
}

Comments

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.