开发者

Variable concatenated mysql query string runs fine in phpMyAdmin but not PHP in script [duplicate]

This question already has answers here: Problem with $_POST, if isset() and mysql_fetch_assoc(): echo returns correct value, page source always returns else value (5 answers) Closed 2 years ago.

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 dda开发者_如何转开发tav1 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?


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;";


example of concat for query

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


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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