2

I am trying to get hold of 1 record from a MySQL table using PHP. I have tried many different SELECT statements, while they all work in MYSQL they refuse to return any result in php.

The countriesRanking table is a simple two column table

country    clicks
------     ------
0        222
66       34 
175      1000
45       650

The mysql returns the ranking of the country column (1, 2, 3, etc..) and it returned all results EXCEPT the first ranked country. Eg when country=175, should return 1 but no result returned. Direct query via web browser return blank page, no error message. My PHP code

$result = mysql_query("SELECT FIND_IN_SET(clicks,
(SELECT GROUP_CONCAT(DISTINCT clicks  ORDER BY clicks DESC) 
FROM countriesRanking)) rank FROM countriesRanking
WHERE country = '$country'") or die(mysql_error());

$row =  mysql_fetch_assoc($result) or die(mysql_error());
$theranking = $row['rank'];
echo $theranking;

EDIT

I tried the following but get the same blank page

var_dump($row['rank']);

EDIT 2

For a successful query print_r($result) returned something like Resource id #4. While print_r($row) returned Array ( [0] => 4 [rank] => 4 ). But when querying for the top ranking country. eg country=175, it returned a blank page.

PutraKg
  • 2,226
  • 3
  • 31
  • 60
  • This exact question was closed, why do you reopen it. http://stackoverflow.com/questions/14295509/mysql-returns-empty-result-in-php and http://stackoverflow.com/questions/14294542/find-the-ranking-of-an-integer-in-mysql – Mr. Radical Jan 13 '13 at 22:00

4 Answers4

1

Assuming you are not getting an error that means you are successfully connecting to your database. You are just not getting back any values. Try:

$row = mysql_fetch_row($result);

I would usually think $row = mysql_fetch_assoc($result); would work but if neither of these work it must be something within your mysql_query.

Devon Bernard
  • 2,250
  • 5
  • 19
  • 32
  • I've just tried $row = mysql_fetch_row($result); But it didn't work also. It returned blank page for other queries as well. I tried many other SELECT statements but they all refused to return the number 1 ranking. – PutraKg Jan 13 '13 at 05:25
  • @PutraKg: Alright, then are you sure your SELECT query is correct? Try a simpler example first to see if you can get a value that way... meaning the query was the issue not the other code. $result = mysql_query("SELECT rank FROM countriesRanking WHERE country = '$country'") or die(mysql_error()); – Devon Bernard Jan 13 '13 at 05:34
  • Yes the SELECT query was tested in phpmyadmin and it returned the correct value. As for your suggestion, 'rank' is not one of the columns. The rank has to be 'calculated' via the query. – PutraKg Jan 13 '13 at 05:40
  • @PutraKg: Alright if you tried dumping the $result and $row variables to see what those are set as? I would try and output those to see what those look like and find where any potential hold up could be. – Devon Bernard Jan 13 '13 at 05:45
  • For a successful query print_r($result) returned something like Resource id #4. While print_r($row) returned Array ( [0] => 4 [rank] => 4 ). But when querying for the top ranking country. eg country=175, it returned a blank page – PutraKg Jan 13 '13 at 06:00
  • @PutraKg: Interesting well in that case if print_r($row) returned that array then $row[rank] should equal 4. Maybe rank is being stored as a variable not a string. If I am not reading your comment incorrectly try $theranking = $row[rank]; without the single quotes. – Devon Bernard Jan 13 '13 at 06:04
  • I get "Use of undefined constant rank" when removing the single quotes – PutraKg Jan 13 '13 at 06:09
  • @PutraKg: Wow that is confusing... if print_r($row) returned that array you left in the comments than your code should work... Are you even able to return $row[0] because that was set as 4 also? – Devon Bernard Jan 13 '13 at 06:11
  • Yes this is confusing.. It returned the correct value using $row[0] – PutraKg Jan 13 '13 at 06:13
  • @PutraKg: Well I know it is a little unconventional but since you are returning only one value and those two values will always be the same I guess you could just use that. Even though it is weird how 'rank' is not working. – Devon Bernard Jan 13 '13 at 06:15
  • That's right. But the main problem I am having is not getting the return value, it's getting the return value when the rank is number one. It returns a blank page for both $row[0] and $row['rank'] – PutraKg Jan 13 '13 at 06:18
1

