data extraction??

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
suziecorbett
Forum Newbie
Posts: 3
Joined: Wed Nov 12, 2003 5:59 am
Location: dublin-ireland

data extraction??

Post by suziecorbett »

hey guys, I'm having a problem that's really driving me mad and i'd really appreciate any help ye could give me! Basically I'm trying to print out a value from a database onto a web page, but everytime i try the following code, regardless of which table i try it on, it prints out
id resourse #3.
it will compare the data fine. like if its a number it will print out wether it's greater that ten etc.

Any ideas??I'm using php as my scripting language



//query the level reached from the database
$query = " SELECT Exam1 FROM Users WHERE username='$username'";
$result = mysql_query( $query )
or die( "sss" );

echo($result);
?>
?>
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

[php_man]mysql_fetch_array[/php_man]()
suziecorbett
Forum Newbie
Posts: 3
Joined: Wed Nov 12, 2003 5:59 am
Location: dublin-ireland

Post by suziecorbett »

thanks a million sami, that worked great, the code is now printing out the value held in the database, my problem now is that i need to compare that value with another as follows,

$result = mysql_query(" SELECT Exam1 FROM Users WHERE username='$username'");
while($row=mysql_fetch_array($result))
{
printf("Score: %s", $row[0]);
}
if($row[0]>=4){
echo("Passed");}
else echo ("Failed");


mysql_free_result($result);


printing the score is working fine, but it's printing out failed regardless of what number is stored in the array.
any ideas ye have would be greatly appreciated.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Try:

Code: Select all

$result = mysql_query("SELECT Exam1 FROM Users WHERE username='$username'");

$row = mysql_fetch_assoc($result);
$score = $row['Exam1'];

echo 'Score: '.$score.'<br />';

if ($score >= 4) {
    echo 'Passed';
} else {
    echo 'Failed';
}
Mac
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Code: Select all

$result = mysql_query(" SELECT Exam1 FROM Users WHERE username='$username'"); 
while($row=mysql_fetch_array($result)) 
{ 
   printf("Score: %s", $row[0]); 
   if($row[0]>=4) 
     echo("Passed"); 
   else echo ("Failed"); 
} 

mysql_free_result($result);
?
Post Reply