-4

I was trying to code where whenever I hit the submit button it will show an alert message when there's no data fetch. Can anyone help me?

if(isset($_POST['submitEstatus'])) {
 if($_POST['empValue'] == $valEmp AND $_POST['ageValue'] == $valAge AND $_POST['genValue'] == $valGen){ 
    $query = "SELECT * FROM users $valEmp $valAge $valGen"; 
    $result = mysql_query($query);
    if ($result = 0) {
        $message = "No Data";
        echo "<script type='text/javascript'>alert('$message');</script>";
    } else {
        while($row = mysql_fetch_array($result)){
            $lat = $row['lat'];
            $lon = $row['lon'];
            $fname = $row['fname'];
            $address = $row['address'];             
            echo("addMarker($lat, $lon, '<b>$fname</b><br />$address');\n");
            $ageSelected = $valAge;
            $empSelected = $valEmp;
            $genSelected = $valGen;
        }     
    }   
}

I have this error:

mysql_fetch_array() expects parameter 1 to be resource, integer given in C:\xampp\htdocs\admin\mapcon.php on line 20

Jonathan Argentiero
  • 5,687
  • 8
  • 29
  • 34
Jake
  • 1
  • 3
    ***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 Jan 26 '17 at 13:02
  • 2
    [Little Bobby](http://bobby-tables.com/) says ***[your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)***. Even [escaping the string](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) is not safe! – Jay Blanchard Jan 26 '17 at 13:02
  • 4
    a) `if($result = 0)` does not *compare*, but *assign*. use `==` b) don't use `mysql`, it's been deprecated and in PHP7 removed. use `mysqli` or `PDO` instead. c) use parameterized statements, otherwise your code is vulnerable to **SQL Injections** – Franz Gleichmann Jan 26 '17 at 13:03
  • 3
    I'm voting to close this question as off-topic because SO is not a tutorial site and this questioner needs to start by learning some basic PHP & SQL. There is next to nothing in the code that makes any sense. – RiggsFolly Jan 26 '17 at 13:14

1 Answers1

0

On your if statement you are not comparing the value:

if ($result = 0) { ....

You are setting 0 to $result.

You should use

if ($result == 0) { ...

fkupper
  • 520
  • 3
  • 9