You can debug this as below.

  1. make sure your SQL is correct by running it on PHPMyAdmin and check the result.
  2. make sure you don't have any fatal errors, if you get a blank page there is something wrong. Turn on PHP errors and see.
  3. print_r($result) and see whether it's empty.
  4. if you want first ranking only then add the LIMIT 1 to your query
Techie
  • 44,706
  • 42
  • 157
  • 243
  • 1. Yes the SQL was tested on PHPMYAdmin and the produce the correct result.
    2. Added ini_set('display_errors',1); error_reporting(E_ALL); but no message when I get the blank page.
    3. print_r($result) returned a blank page
    4. Done that
    – PutraKg Jan 13 '13 at 05:48
1

If you want only one record from database than you have to use limit in query

$result = mysql_query("SELECT FIND_IN_SET(clicks,
(SELECT GROUP_CONCAT(DISTINCT clicks  ORDER BY clicks DESC) 
FROM countriesRanking)) rank FROM countriesRanking
WHERE country = '$country' LIMIT 1") or die(mysql_error());

and after that use while loop

while($row =  mysql_fetch_assoc($result)) {
    $theranking = $row['rank'];
}
echo $theranking;
Yogesh Suthar
  • 30,424
  • 18
  • 72
  • 100
  • Thanks. Just tried that but it's still returning blank page for the number one ranking – PutraKg Jan 13 '13 at 05:32
  • @PutraKg have you tried this same query in phpmyadmin? may be it doesn't return any result. – Yogesh Suthar Jan 13 '13 at 05:34
  • Yes I always try the query in phpmyadmin first to make sure. It returned the correct result there but it did not return the value when assigning the result in php variable. – PutraKg Jan 13 '13 at 05:35
  • @PutraKg Ok. If it is returning result in phpmyadmin then try this code `print_r($row)` and show its result in your question. – Yogesh Suthar Jan 13 '13 at 05:38
  • OK print_r($row) return Array ( [0] => 2 [rank] => 2 ) for other query but return blank page when asked for the first ranking. – PutraKg Jan 13 '13 at 05:47
  • OK, tried that. It still returning blank when querying for the top ranked country but return proper results for other queries. – PutraKg Jan 13 '13 at 12:52
1

Give this a try. It is based on your earlier question. MYSQL returns empty result in PHP
MYSQLI version:

<?PHP
function rank(){
/* connect to database */
$hostname = 'server';
$user = 'username';
$password = 'password';
$database = 'database';

    $link = mysqli_connect($hostname,$user,$password,$database);

    /* check connection */

    if (!$link){ 
        echo ('Unable to connect to the database');
    }

    else{
    $query = "SELECT COUNT(*) rank FROM countryTable a JOIN countryTable b ON a.clicks <= b.clicks WHERE a.country = 175";
    $result = mysqli_query($link,$query);
    $arr_result =  mysqli_fetch_array($result,MYSQLI_BOTH); 
        return $arr_result['rank'];
    }
    mysqli_close($link);
}

echo rank();

?>

MYSQL version:

<?PHP
function rank(){
  /* connect to database */
  $hostname = 'server';
  $user = 'username';
  $password = 'password';
  $database = 'database';

  $link = mysql_connect($hostname,$user,$password);

  /* check connection */

  if (!$link){ 
    echo ('Unable to connect to the database');
  }

  else{
  $query = "SELECT COUNT(*) rank FROM countryTable a JOIN countryTable b ON a.clicks <= b.clicks WHERE a.country = 66";
  mysql_select_db($database);
  $result = mysql_query($query);
  $arr_result = mysql_fetch_array($result,MYSQL_BOTH); 
  return $arr_result['rank'];
  }
  mysql_close($link);
}

echo rank();

?>
Community
  • 1
  • 1
Mr. Radical
  • 1,847
  • 1
  • 19
  • 29
  • I get 'Array' for the top ranked country – PutraKg Jan 13 '13 at 13:02
  • If you change "return $arr_result" into "return $arr_result['rank'] " it should give you the correct result. – Mr. Radical Jan 13 '13 at 14:00
  • Finally! Yes it gives the correct result. But it made me wonder why it doesn't work with mysql. I am not a php programmer so converting my script to mysqli will require me to start from the beginning. – PutraKg Jan 13 '13 at 15:33
  • Hi, it does work with mysql. However, as soon as you upgrade to version 5.5. from PHP mysql will not work. I will recode it into mysql. – Mr. Radical Jan 13 '13 at 15:50