通过php从mySQL接收数据

通过php从mySQL接收数据,php,mysql,Php,Mysql,任何人都可以解释为什么我的下面的脚本没有得到结果:当我在phpmyadmin中运行查询时,它返回正确的结果(id),但运行下面的php脚本时,它会回显“找不到行!” 您的查询无效,应如下所示: $query = ('SELECT id FROM `customer` WHERE mobil = "$word" '); 此外,您不应该使用Mysql,因为Mysql已被弃用,不久将被删除。 改用PDO或MySQLI。您可以在此处找到更多信息: 试试这个 <?php require_once(

任何人都可以解释为什么我的下面的脚本没有得到结果:
当我在phpmyadmin中运行查询时,它返回正确的结果(id),但运行下面的php脚本时,它会回显“找不到行!”


您的查询无效,应如下所示:

$query = ('SELECT id FROM `customer` WHERE mobil = "$word" ');
此外,您不应该使用Mysql,因为Mysql已被弃用,不久将被删除。 改用PDO或MySQLI。您可以在此处找到更多信息:

试试这个

<?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.
     }
   }
 }
 ?>
$results=mysqli\u查询($query);
如果(mysqli_num_行($results)>0){
while($row=mysqli\u fetch\u assoc($results)){
回显“”,打印($row),“”;
$query = mysql_query("YOUR QUERY");
$db = mysqli_connect("localhost","username","password","database_name");
} 否则{ 回声“找不到行!”; } }
试试这个:

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

注意Mysql已折旧。您应该使用MysqliPDO

您的查询是错误的

更改此项:

致:

但是您的代码有许多安全问题。

更新并解决
感谢@CodingAnt(谁问:这个mobil字段包含什么数据类型?)-我检查了我的数据库,发现mobil被设置为VARCHAR而不是INT。将其更改为INT,现在一切正常。

您的查询无效,请使用$query=mysql\u查询(“从客户那里选择id,mobil=”“$word.”;此mobil字段包含什么数据类型?在$word变量周围使用单引号。我建议使用PDO而不是mysql_*函数。您也可以使用mysqli_*函数而不是mysql_*@CodingAnt
echo gettype($word)$db = mysqli_connect("localhost","username","password","database_name");
$query = ("SELECT id FROM customer WHERE mobil = $word");
$query = ("SELECT id FROM customer WHERE mobil = '".$word."'");