473,394 Members | 1,674 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

mysql prepare/execute woes

I have a problem with some SQL code that can be run at the mySQL
command prompt, and in PHP, I can prepare it with no problem, but I
get a syntax error when I try and exexute it.

$sql = <<< EOF
SELECT * from ad where id = ? and
((endDate is null) or (endDate is not null and endDate >=
now()))";
EOF;

$query = $dbh->prepare($sql);
if (DB::iserror($query)) {
echo "Query error, " . $query->getMessage() . " please contact
admin<br>";
exit();
}
# works, no compaints, but a few steps later

foreach ($controls as $c) {
$e = $dbh->execute($query, $c);
if (DB::iserror($e)) {
echo "Execute error" . $e->getMessage() . " please contact
admin.<br>";
exit();
}
$e = $e->fetchRow(DB_FETCHMODE_ASSOC);
}

I get this in my browser:

Execute error DB Error: syntax error please contact admin.

Now, if the sql was broken, I could understand the error, but I would
kind of have expected the prepare to fail.

This works OK if I leave off everything after id = $.

Any ideas?

Thanks in advance,
Randy
Jul 17 '05 #1
3 3161
"Randy Rodent" <ch*************@yahoo.com> wrote in message
news:f6**************************@posting.google.c om...
I have a problem with some SQL code that can be run at the mySQL
command prompt, and in PHP, I can prepare it with no problem, but I
get a syntax error when I try and exexute it.

$sql = <<< EOF
SELECT * from ad where id = ? and
((endDate is null) or (endDate is not null and endDate >=
now()))";
EOF;


You are using heredoc syntax, the "; is not needed.
Jul 17 '05 #2
CJ Llewellyn wrote:
"Randy Rodent" <ch*************@yahoo.com> wrote in message
news:f6**************************@posting.google.c om...
I have a problem with some SQL code that can be run at the mySQL
command prompt, and in PHP, I can prepare it with no problem, but I
get a syntax error when I try and exexute it.

$sql = <<< EOF
SELECT * from ad where id = ? and
((endDate is null) or (endDate is not null and endDate >=
now()))";
EOF;


You are using heredoc syntax, the "; is not needed.


If you want to see what query it actually tried to run then do the
following:

print $dbh->last_query;

Really useful for seeing what you've done wrong. I'm sure if you'd printed
it out in this way you probably would have caught that error straight away.

--
Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/
Jul 17 '05 #3
On 3 Aug 2004 22:42:28 -0700, ch*************@yahoo.com (Randy Rodent) wrote:
I have a problem with some SQL code that can be run at the mySQL
command prompt, and in PHP, I can prepare it with no problem, but I
get a syntax error when I try and exexute it.

I get this in my browser:

Execute error DB Error: syntax error please contact admin.

Now, if the sql was broken, I could understand the error, but I would
kind of have expected the prepare to fail.


Others have addressed the problem with the SQL, however the reason why it
doesn't fail on prepare is that MySQL doesn't support prepared statements until
4.1, and only then through the mysqli extension.

For databases that don't support prepared statements, DB::prepare only sets
things up on the client side, and it's not until execute that it actually hits
the server.

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
(v1.4.0 new 1st Aug 2004)
Jul 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Mike Chirico | last post by:
Interesting Things to Know about MySQL Mike Chirico (mchirico@users.sourceforge.net) Copyright (GPU Free Documentation License) 2004 Last Updated: Mon Jun 7 10:37:28 EDT 2004 The latest...
1
by: Cern | last post by:
Is it somebody out there who has made a migration from an Oracle server to an MySQL server?? The scenario is as simply: I've got a Oracle 8 server with a database with content that I want to...
2
by: James | last post by:
Hey everyone... I'm having an issue with a seemingly simple piece of PHP/MySQL I have a stored procedure in MySQL5 as such: SQL: -------------- DELIMITER $$; DROP PROCEDURE IF EXISTS...
3
by: howachen | last post by:
Hi, When doing mysql query (SELECT statements) in php, we often use prepare statement to prevent SQL injection. However, I just noticed that the prepare statements can SLOW the number of queries...
1
by: Akino877 | last post by:
Hello, I have a small Perl program : #!/usr/bin/perl -w use DBI; use Mysql; $host = ""; $database = ""; $user = "root";
2
fungazid
by: fungazid | last post by:
Help help help please I’m using DBD::mysql, and I want to insert a record into clients table (id, address, and phone-number of a client): my $str= “?,?,?,,,”; my...
2
by: smithji | last post by:
Hi All, I'm having a problem executing a MySQL query witihin Perl and was hoping that someone will be able to help me in my moment of stress. When I run the below script I get this...
1
ssnaik84
by: ssnaik84 | last post by:
Hi Guys, Last year I got a chance to work with R&D team, which was working on DB scripts conversion.. Though there is migration tool available, it converts only tables and constraints.. Rest of...
17
dbrewerton
by: dbrewerton | last post by:
Hello all. This script I have did work on linux/apache and my service provider has told me the cgi-handler has been properly set up. I am able to get hello world test to display to a web page. My...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.