codeigniter count_all_results with join table [duplicate] - php

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.

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

unable to save polygon coordinates in database [duplicate]

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

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

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

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

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