简体   繁体   中英

Comparing value from MySQL to an int in PHP doesn't work

I am attempting to make my own website, from scratch, for a school project. It is going very well, and I got really far, but I encountered an issue, when comparing a variable to a number in a PHP script.

(I skipped alot of the code, which is not relevant, such as SQL connection etc)

$SQL = "INSERT INTO members (points) VALUES ('800')";

this works fine, and the number gets inserted in to my SQL databse as "int(64)"

then i put the variable in to a session in a PHP script

$_SESSION['points'] = $points[0];

then i put it down to another PHP script

$SQL = mysql_query("SELECT points FROM members WHERE username = $uname");
$points = mysql_fetch_row($SQL);

still fine, when I check $points[0] it is still 800

but I can't compare it in a statement such as this

if ($Points[0] >= 400)
{echo "took it";
}
else {echo "didnt take it";
}

Any idea of what could be wrong? It is bigger, but the variable gets threated as if its lower or empty.

I'm taking a guess here, but I think you intend to compare $_SESSION['points'] when you say "then i put it down to another PHP script" .

You need to update $_SESSION['points'] if you want the value to change.

In addition, variables are case sensitive. So in your if statement, you should be using lower case $points . You can check what mysql returns by doing var_dump($points) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM