Reputation: 952
I am trying to get a value from the database. If this value is equal to 1, it needs to show the text "One".
But my script is not working. To check if the result of the sql statement is equal to 1 i am using the following part:
if ($result==1)
{
echo 'One';
}
But this is the result I get:
else
{
echo 'None';
}
I think that $result==1
is not representing a integer.
Does anybody know how I can make this script working?
Here is other part of my script:
<?php
$user_name = "root";
$password = "";
$database = "db";
$server = "localhost";
$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);
if ($db_found) {
$result =mysql_query("SELECT id FROM users WHERE user_id=1");
if ($result==1)
{
echo 'One';
}
if ($result==2)
{
echo 'Two';
}
if ($result==3)
{
echo 'Three';
}
else
{
echo 'None';
}
}
else {
print "Database NOT Found.";
mysql_close($db_handle);
}
?>
Upvotes: 0
Views: 66
Reputation: 7294
Please try this.
<?php
$user_name = "root";
$password = "";
$database = "db";
$server = "localhost";
$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);
if ($db_found) {
$result =mysql_query("SELECT id FROM user WHERE user_id = 1");
$result = mysql_fetch_array($result);}
if($result){
echo $result['id'];
}
else
{
echo 'None';
}
mysql_close($db_handle);
?>
Upvotes: 0
Reputation: 11830
Change to this :
$result =mysql_query("SELECT id FROM users WHERE user_id=1");
$count = mysql_num_rows($result);
if ($count == 1) {
echo 'One';
}
and so on
Note :- The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead
If you want exact value from sql, use below code :
$res = mysql_fetch_row($result);
if ($res[0] == 1) {
echo 'One';
}
Upvotes: 1