[SOLVED] Query Not Executing Correctly

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Harlequin
Forum Commoner
Posts: 51
Joined: Tue Sep 21, 2004 10:51 am
Location: UK

Query Not Executing Correctly

Post by Harlequin »

I have the following query which returns a "Query Empty" error:

Code: Select all

$Query = mysql_query("SELECT * FROM events
   WHERE EventCode == 'PMJ';
   ORDER BY EventTime DESC");
   $Data = mysql_query($Query) or die("Error: " . mysql_error());
   while($row = mysql_fetch_array($Query))
Can anyone see what I can't...? I've checked and double checked and it seems OK and there's definately data in the tale.
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

echo

Post by phpScott »

echo out the query and run it against the db directly

but I think it is the == try just using a single =
mysql isn't the same a php for checking if an item is equal.
Harlequin
Forum Commoner
Posts: 51
Joined: Tue Sep 21, 2004 10:51 am
Location: UK

Post by Harlequin »

Well, this is just driving me mad...

This works when I execute it against the database:

Code: Select all

SELECT * FROM events
WHERE EventCode = 'PMJ';
But this, in the PHP code doesn't:

Code: Select all

$Query = mysql_query "SELECT * FROM events
WHERE EventCode = 'PMJ'";
I get "Unexpected T_STRING


Grrr...
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

[php_man]types.string[/php_man]

notice that your syntax is wrong.
Harlequin
Forum Commoner
Posts: 51
Joined: Tue Sep 21, 2004 10:51 am
Location: UK

Post by Harlequin »

I know mate but I can't seem to get it right...!

I'll keep plugging away though...
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

two different codes

Post by phpScott »

OK you now have to different sets of codes
the second set
$Query = mysql_query "SELECT * FROM events
WHERE EventCode = 'PMJ'";
will not work because you need the () around the select statement.
try

Code: Select all

$Query = mysql_query ("SELECT * FROM events
WHERE EventCode = 'PMJ'");
Harlequin
Forum Commoner
Posts: 51
Joined: Tue Sep 21, 2004 10:51 am
Location: UK

Post by Harlequin »

I tried that Scott. Now I get a syntax error returned so I must have something screwed up later in the query where I am trying to present the results.

Thanks very much for the tip mate.
Harlequin
Forum Commoner
Posts: 51
Joined: Tue Sep 21, 2004 10:51 am
Location: UK

Post by Harlequin »

Scott.

I know this is basic stuff but I just can't sem to get it working properly:

I Execute the query:

Code: Select all

// Select Events For PMJ:
   $Query = mysql_query ("SELECT * FROM events 
   WHERE EventCode = 'PMJ'");
Then I get the error (SQL Syntax":

Code: Select all

$Data = mysql_query($Query) or die("Error: " . mysql_error());
   while($row = mysql_fetch_array($Query))
   {
// Pull Strings From Table:
   $EventDate=$rowї"EventDate"];
   $EventDateBroken = explode("-", $EventDate);
What's causing the error...? Can you see it...?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

<?php
   $Data = mysql_query($Query) or die("Error: " . mysql_error()); 
   while($row = mysql_fetch_array($Query)) 
   { 
// Pull Strings From Table: 
   $EventDate=$row["EventDate"]; 
   $EventDateBroken = explode("-", $EventDate); 
?>
should b

Code: Select all

<?php
   $Data = mysql_query($Query) or die("Error: " . mysql_error()); 
   while($row = mysql_fetch_array($Data)) 
   { 
// Pull Strings From Table: 
   $EventDate=$row["EventDate"]; 
   $EventDateBroken = explode("-", $EventDate); 
?>
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

just say it

Post by phpScott »

you are doing

Code: Select all

$Query = mysql_query("SELECT * FROM events
   WHERE EventCode == 'PMJ';
   ORDER BY EventTime DESC");
   $Data = mysql_query($Query) or die("Error: " . mysql_error());
   while($row = mysql_fetch_array($Query))
what you are doing is creating the $Query with mysql_query() in it.
then with $DATA you are agian using mysql_query().

try

Code: Select all

$Query = "SELECT * FROM events
   WHERE EventCode == 'PMJ';
   ORDER BY EventTime DESC";
   $Data = mysql_query($Query) or die("Error: " . mysql_error());
   while($row = mysql_fetch_array($Data))
$query now only holds the text for the query, it then gets submitted to mysql_query();
$Data will now hold your result set.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

// Select Events For PMJ:
   $Query = "SELECT * FROM events 
   WHERE EventCode = 'PMJ'";
and then

Code: Select all

<?php
   $Data = mysql_query($Query) or die("Error: " . mysql_error()); 
   while($row = mysql_fetch_array($Query)) 
   { 
// Pull Strings From Table: 
   $EventDate=$row["EventDate"]; 
   $EventDateBroken = explode("-", $EventDate); 
?>
should b

Code: Select all

<?php
   $Data = mysql_query($Query) or die("Error: " . mysql_error()); 
   while($row = mysql_fetch_array($Data)) 
   { 
// Pull Strings From Table: 
   $EventDate=$row["EventDate"]; 
   $EventDateBroken = explode("-", $EventDate); 
?>
Harlequin
Forum Commoner
Posts: 51
Joined: Tue Sep 21, 2004 10:51 am
Location: UK

Post by Harlequin »

Worked a treat 8)

Now all i need to do is get the tables layed out correctly. For some reason when I insert a table below the code for the last table it's header text appears above the last table. 8O
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

good luck

Post by phpScott »

good luck
view source of the web page is your friend.
Harlequin
Forum Commoner
Posts: 51
Joined: Tue Sep 21, 2004 10:51 am
Location: UK

Post by Harlequin »

AHA...!

I forgot to close the <table> TAG 8)

Thanks once again...!
Post Reply