-2

Can anyone please explain why I am getting no result from my below script:
When I run the query in phpmyadmin it returns the correct result (the id), but running my below php script it echo´s out "No row found!";

<?php
if(isset($_SESSION['word'])) {
  $word = $_SESSION['word']."<br>";
    echo $word;
    $query = ("SELECT id FROM customer WHERE mobil = $word");
    if (!$query) {
    die('Invalid query: ' . mysql_error());
    }
    else {  
            $results = mysql_query($query);
            while ($row = mysql_fetch_assoc($results)) {
            echo '<pre>', print_r($row), '<pre>';
        }
            if (!$row) {
                echo "No row found!";
            }
        }
}
?>
CRABOLO
  • 8,605
  • 39
  • 41
  • 68
Nomis
  • 437
  • 1
  • 4
  • 13

6 Answers6

2

Your query is invalid, it should be like this:

$query = mysql_query("YOUR QUERY");

Also, you shouldn't use Mysql_ since this is deprecated and soon will be removed. Use PDO or MySQLI instead. You can find more info here:

Why shouldn't I use mysql_* functions in PHP?

Community
  • 1
  • 1
Matheno
  • 4,112
  • 6
  • 36
  • 53
0

try this

    $results = mysqli_query($query);
     if(mysqli_num_rows($results)>0){
        while ($row = mysqli_fetch_assoc($results)) {
        echo '<pre>', print_r($row), '<pre>';
    }
     else{
            echo "No row found!";
        }
    }
user3510665
  • 218
  • 3
  • 13
0

try this:

$query = ('SELECT id FROM `customer` WHERE mobil = "$word" ');
Ōkami X Oukarin
  • 435
  • 5
  • 16
0

It should be,

<?php
require_once("db_connect.php");
  if(isset($_SESSION['word'])) {
     $word = $_SESSION['word']."<br>"; //Getting word here       
     $query = "SELECT id FROM customer WHERE mobil = $word";
     $result = mysqli_query($db,$query); //$db holds the database connection
     if (!$result) 
       {
         die('Invalid query: ' . mysqli_error($db));
      }
     else
    {
     $result_rows = mysqli_num_rows($result); //Getting the no. of rows
     if($result_rows!=0)                      // If any record present,the below code executes
     {
       while ($row = mysqli_fetch_assoc($result)) 
       {
            echo '<pre>', print_r($row), '<pre>';
        }
     }
     else
     {
       echo "No rows found";                 // If no record found,this prints.
     }
   }
 }
 ?>

Database connection (db_connect.php) :

$db = mysqli_connect("localhost","username","password","database_name");

Note : Mysql is depreciated. You should use either Mysqli or PDO.

user3289108
  • 770
  • 5
  • 10
  • 29
0

Your query is wrong.

Change this:

$query = ("SELECT id FROM customer WHERE mobil = $word");

To:

$query = ("SELECT id FROM customer WHERE mobil = '".$word."'");

But your code has many security issues.

0

UPDATE and SOLVED My above script works fine, thanks to @CodingAnt (who asked: what data type this mobil fields holds?) - I checked my database and it turns out mobil was set to VARCHAR instead of INT. Changed it to INT and now everything works.

Nomis
  • 437
  • 1
  • 4
  • 13