This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 5 years ago.
So I am new to php and json and app development. I tried googling for answers but those answers just generate more errors. I am posting an user_id from the app and trying to get all the gyms related to the users. I have had no problem getting response from other php files.
I am trying to get back a 2d array of 3 columns, gym_id, gym_name, and user_id. The code below results in an error of "Parse error: syntax error, unexpected end of file ... on line 25". I am sorry if this is too amateur, but I simply have ran out of ways to proceed.
Thanks in advance for help.
<?php
$con = mysqli_connect(...);
$user_id= $_POST["user_id"];
$statement = mysqli_prepare($con, "SELECT Gym.gym_id, Gym.name, user_gym_rel.user_id FROM Gym INNER JOIN user_gym_rel ON Gym.gym_id = user_gym_rel.gym_id WHERE user_gym_rel.user_id = '$user_id');
mysqli_stmt_execute($statement);
mysqli_stmt_store_result($statement);
mysqli_stmt_bind_result($statement, $gymid, $gymname, $userid);
$response = array();
i=0
while(mysqli_stmt_fetch($statement)){
$response[i] = array();
$response[i]['gym_id'] = $gymid;
$response[i]['gym_name'] = $gymname;
$response[i]['user_id'] = $userid;
i++;
}
echo json_encode($response);
?>
Ok so thanks for the speedy reply, added the ", changed the variable i to $i. and everything works! thanks for the help everyone :)
Related
This question already has answers here:
php JSON_encode not working
(4 answers)
Closed 1 year ago.
What I am trying to achieve is to simply put all the rows of a mysqli result to a JSON file.
My code looks like this:
$sth = mysqli_query($mysqli, "SELECT * FROM table");
$rows = array();
while($r = mysqli_fetch_assoc($sth)) {
$rows[] = $r;
}
print_r($rows);
$mysqli->close();
$fileobj = fopen("takeOutItems.json", 'w');
fwrite($fileobj,json_encode($rows));
fclose($fileobj);
printing the $rows arrays shows data correctly.
fwrite, however, does not change anything in takeOutItems.json.
What am I doing wrong?
The issue was that some data elements were not displayed correctly. Adding $mysqli->set_charset("utf8"); resolved the issue.
This question already has answers here:
How to force PDOStatement->fetchAll to return array of objects?
(3 answers)
Closed 3 years ago.
I'm getting an error and I don't really know where is the issue. Please can anybody show me what is wrong? I would appreciate any assistance, thanks!
Trying to get property of non-object in on line 20
class.php
class PostsData extends dbh {
public function fetchAllPosts() {
$sql = "SELECT * FROM post";
$stmt = $this->connect()->query($sql);
$stmt->execute([]);
$result = $stmt->fetchAll();
return $result;
} }
blog.php
$post_ = new PostsData;
$allposts = $post_->fetchAllPosts();
foreach ($allposts as $post) {
echo $post->post_title; //error
You are not checking if the result returned is null or not. The error would be generated if it is because there will be no property to access altogether.
Consider a print statement in FetchAllPosts function to check if you get any rows returned. That may help narrow down the scope of the error.
This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 5 years ago.
I want to save the JSON coordinates of polygon in the PostGIS database.
This is my PHP code-
$coordinates = ($_POST['Coordinates']);
$query = "INSERT INTO table_name (column_name) VALUES
(ST_SetSRID(ST_GeomFromGeoJSON ('{
['".($coordinates) ."']
}'), 4326))"
$success = pg_query($conn,$query);
where Coordinates are-
{"type":"polygon","coordinates":[[[-97.53662109375,43.67581809328341],[-98.096923828125,42.60970621339408],[-96.427001953125,42.924251753870685],[-97.53662109375,43.67581809328341]]]}
but the error is-
"Parse error: syntax error, unexpected '$success' (T_VARIABLE)"
column_name is polygon type column in table.
please someone correct me how to resolve syntax error.
Thanks.
T_VARIABLE error mainly occurs because of the syntax error. You missed a Semicolon before $success line. Please check the syntax
$coordinates = ($_POST['Coordinates']);
$query = "INSERT INTO table_name (column_name) VALUES
(ST_SetSRID(ST_GeomFromGeoJSON ('{
['".($coordinates) ."']
}'), 4326))";
$success = pg_query($conn,$query);
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
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';";