# mySQL if no results found **....}?



## ParsaAkbari (May 10, 2009)

*[SOLVED] mySQL if no results found **....}?*

function getrank($userid) **
global $clanrank;
//Database configuration:
include("configuration.php");

//Connect to database:
mysql_connect("$server", "$username", "$password") or die(mysql_error());
mysql_select_db("$database") or die(mysql_error());

$result = mysql_query("SELECT * FROM ranklist WHERE user_id='$userid'");

// Turn the result row into a array
$array = mysql_fetch_array($result);

// Assign globalised variable to sellected data
$clanrank = $array['rank'];

}


This is my current code for retriving the users rank from a mySQL database. Some users don't have a entry on the table and are therefore "recruit" rank. Once thier rank raises i add a entry on the table for them.

Is there anyway to tell PHP 
if (no entry on table) ** $rank = recruit }
else **
// Turn the result row into a array
$array = mysql_fetch_array($result);

// Assign globalised variable to sellected data
$clanrank = $array['rank'];
}

PS: Im NOT SURE where i should do the mysql_fetch_array(); function.
before the if - else statement or inside it.


----------



## ParsaAkbari (May 10, 2009)

While puzzling over this i had a strange flash of intelligence.
Login scripts will use a similar method to me to make sure your username and password only match ONE row on the table.

So if i could find a example of a login script i could change it to make it do a certain command if a certain SQL queiry matched NO rows.

And the result (for anybody who is experiancing the same problem) is:


function getrank($userid) **
global $clanrank;
//Database configuration:
include("configuration.php");

//Connect to database:
mysql_connect("$server", "$username", "$password") or die(mysql_error());
mysql_select_db("$database") or die(mysql_error());

$result = mysql_query("SELECT * FROM ranklist WHERE user_id='$userid'");

//COUNTS THE ROWS the $result came up with.
$countrows= mysql_num_rows($result);

if ($countrows == 0) ** $clan = recruit; }
else 
// Turn the result row into a array
$array = mysql_fetch_array($result);

// Assign globalised variable to sellected data
$clanrank = $array['rank'];
}


----------

