简体   繁体   English

Where子句的M​​ySQL语法

[英]MySQL Syntax for Where clause

Im not good in php. 我不擅长PHP。 I have a web application that displays the Data from my MySQL Database. 我有一个Web应用程序,显示MySQL数据库中的数据。 I am not getting any erros but I need to fetch the Data I want. 我没有得到任何错误,但我需要获取我想要的数据。

ex. 恩。 code. 码。

<?php
$dbhost = 'localhost';
$dbuser = 'user';
$dbpass = 'password';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
$token = $_GET['token'];

if(! $conn )
  {
    die('Could not connect: ' . mysql_error());
  }
$sql = "SELECT * FROM table WHERE token = '" . $token . "'";

mysql_select_db('database');
$retval = mysql_query( $sql, $conn );
if(! $retval )
  {
    die('Could not get data: ' . mysql_error());
  }

 while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
   {
     //Displays the data
   echo "The Token is here";
   }

  mysql_close($conn);
  ?>

I have no errors in here. 我在这里没有错误。 But I need to do something like, If the token data is not yet added to the table, then I will put an echo message like.. "The token is not yet added here". 但是我需要做一些事情,如果令牌数据还没有添加到表中,那么我会发出一条回复消息,比如......“这里还没有添加令牌”。 Where I can put the syntax here? 我可以在哪里放置语法? Please help me. 请帮我。 I am new in php. 我是php新手。

Use this: 用这个:

 if(mysql_num_rows($retval)) {
     while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
     {
      //Displays the data
      echo "The Token is here";
     }
   } else {
     echo "The token is not yet added here";
   }

Try with mysql_num_rows like 尝试使用mysql_num_rows

$sql = "SELECT * FROM table WHERE token = '" . $token . "'";
mysql_select_db('database');
$retval = mysql_query( $sql, $conn );
$num_rows = mysql_num_rows($retval);
if($num_rows < 1) {
    echo "The token is not yet added here";
} else {
    echo "Token already added";
}

And try to avoid using mysql_* functions due to they are deprecated.Instead use mysqli_* functions or PDO statements. 并尝试避免使用mysql_*函数,因为它们已被弃用。 mysqli_*使用mysqli_*函数或PDO语句。

Put condition with mysql_num_rows(). 使用mysql_num_rows()放置条件。 If that value is 0 then you can show the message The token is not yet added here else some other codes. 如果该值为0,那么您可以显示消息The token is not yet added here其他一些其他代码。

Try if the following way: 尝试以下方式:

if(isset($_GET['token']))
{
if(! $conn )
  {
    die('Could not connect: ' . mysql_error());
  }
$sql = "SELECT * FROM table WHERE token = '" . $_GET['token'] . "'";

mysql_select_db('database');
$retval = mysql_query( $sql, $conn );
if(! $retval )
  {
    die('Could not get data: ' . mysql_error());
  }

 while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
   {
     //Displays the data
   echo "The Token is here";
   }

  mysql_close($conn);
}
else
{
   echo "The token is not yet added here";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM