变量连接的 mysql 查询字符串在 phpMyAdmin 中运行良好,但在脚本中运行 PHP 则不行

发布于 2024-10-31 09:41:12 字数 2280 浏览 0 评论 0原文

更新于 14/04/2011

仍然有麻烦。我将代码简化为最简单的形式。我使用 IF 函数来检查 isset() 中的复选框,效果很好。如果选中该复选框,它将连接一个由两部分组成的字符串。很简单。

if (isset($_POST[testType1])) {
   $filterQuery .= "(testType1 = '1'"; 
   }
   $filterQuery .= ") ";
}

当我使用 mysql_fetch_assoc 并回显 $rows 中的信息时,它可以工作。但是当我在 Google Chrome 中查看页面源时,它显示: <代码> 无效查询:您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在第 1 行 '' 附近使用的正确语法;

如果我 echo $filterQuery 它会正确显示,并且当我将回显的字符串复制到代码中时,MySQL 将返回正确的结果:

SELECT * FROM fdatav1 JOIN ddatav1 ON ddatav1.ID = fdatav1.ID WHERE (testType1 = '1')< /code>

我也尝试将 $filterQuery 转换为字符串。没有成功。

更新于 12/04/2011

我仍然有一个问题,这不是一个拼写错误。请参阅下面的代码:

$query = "SELECT * FROM fdatav1 JOIN ddatav1 ON ddatav1.ID = fdatav1.ID WHERE ";`
$orTrigger = "";`

function setOrTrigger() {
    global $orTrigger;
    if ($orTrigger=="") {
        $orTrigger="OR ";
    }
}

function getTestFilterQuery($testType) {
   if (!(isset($_POST[test1])) && !(isset($_POST[test2])) && !(isset($_POST[test3]))) {
     $returnString = NULL;
     return $returnString;
   }

}

if (isset($_POST[testType1])) {
   $filterQuery .= $orTrigger ."(testType1 = '1'"; 
   setOrTrigger();
   $addTestFilterQuery = getTestFilterQuery("testType1");
   if ($addTestFilterQuery != NULL) {
    $filterQuery .= "AND " .$addTestFilterQuery;
   }
   $filterQuery .= ") ";
}

$connection = mysql_connect(localhost, $username, $password);

if (!$connection) {
   die('Not connected : ' . mysql_error());

}

$db_selected = mysql_select_db($database, $connection);

if (!$db_selected) {
   die ('Can\'t use db : ' . mysql_error());
}

$result = mysql_query($filterQuery);

if (!$result) {
   die('Invalid query: ' . mysql_error());
}

while ($row = @mysql_fetch_assoc($result)) {
   echo $row['name'];
   echo $row['description'];
}

当我 echo $query 时,我得到:

SELECT * FROM fdatav1 JOIN ddatav1 ON ddatav1.ID = fdatav1.ID WHERE (testType1 = '1')

当我将其直接复制到 mysql_query 时,如下所示:

mysql_query("SELECT * FROM fdatav1 JOIN ddatav1 ON ddatav1.ID = fdatav1.ID WHERE (testType1 = '1')");

效果很好。但是当我传递如下变量时:

mysql_query($filterQuery);

我在“”附近遇到语法错误。有谁知道如何解决这个问题?

UPDATED 14/04/2011

Still in trouble. I reduced my code to its simplest form. I use the IF function to check isset() for a checkbox, which works fine. If the checkbox is checked it concatenates a string made of two parts. Very simple.

if (isset($_POST[testType1])) {
   $filterQuery .= "(testType1 = '1'"; 
   }
   $filterQuery .= ") ";
}

When I use mysql_fetch_assoc and echo the info in the $rows it works. But when I view the page source it in Google Chrome it says:

Invalid query: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
;

If I echo $filterQuery it displays correctly and when I copy the echoed string into my code MySQL returns the correct results:

SELECT * FROM fdatav1 JOIN ddatav1 ON ddatav1.ID = fdatav1.ID WHERE (testType1 = '1')

I have tried casting $filterQuery to a string as well. No success.

UPDATED 12/04/2011

I still have a problem, it wasn't a typo. See code below:

$query = "SELECT * FROM fdatav1 JOIN ddatav1 ON ddatav1.ID = fdatav1.ID WHERE ";`
$orTrigger = "";`

