Error message:

Query failed: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

--------------------------------------
Can any one able to edit it in correct form?(*I using PHP5)
----------------------------------------------


$

query="select count(*) from cart where cookieId ='GetCartId()'
and bookid = '$bookid'";

Dani AI

Generated

The MySQL error “... near ')' at line 1” means the SQL string you actually sent to the server is malformed (unbalanced parentheses, missing token or stray characters). was right to suggest inspecting the final SQL, and both and were looking at the right places — the real fixes are: (1) execute any PHP function before you build the SQL string (functions written inside quotes are treated as literal text), and (2) make sure your concatenation/spacing produces valid SQL (a missing space before AND will break the statement).

Checklist to debug quickly

  • Show the exact SQL the app sent (print or log the string and examine quotes, parentheses and spaces).
  • Verify each variable is set and not empty (empty values or an empty IN() list commonly produce a syntax error).
  • Check the DB error output (mysqli_error or PDOException) for the precise parse point.
  • Look for invisible characters (BOM, stray newlines) and accidental missing spaces around SQL keywords.

Safer, maintainable approach
Use parameterized queries so data and SQL are separated. This avoids quoting mistakes and SQL injection. Example (PDO):

$cookieVal = get_cart_id();   // call your cart-id function first
$bookVal   = (int) $bookId;   // cast if numeric

$stmt = $pdo->prepare('SELECT COUNT(*) FROM cart WHERE cookieId = :cookie AND bookid = :book');
$stmt->execute([':cookie' => $cookieVal, ':book' => $bookVal]);
$count = (int) $stmt->fetchColumn();

Extra notes

  • If you only need to know existence, SELECT EXISTS or fetching a single column is lighter than pulling full rows.
  • Be careful with whitespace when building strings (this subtlety explains why ’s idea was correct in principle but his example could produce a syntax error if a space is missing).
  • Move away from manual string-building for production code—prepared statements are more robust and make debugging far simpler.

Type the following after you declare the $query variable:

echo $query;

You should then be able to debug your query. If not, post what you get here.

Error message:

Query failed: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

--------------------------------------
Can any one able to edit it in correct form?(*I using PHP5)
----------------------------------------------


$

query="select count(*) from cart where cookieId ='GetCartId()'
and bookid = '$bookid'";

Hello cty ,

According to your SQL statement that is embeded in your PHP code, there is a very simple mistake which is : take a look at your SQL statement , the second condition in the WHERE Clause which is -
bookid = '$bookid'; - there has to be two dots , one before the $ sign in the variable and one after the variable , also there has to be double quotes before the single quotes, so it should look like this :
bookid = "'.$bookid.'";

So your query will be like this :

query="select count(*) from cart where cookieId ='GetCartId()'
and bookid = "'.$bookid.'" ";

So, please try it and see if it works , and please reply me whether it worked or not , because this was from my experience using PHP With MySQL ..

Hope it works well .. ;)
looking forward for your reply ,

Best regards
Joseph.G.Hodali

Sorry joseph.hodali you are wrong. The bookid part is ok, except from the fact that he needs to user {$bookid} when using variables inside double quotes. The {} define the beginning and end of a varibale inside double quotes. This makes it possible to do

$string="{$array['id']} bla bla.";

The problem is the function GetCartId(), if its an sql function you don't use the single quotes, but I bet its an php function and you can not include a function inside a string. Try this it should function better.

$cartId = GetCartId();
$query="select count(*) from cart where cookieId ='{$cartId}'and bookid = '{$bookid}'";
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.