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

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();
...
?>

Related

get value of mysql colum with numbers in name [duplicate]

This question already has answers here:
How can I access an object attribute that starts with a number?
(3 answers)
Closed 2 years ago.
I would like to get a value of my mysql table.
the column name has got numbers.
$temp= array();
$sql = "SELECT `00003A` FROM `table`";
$statement = $mysqli->prepare($sql);
$statement->execute();
$result = $statement->get_result();
while($row = $result->fetch_object()) {
temp[] = $row->00003A;
}
I get this error:
Parse error: syntax error, unexpected '00003' (T_LNUMBER), expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$' on line 19
This is line 19:
temp[] = $row->00003A;
You can try save this value in a variable like:
$prop = '00003A';
temp[] = $row->$prop;
Or get data with
temp[] = $row['00003A'];

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";

How to fix "Unexpected T_ENCAPSED_AND_WHITESPACE" error in PHP? [duplicate]

This question already has an answer here:
Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE) [duplicate]
(1 answer)
Closed 8 years ago.
Here is the error:
Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in C:\xampp\htdocs\new-training-site\config\info.php on line 4;
Here is line 4:
$link = mysql_query("SELECT * FROM users WHERE id='$_SESSION['MM_Username']' AND password='$_SESSION['MM_PASSWORD']'");
$link = mysql_query("SELECT * FROM users WHERE id='{$_SESSION['MM_Username']}' AND password='{$_SESSION['MM_PASSWORD']}'");
Try this one
$link = mysql_query(sprintf(
"SELECT * FROM users WHERE id='%s' AND password='%s'",
mysql_real_escape_string($_SESSION['MM_Username']),
mysql_real_escape_string($_SESSION['MM_PASSWORD']),
));
It will help you to avoid sql-injection. And please don't use mysql_ functions as it were mentioned in comments

Parse error: syntax error in mysqli_fetch_array() [duplicate]

This question already has an answer here:
Difference in accessing arrays in PHP 5.3 and 5.4 or some configuration mismatch?
(1 answer)
Closed 8 years ago.
Parse error: syntax error, unexpected '[', expecting ',' or ';' in /home/u844411171/public_html/index.php on line 8
I have a problem with mysqli_fetch_array(). I use mysqli_fetch_array():
if (isset($_SESSION["user_id"])) {
$query = "SELECT typ FROM `uzivatel` WHERE id = " . $_SESSION["user_id"];
$uzivatel = $mysqli->query($query);
$prava = mysqli_fetch_array($uzivatel)[0];
}
and
$celkemClenu = "SELECT count(`id`) FROM
`uzivatel`" or die("Error in the consult.." . mysqli_error($mysqli));
$result = $mysqli->query($celkemClenu);
echo "<p>Celkem členů: " . mysqli_fetch_array($result)[0];
Plz, where is a problem?
Your PHP is older than 5.4, which is when direct array dereferencing of function returns was added:
echo foo()[0]; // ok in 5.4, syntax error in older versions
Workaround, use a temporary variable to store the array:
$temp = foo();
echo $temp[0];

How to get data from database and echo on php page? [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 3 years ago.
How to get the date from database and echo on PHP page?
$query = $pdo->prepare('SELECT * FROM shop WHERE shopname=:shopname');
$query->bindParam(':shopname', $shopname, PDO::PARAM_STR);
$query->execute();
$result = $query->fetchAll(PDO::FETCH_ASSOC);
echo "$result['shopid']";
This gives me the following error:
Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE),
expecting identifier (T_STRING) or variable (T_VARIABLE) or number
(T_NUM_STRING)
echo "$result['shopid']";
This line is incorrect
echo $result["shopid"];
// OR
echo "{$result['shopid']}";
remove the double quotes, change
echo "$result['shopid']";
with
echo $result['shopid'];

Categories