function setOrTrigger() {
    global $orTrigger;
    if ($orTrigger=="") {
        $orTrigger="OR ";
    }
}

function getTestFilterQuery($testType) {
   if (!(isset($_POST[test1])) && !(isset($_POST[test2])) && !(isset($_POST[test3]))) {
     $returnString = NULL;
     return $returnString;
   }

}

if (isset($_POST[testType1])) {
   $filterQuery .= $orTrigger ."(testType1 = '1'"; 
   setOrTrigger();
   $addTestFilterQuery = getTestFilterQuery("testType1");
   if ($addTestFilterQuery != NULL) {
    $filterQuery .= "AND " .$addTestFilterQuery;
   }
   $filterQuery .= ") ";
}

$connection = mysql_connect(localhost, $username, $password);

if (!$connection) {
   die('Not connected : ' . mysql_error());

}

$db_selected = mysql_select_db($database, $connection);

if (!$db_selected) {
   die ('Can\'t use db : ' . mysql_error());
}

$result = mysql_query($filterQuery);

if (!$result) {
   die('Invalid query: ' . mysql_error());
}

while ($row = @mysql_fetch_assoc($result)) {
   echo $row['name'];
   echo $row['description'];
}

When I echo $query I get:

SELECT * FROM fdatav1 JOIN ddatav1 ON ddatav1.ID = fdatav1.ID WHERE (testType1 = '1')

When I copy this directly into mysql_query like:

mysql_query("SELECT * FROM fdatav1 JOIN ddatav1 ON ddatav1.ID = fdatav1.ID WHERE (testType1 = '1')");

it works fine. But when I pass the variable like:

mysql_query($filterQuery);

i get a syntax error one near ''. Does anyone know how to resolve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

芯好空 2024-11-07 09:41:12

您是否真的在字符串中添加了双引号,例如:

$query = '"SELECT * FROM table WHERE col = value"';
echo $query; //output is exactly: "SELECT * FROM table WHERE col = value"

如果是这样,您需要从字符串、mysql_query 或任何采用普通字符串的内容中删除 ",例如:

$query = "SELECT * FROM table WHERE col = value";
echo $query; //output is exactly: SELECT * FROM table WHERE col = value

以同样的方式,您不会在字符串中使用 ; 结束 SQL 查询,例如: $query = "SELECT * FROM table WHERE col = value;";

Are you actually putting the double quotes in the string, like:

$query = '"SELECT * FROM table WHERE col = value"';
echo $query; //output is exactly: "SELECT * FROM table WHERE col = value"

If so, you need to remove the "s from inside the string, mysql_query or whatever takes a normal string like:

$query = "SELECT * FROM table WHERE col = value";
echo $query; //output is exactly: SELECT * FROM table WHERE col = value

In much the same way, you don't end an SQL query with ; in the string like: $query = "SELECT * FROM table WHERE col = value;";

一紙繁鸢 2024-11-07 09:41:12

concat 查询示例

$query = "select * from tablename ";
$query.= "WHERE id=1";
$query.= " and item_id= 'foo'";

example of concat for query

$query = "select * from tablename ";
$query.= "WHERE id=1";
$query.= " and item_id= 'foo'";
别忘他 2024-11-07 09:41:12

尝试将 else 块添加到这部分代码中,如下所示。否则 $filterQuery 永远不会被设置,除非 isset($_POST[testType1])。

if (isset($_POST[testType1])) {
   $filterQuery .= $orTrigger ."(testType1 = '1'"; 
   setOrTrigger();
   $addTestFilterQuery = getTestFilterQuery("testType1");
   if ($addTestFilterQuery != NULL) {
    $filterQuery .= "AND " .$addTestFilterQuery;
   }
   $filterQuery .= ") ";
} else {
   $filterQuery = $query;
}

try adding an else block to this bit of the code, as per below. otherwise $filterQuery will never get set unless isset($_POST[testType1]).

if (isset($_POST[testType1])) {
   $filterQuery .= $orTrigger ."(testType1 = '1'"; 
   setOrTrigger();
   $addTestFilterQuery = getTestFilterQuery("testType1");
   if ($addTestFilterQuery != NULL) {
    $filterQuery .= "AND " .$addTestFilterQuery;
   }
   $filterQuery .= ") ";
} else {
   $filterQuery = $query;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文