0

I am trying to build a script in PHP, the script needs to select a id from the database. If the selected id = 1 the scripts needs to show the text "One"

Here is what I have:

<?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);
}

?>

After running this script I get: OneTwoThree in my screen.

What am I doing wrong? How can I write this script so it will give me the right text?

andrewsi
  • 10,807
  • 132
  • 35
  • 51
John
  • 904
  • 8
  • 22
  • 56
  • 5
    You aren't comparing. `=` sets, `==` compares, `===` compares value and type. See http://php.net/manual/en/language.operators.comparison.php vs. http://php.net/manual/en/language.operators.assignment.php – chris85 May 31 '16 at 20:46
  • I also don't see you fetching the results from the query? Is there a reason for this? – Alon Eitan May 31 '16 at 20:48
  • 1
    Additional note, if only one of those cases (1,2,3, etc.) can occur you might want to use a `switch` instead of multiple `if`s. – chris85 May 31 '16 at 20:50
  • Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard May 31 '16 at 20:53
  • With `==` and `===` I get `None` – John May 31 '16 at 20:57
  • `id` in users is `int(10)`. So I think that == or === needs to work. But it isn't working – John May 31 '16 at 21:06
  • Einstein Hassan Zaman thanks for the edit, but this isn't working to – John May 31 '16 at 21:09
  • Did you run a `fetch` after the query? Update the question with your exact code. – chris85 May 31 '16 at 21:20
  • This is my exact code – John Jun 01 '16 at 02:20

0 Answers0