变量连接的 mysql 查询字符串在 phpMyAdmin 中运行良好,但在脚本中运行 PHP 则不行
更新于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否真的在字符串中添加了双引号,例如:
如果是这样,您需要从字符串、mysql_query 或任何采用普通字符串的内容中删除
"
,例如:以同样的方式,您不会在字符串中使用
;
结束 SQL 查询,例如:$query = "SELECT * FROM table WHERE col = value;";
Are you actually putting the double quotes in the string, like:
If so, you need to remove the
"
s from inside the string, mysql_query or whatever takes a normal string like:In much the same way, you don't end an SQL query with
;
in the string like:$query = "SELECT * FROM table WHERE col = value;";
concat 查询示例
example of concat for query
尝试将 else 块添加到这部分代码中,如下所示。否则 $filterQuery 永远不会被设置,除非 isset($_POST[testType1])。
try adding an else block to this bit of the code, as per below. otherwise $filterQuery will never get set unless isset($_POST[testType1]).