【问题标题】:Query would run directly on MySQL but not through PHP查询将直接在 MySQL 上运行,而不是通过 PHP
【发布时间】:2015-08-05 10:17:09
【问题描述】:

我有一个简单的 MySQL 查询

select * from tutor where verified = 0 and alert_by < '2015-08-05' LIMIT 0,1

现在,直接通过 phpMyAdmin 运行它会提供所需的结果,但是,当通过一组 PHP 语句执行此查询时,它不会返回任何内容。以下是我在 PHP 中的代码

$this_date = date("Y-m-d");

$query = "select * from tutor where verified = 0 and alert_by < '$this_date' LIMIT 0,1";

$contact = mysqli_query($conn, $query);
$row = $contact->fetch_array(MYSQLI_ASSOC);

但是,$row 是空的,我似乎无法弄清楚这一点。我知道这看起来微不足道,但有点烦人。

注意:从查询中删除 "and alert_by &lt; '$this_date'" 可以正常工作。

【问题讨论】:

  • 那么mysqli_query的结果是什么?
  • 请检查我的问题的注释部分,如果错误与mysqli_connect() 有关。查询根本不会运行。
  • 我们需要更多信息。 var_dump $conn, $contact 并告诉我们
  • 好的,你能在$query声明之后尝试var_dump($query);吗?
  • 这里:string(76) "select * from tutor where verified = 0 and alert_by &lt; '2015-08-05' LIMIT 0,1"

标签: php mysql


【解决方案1】:

检查

  1. 连接

    $con = mysqli_connect("localhost","my_user","my_password","my_db");
    
    if (mysqli_connect_errno())
    {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    
  2. 像这样使用mysqli_query

    $contact = mysqli_query($con,"select * from tutor where verified = 0 and alert_by < '$this_date' LIMIT 0,1");
    
  3. 像这样使用fetch array示例#2 程序样式

    $row = mysqli_fetch_array($contact, MYSQLI_ASSOC)
    

【讨论】:

  • 都试过了,还是不行。
  • 你能说出 Namit 写的有什么问题吗?可能没有其他编写代码的方法,但关键是你应该告诉他的代码有什么问题?
猜你喜欢
  • 2011-02-17
  • 1970-01-01
  • 2012-10-17
  • 2016-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-29
  • 2010-12-10
相关资源
最近更新 更多