Jump to content

Recommended Posts

I get this error "Error preforming 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 ''1''' at line 1"

 

whith the script below using sql 5.1.41 yet it works fine using sql 4.0.24

 

<?php


include("includes/configure.inc");

//******* [ START: Database Connection ] **********

$connection = mysql_connect($host, $user, $password)
or die('<p>Unable to connect to the database at this time.</p>' );

//Select Daniels Data Base
$db = mysql_select_db($database)
or die('<p>Unable to locate the Daniels' . 'database at this time.</p>' );

//******* [ END: Database Connection ] ************



$result = mysql_query("SELECT ID, DATE_FORMAT(d_date, '%a %b %D') as d_date, first_name, last_name, nee, obituary FROM obituaries WHERE ID='$obitid'");

if (!$result) {
die('<p>Error preforming query: ' . 
mysql_error() . '</p>');
}

$x=0;

while ( $x < mysql_numrows($result)):
        //Gather Variable Data from $result array
        
        $id = mysql_result($result, $x, 'id');
        $d_date = mysql_result($result, $x, 'd_date');
        $first_name = mysql_result($result, $x, 'first_name');
        $last_name = mysql_result($result, $x, 'last_name');
        $nee = mysql_result($result, $x, 'nee');
	  $obituary = mysql_result($result, $x, 'obituary');



// Request the text of obituaries
{
$obituary = htmlspecialchars($obituary);
$obituary = ereg_replace("\r",'',$obituary);
$obituary = ereg_replace("\n\n",'</p><p>',$obituary);
$obituary = ereg_replace("\n",'<br />',$obituary);
$obituary = eregi_replace('\[b]','<b>',$obituary);
$obituary = eregi_replace('\[eb]','</b>',$obituary);
$obituary = eregi_replace('\[i]','<em>',$obituary);
$obituary = eregi_replace('\[ei]','</em>',$obituary);



echo "<br><br>test<b>$first_name, $last_name</b>";
echo "<br>nee ($nee)";
echo "<br>$d_date";
echo "<br>$obituary<br>"; 
}
$x++;
endwhile;
?>   

Link to comment
https://forums.phpfreaks.com/topic/251135-syntax-error-in-sql-5141/
Share on other sites

The error's in line 1 of the sql statement.

 

I would form the query statement in a php variable (and use that variable in the mysql_query() statement) and then echo that variable so that you can see exactly what the query statement is.

 

You most likely have some extra characters in the $obitid value that is breaking the sql syntax. Where is $obitid being set at in the code?

Agreed, obitid is probably the problem.

 

Like what was stated above, print out your fully-formed query and look at it to find the error.  There's probably an apostrophe in your obitId

 

Also, sunfighter's tip is wrong.  MySQL and PHP are separate languages.  The error you were getting was coming from the MySQL server, which ONLY knows about the single-line query you sent it. 

 

-Dan

Thanks!!

yes it was an apostrophe in the obitId

Which tells me:

1)  Your IDs are not numeric.  You should fix that

2)  You're not passing strings through mysql_real_escape_string, which would have solved this problem entirely.

If they're smallint and auto-increment:

1)  Why are you quoting them, they're numbers.

2)  What happens when you overload the smallint datatype?  It will run out at 32767 rows.

3)  If you're expecting a number, how did you get an apostrophe?

If they're smallint and auto-increment:

1)  Why are you quoting them, they're numbers.

2)  What happens when you overload the smallint datatype?  It will run out at 32767 rows.

3)  If you're expecting a number, how did you get an apostrophe?

 

Sorry Dan, you're talking above my head, just learning sql here.

1) What do you mean by quoting the numbers?

2) don't think we'll need more id's than that in the next 20 years

3) my mistake it was a single quote not an apostrophe and it came from the previous page <a href=obituary.php?first=no&lastName=1&last_name_ID=$obitid' target='blank'> and picked up on the new page <?php  $obitid = $_GET[last_name_ID]; ?>

 

John

 

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.