Hey I am trying to run this in php:

// Retrieve data from table where row that match this passkey
$sql1="SELECT * FROM $tbl_name1 WHERE confirm_code ='$passkey'";
$result1=mysql_query($sql1) or die(mysql_error())

and I get this error:

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

Any ideas on what I am doing wrong?

Dani AI

Generated

The MySQL message "You have an error in your SQL syntax … near '' at line 1" means the SQL string you actually sent to the server is malformed or empty. Common causes are an empty/incorrect table name variable, an unescaped quote inside a value that breaks the literal, concatenation that drops a quote or dot, or a prior PHP/SQL statement left unterminated. Check the basic PHP syntax first (as pointed out a missing semicolon can stop execution) and then inspect the exact SQL your script built (as recommended).

Quick steps to debug

  • Dump the generated SQL exactly (use var_export, var_dump or error_log) so you can copy/paste it into the MySQL client and see the real error. Don’t rely on browser HTML — whitespace and control chars can hide problems.
  • Verify that the variables used to build the query are non-empty and don’t contain stray quotes or newlines.
  • If you build multi-statement SQL strings, ensure every statement is properly closed; an unterminated string will make the next statement appear at the reported error line.

Use parameterized queries to avoid quoting problems and injection. For example, with mysqli prepare/bind (pseudo-example):

$stmt = $mysqli->prepare("SELECT * FROM `your_table` WHERE confirm_code = ?");
$stmt->bind_param('s', $passkey);
$stmt->execute();
$result = $stmt->get_result();

For the INSERT error mentioned by /: check that the number of VALUES matches the table columns (or explicitly list the target columns), that any single quotes inside string values are escaped or bound, and that no column name is a reserved word. ’s suggestion to explicitly name columns is a good practice. Finally, consider migrating off the old mysql_* API to mysqli or PDO (prepared statements) to make these errors much easier to diagnose and to avoid security pitfalls.

Recommended Answers

All 6 Replies

You've missed out on a semicolon after 'die'.
do this:
.... or die(mysql_error());
This will not give an error

hello try this may works:

$sql1="SELECT * FROM $tbl_name1 WHERE confirm_code =".$passkey;
$result1=mysql_query($sql1) or die(mysql_error());

You need to know whether the generated query is a valid or not. For this, print the query e.g.

$sql1="SELECT * FROM $tbl_name1 WHERE confirm_code ='$passkey'";
echo $sql1."<br>";
$result1=mysql_query($sql1) or die(mysql_error());

Post back the query you get.

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 'INSERT INTO raf_emails VALUES(1, 'Email Verification Link', 'You or someone else' at line 12.


Any ideas on what to do?

Its for a raffle system.

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 'INSERT INTO raf_emails VALUES(1, 'Email Verification Link', 'You or someone else' at line 12.


Any ideas on what to do?

Its for a raffle system.

I am having the same problem. Any help will be appreciated

Hi first check that your PHP variables are passing values to your query...

Try your code like this

// Retrieve data from table where row that match this passkey
echo $sql1="SELECT * FROM $tbl_name1 WHERE confirm_code ='".$passkey."'";
$result1=mysql_query($sql1) or die(mysql_error());

In the above query, if all the values are displaying, there is nothing wrong in your query...

Same case in your insert statements,
If your table contains more than 3 columns and you are passing only 3 values to your insert statement, then it will results to error.

so in order to avoid confusion for your insert query

try like this

INSERT INTO raf_emails (column1, column2, column3) VALUES (1, 'Email Verification Link','column3_value')

Hey I am trying to run this in php:

// Retrieve data from table where row that match this passkey
$sql1="SELECT * FROM $tbl_name1 WHERE confirm_code ='$passkey'";
$result1=mysql_query($sql1) or die(mysql_error())

and I get this error:

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

Any ideas on what I am doing wrong?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.