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];
Related
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'];
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();
...
?>
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. "'";
This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 7 years ago.
I am new to PHP, I got this syntax error but I can't figure it out
$pageid = $_GET['pageid'];
$sql = "SELECT article_id FROM page_content WHERE id='" .$pageid ."'";
$row = $conn->query($sql);
if ($row != false){
$result = $row->fetch();
$sql = "SELECT html_code from article WHERE id=" .$result['article_id'];
$row = $conn->query(&sql);
if ($row != false){
$result = $row->fetch();
echo $result['html_code'];
}
}
Result: Parse error: syntax error, unexpected ')', expecting '(' in the line 7.
Please help me, thank you.
You are using a & not a $ for the variable so change this:
$row = $conn->query(&sql);
to this:
$row = $conn->query($sql);
This question already has answers here:
5.4 dereferencing to valid 5.3 array call [duplicate]
(2 answers)
Closed 9 years ago.
i keep getting the error Parse error: syntax error, unexpected '['
for some strange reason and i really cant figure out why, heres my skype.class
<?php
class SkypeResolver {
function __construct($db) {
$this->db = new DB($db);
}
function get_ip($username) {
$str = $this->skypeurl();
$str .= $username;
$output = file_get_contents($str);
return $output;
}
function skypeurl() {
return $this->db->fetch_array($this->db->query("SELECT * FROM `settings` WHERE `ident`='skype-api-url'"))['val'];
}
}
?>
ive tried removing the "[" but it then throws another error which i fix which then leads to another error and so on... anyone got any ideas ?
Like somebody said older PHP version. You need to do it the old way.
function skypeurl() {
$array = $this->db->fetch_array($this->db->query("SELECT * FROM `settings` WHERE `ident`='skype-api-url'"));
return $array['val'];
}
You have to use PHP 5.4 or higher to directly get array keys from functions.
This is a workaround working on any version:
function skypeurl() {
$res = $this->db->fetch_array($this->db->query("SELECT * FROM `settings` WHERE `ident`='skype-api-url'"));
return $res['val'];
}
A possible solution:
function skypeurl() {
$array = $this->db->fetch_array($this->db->query(\
"SELECT * FROM `settings` WHERE `ident`='skype-api-url'"));
return $array['val'];
}