geting the value from a mysql_query

Hello all, I am trying to get a specific value from a mysql_query. This Query returns a table with 1 row and 1 column. I want to use the result inside the table to do a If statement and am having trouble.

here is some of the code.

[code] $table_name2 = “members”;

					$sql2="select Sum(level) from $table_name2 where username = $aname";

					$result2=mysql_query($sql2);

[/code]

this code should return a table where the value inside it is an int. I want to do something like this with the result

if( $result2 > 2){ do this

but it is not doing this. Is there a way to get the value out of the table to compare.

Thanks in advance

Hi,

the mysql_query() function returns a resource, which contains a reference to the mysql result set. So, you need to pull out the answer you are looking for. There are a few different ways to do this, but one way that should work with your code is

$sum = mysql_result($result2, 0); if($sum > 2){//do this.....

http://php.net/ is a good resource for info like this. You can check out the different ways to pull data from the result set:
mysql_fetch_assoc(), mysql_fetch_array() etc.

Thanks waylon999, gonna try it now.

That didnt work, i am now getting this error on my webpage

Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /home/lgrodriguez2/public_html/Forum/forum2/Copy of project 4/Copy of project 4/forum2.php on line 168

Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /home/lgrodriguez2/public_html/Forum/forum2/Copy of project 4/Copy of project 4/forum2.php on line 168

not too sure why the warnings so i am going to look them up but any other help would be good, thanks.

figured those warningsout, was missing a ’ around $aname, but now the error is

Warning: mysql_result() [function.mysql-result]: Unable to jump to row 0 on MySQL result index 4 in /home/lgrodriguez2/public_html/Forum/forum2/Copy of project 4/Copy of project 4/forum2.php on line 170

dont know why its giving me a error. and looking for that error on the net was not too good, so any helpwould be appreciated.

Thanks

hmm,

well to be honest I usually use mysql_fetch_assoc(), so you can change your query to this

$sql2="select Sum(level) as level_sum from $table_name2 where username = $aname";

where the difference is that you give the name ‘level_sum’ to the result column Sum(level)
and then use

$row = mysql_fetch_assoc($result2);
if($row['level_sum'] > 2){//do this.....

So give that a shot and let me know how it works out

Sorry hadnt posted on the new info, been a long saterday, manual labor type. will try it soon.

didnt work, im not getting any errors, but it didnt do what i wanted it to. Basically I want this:
I am creating a web froum with php, and have it all done except for the levels of acces, which is what i am working on, so here is the deal, i am checking whether that person has a high enough access level, if he/she does show them this link, in the following code:

$sql2 = "select level as level_access from $table_name2 where username = '$aname2' ";
$result2 = mysql_query($sql2); 
$row = mysql_fetch_assoc($result2);

if($row['level_access'] == 3){
?>
<tr><td colspan="0" align center bgcolor ="#E6E6E6"><a href="main_Login.php"><strong>special forums</strong></td></tr>
<?php
}
?>

But if the person has 3 for access, it is not showing the special forums like it should. this is because a level 2 should not see this.

anymore help would be appreciated. Thanks

I’d suggest using echo statements and error_reporting(E_ALL); to debug your code. I’m not sure where the problem is in your script, but you should always check to see if what a MySQL query returns is what you expect it to return, by using the following code for example:

$result2 = mysql_query($sql2) or die('Query Error: ' . mysql_error());

I got it finally, thanks for all the help everyone.

Sponsor our Newsletter | Privacy Policy | Terms of Service