syntax error, unexpected 'order' (T_STRING) [duplicate] - php

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 7 years ago.
I put this code in my php file and it gives me the error syntax error, unexpected 'order' (T_STRING)
What am I doing wrong?
$sqldelreq="DELETE FROM `requests` WHERE tablecode = 1 and type = "order"";
$result2=mysql_query($sqldelreq);
if($result2)
{
header("Location: http://localhost/mjjapp/index.php");
}

I think the query should be:
"DELETE FROM `requests` WHERE tablecode = 1 and type = 'order'";
Please note the single quotes around order.

You should do proper escape
$sqldelreq = "DELETE FROM `requests` WHERE `tablecode` = 1 and `type` = 'order'";
Also your if is invalid. It only means that query was successful if you wan't to check if any rows were deleted you need to check how many rows were affected with mysql_affected_rows() function.
Moreover, consider using mysqli or pdo. Mysql_* functions are deprecated.
https://dev.mysql.com/doc/refman/5.6/en/keywords.html
https://dev.mysql.com/doc/refman/5.0/en/string-literals.html

Correct syntax usually goes a long way; try:
$sqldelreq = "DELETE FROM `requests` WHERE `tablecode` = 1 AND `type` = 'order';";

i found this running well thanks guys for ideas
$sqldelreq="DELETE FROM requests WHERE tablecode = 1 and type = 'order';";

Related

SELECT execute([]) Parse error: syntax error, unexpected '[', expecting ')' in [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 3 years ago.
I'm adding [] this symbol within an execute() in PDO, and it returns error.
I'm working in WAMP5
$sqlStatement="
SELECT *
FROM $table_results
WHERE title = ? AND id_category = ?
";
$stmt = $bdConection->prepare($sqlStatement);
$stmt->execute([$title, $id_category]);
echo $stmt->rowCount();
It works well if I delete WHERE title = ? AND id_category = ? and [$title, $id_category] bur returns more results, instead of adding those ... then
IT RETURNS:
Parse error: syntax error, unexpected '[', expecting ')' in
I can reproduce this error with PHP 5.2. Documentation about PHP array's syntax states:
... As of PHP 5.4 you can also use the short array syntax, which
replaces array() with []. ...
What you can do in your case is to define an array using array():
<?php
$sqlStatement = "
SELECT *
FROM $table_results
WHERE title = ? AND id_category = ?
";
...
$stmt = $bdConection->prepare($sqlStatement);
$stmt->execute(array($title, $id_category));
echo $stmt->rowCount();
...
?>

PHP MySQL queries with variables [duplicate]

This question already has answers here:
How can I combine two strings together in PHP?
(19 answers)
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 4 years ago.
In my php I make this query
$sql = "SELECT * FROM session WHERE sessionid = '$_SESSION["id"]';";
which results in an error
Parse error: syntax error, unexpected '"', expecting '-' or identifier
(T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in
/opt/lampp/htdocs/Chore-Champs/index.php on line 6
Obviously there is something wrong with how I'm nesting the quotes, so I've tried different ways, including
$sql = "SELECT * FROM session WHERE sessionid = " . $_SESSION['id'] . ";";
this still results in the same error.
Normally the first method would work with normal variables such as $username, but I guess that session variables are handled differently. What's the correct way to write this query?
Try
$sql = "SELECT * FROM session WHERE sessionid = '" . $_SESSION['id'] . "';";
A basic string concatenation in php
try this:
$sql = "SELECT * FROM session WHERE sessionid = '". $show. "'";

codeigniter count_all_results with join table [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 5 years ago.
I have a problem getting the number of rows from my tables.
Here is my mysql query using codeiginiter:
$this->db->where('rd', 1);
$this->db->where('id_user_first', $user_type);
$this->db->from("messaging");
$this->db->join('answers', "answers.id_message = messaging.id AND answers.id_user != '$user_type'");
$count_reponse = $this->db->count_all_results();
I get this error message:
Parse error: syntax error, unexpected '' ''
(T_CONSTANT_ENCAPSED_STRING), expecting ',' or ';' in
You can try this solution for your problem :
Please changes query
$this->db->select('messaging.*, answers.*');
$this->db->from("messaging");
$this->db->join('answers', "answers.id_message = messaging.id");
$this->db->where('answers.id_user <>', $user_type);
$this->db->where('messaging.rd', 1);
$this->db->where('messaging.id_user_first', $user_type);
$count_reponse = $this->db->count_all_results();
You need to escape your quotes or change the inner double-quotes to single quotes.
$this->db->join('answers', 'answers.id_message = messaging.id AND answers.id_user != '.$user_type.'','left');
Try This query:
$this->db->where('rd', 1);
$this->db->where('id_user_first', $user_type);
$this->db->from("messaging");
$this->db->join('answers', 'answers.id_message = messaging.id', 'left');
$this->db->where('answers.id_user !=', $user_type);
$count_reponse = $this->db->count_all_results();
I hope it will help. Please leave a comment if any error came.

T_CONSTANT_ENCAPSED_STRING error. [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 6 years ago.
Hello, I'm very new to PHP and im getting this error...:
Parse error: syntax error, unexpected ''arak''
(T_CONSTANT_ENCAPSED_STRING) in /testSQL.php on line 6
...for this line:
$query = UPDATE 'arak' SET `ara` = '$ar1' Limit 0,1;
A little help would be appriciated :)
You have to quote the string by ", protect the table name by ` and protect value with '
$query = "UPDATE `arak` SET `ara` = '$ar1' Limit 0,1";
Be careful, $ar1 must be protected. For example, if $ar1 = '33\'33', you could have problem.
$ar1 = addslashes($ar1);
$query = "UPDATE `arak` SET `ara` = '$ar1' Limit 0,1";
Addslashes is a first step to prevent SQL Injection, but it is not enough as you can read it

How to assign Array to a String in PHP? [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 7 years ago.
I am trying to assign the contents of a 2D array to a string in PHP.
$sql="SELECT Order_Code FROM Order WHERE CUST_CODE = '$output['username']';";
I know the problem exists in how I'm writing the $output variable assignment.
The following line of code outputs the correct data from the variable:
echo $output['username'];
The following error is being thrown:
Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING)
Parameters surrouned by curly brackets will work well in your case. Here is what i mean. {$array['key']}
And for your example:
$sql="SELECT Order_Code FROM Order WHERE CUST_CODE = '{$output['username']}';";
You need to concatenate this string as the multiple single quotes from accessing the array element are mixing up the string.
$sql="SELECT Order_Code FROM Order WHERE CUST_CODE = '" . $output['username'] ."';";
Try this:
$sql="SELECT Order_Code FROM Order WHERE CUST_CODE = '".$output['username']."'";
You can also try this:
$user_name = $output['username'];
$sql = "SELECT Order_Code FROM Order WHERE CUST_CODE = $user_name";

Categories