Jump to content

Query won't Execute


Asheeown

Recommended Posts

My query in PHP:
[code]$UserLogs = mysql_query("SELECT Originating_TG WHERE Originating_TG IN ('".$_SESSION['CDRId']."'), UTCTime > '$StartDate' AND UTCTime < '$EndDate' ORDER BY '$Field' '$Order'") or die(mysql_error());[/code]

And the error I'm receiving:
[quote]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 'WHERE Originating_TG IN ('000500,000501') AND UTCTime > '2006-10-13 20:40:00' AN' at line 1[/quote]
Link to comment
https://forums.phpfreaks.com/topic/35475-query-wont-execute/
Share on other sites

You have a comma after the first case following the WHERE clause - it should be an AND. Also, there could be a problem with some of the variables - but you won't know unless you echo the query to the page when there is an error.

I always create a string for my query so that I can echo it to the page if there is a problem. I suggest you do the following:
[code]<?php
$query = "SELECT Originating_TG
          WHERE Originating_TG IN ('".$_SESSION['CDRId']."')
            AND UTCTime > '$StartDate'
            AND UTCTime < '$EndDate'
          ORDER BY '$Field' '$Order'";

$UserLogs = mysql_query($query) or die("The Query:<br>".$query."<br>Produced the error:<br>".mysql_error());
?>[/code]

EDIT: Perhaps the comma is interpreted as AND - because in the error message it is an AND but it is a comma in your query. Odd.

EDIT #2: OK, After a 2nd look I think the problem is the first WHERE condition. The values in the IN list need to have quotes around EACH list value [b]or[/b] if it is for a numeric field, then you do not need quotes at all.

From the manual ( http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html ):

    mysql> SELECT 2 IN (0,3,5,7);
    mysql> SELECT 'wefwf' IN ('wee','wefwf','weg');
Link to comment
https://forums.phpfreaks.com/topic/35475-query-wont-execute/#findComment-167877
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.