php - 这段代码有什么问题?

标签 php mysql

我有以下代码:

function query_tip($title,$desc)
{
    $q1=("SELECT id, company, name FROM abc
            where ('$title' LIKE CONCAT('% ',company,' %') or
                   '$desc' LIKE CONCAT('% ',company,' %') or
                   '$title' LIKE CONCAT('% ',name,' %') or
                   '$desc' LIKE CONCAT('% ',name,' %'))
              AND company != ''
              AND name != ''");
    $r1=mysql_fetch_array($q1);
    $id=$r1['id'];
    return $id_nse;
}

在这里,$title 包含诸如“我的名字叫安娜”之类的数据,$desc 包含诸如“我是 xyz 公司的程序员,我在 PHP 平台上工作”之类的数据。

但是,它给出了这个错误:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /bin/whatever on line 2005

但是当我在命令提示符下或在我的数据库中运行此查询时,它运行良好且没有错误。

为什么当我通过 PHP 运行此查询时会出错?

最佳答案

您没有运行查询。您可能还想检查您的查询(即如果失败则将其打印到屏幕或将其记录到文件中)。看起来有点奇怪。

$q1 = "SELECT id, company, name FROM abc where ('$title' LIKE CONCAT('% ',company,' %') or '$desc' LIKE CONCAT('% ',company,' %') or '$title' LIKE CONCAT('% ',name,' %') or '$desc' LIKE CONCAT('% ',name,' %')) AND company != '' AND name != ''";
$res = mysql_query($q1);
$r1 = mysql_fetch_array($res);

关于php - 这段代码有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1315303/

相关文章:

php - Mysql distinct with count with 条件

mysql - 1064 Mysql 查询中的 Sql 语法错误

php - codeigniter 表单验证和数组作为字段名称

php - 在php后台运行url

mysql - 对不同条件下的相同值求和

javascript - 我们能知道用户从哪个页面访问了php中的这个页面吗?

mysql - 如果 "function.mysql-connect"出现在我的网站搜索数据中,人们在试图破解什么?

php - 如何使用 URL 传递 PHP 变量

php - 使用 MySQL 禁止通配符 IP

php - 我的论坛搜索不能只是搜索主题