How to show full query string when occur errors. My query as follows
$builder = $this->table("$this->table");
$builder->select("usr_lock");
$builder->where("usr_lock >= '" . date('Y-m-d H:i:s') . "'");
$builder->where($this->primarykey, $user_id);
$r = $builder->get()->rowArray();
echo "<br>Error: " . $builder->error();
echo "<br>Last Query: " . $builder->getLastQuery();
return $r['usr_lock'];
When execute this, it is showing errors as You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '= '1'' at line 4 . To know about the error I need to get full query. So I used $builder->getLastQuery(). But it is not executes. The page look like as
Use $builder->getCompiledSelect() before executing the $builder->get()
$builder = $this->table("$this->table");
$this->db_debug = FALSE;
$builder->select("usr_lock");
$builder->where("usr_lock >= '" . date('Y-m-d H:i:s') . "'");
$builder->where($this->primarykey, $user_id);
echo "<br>Last Query: " . $builder->getCompiledSelect(false);
exit();
Related
I am wanting to get column information on a Postgres database table. I am using the following query:
select column_name as "Field", data_type as "Type", is_nullable as "Null", character_maximum_length as "max_length" from information_schema.columns where table_name='testempty'
(At some point, I will be removing the AS clauses. I had reasons for including them when I originally set up this query, but these reasons have since evaporated.)
When I run the query in PGAdmin, I get the results I expect: There are 2 columns, and I see their requested details. When I execute the same query using PDO in PHP, I get 0 rows back. No errors, the execute call returns true. Here is the PHP code:
try {
$remote_statement = $remote_con->prepare($column_query);
$remote_exec = $remote_statement->execute();
} catch(Exception $e) {
file_put_contents("logs/remote.log", '[' . date("Y-m-d H:i:s") . ' ' . $_SERVER["REMOTE_ADDR"] . "] Prepare failed: " . $e->getMessage() . "\n", FILE_APPEND);
}
if (!$remote_exec) {
file_put_contents("logs/remote.log", '[' . date("Y-m-d H:i:s") . ' ' . $_SERVER["REMOTE_ADDR"] . "] Execute failed\n", FILE_APPEND);
}
$remote_error = $remote_statement->errorInfo();
if (!empty($remote_error[2])) {
file_put_contents("logs/remote.log", '[' . date("Y-m-d H:i:s") . ' ' . $_SERVER["REMOTE_ADDR"] . "] Query failed: " . $remote_error[2] . "\n", FILE_APPEND);
die($remote_error);
}
$remote_rows = $remote_statement->fetchAll(PDO::FETCH_ASSOC);
$remote_con is a PDO connection object I created earlier in the code. $column_query is set to the query I listed above. There is another table I run this same code on prior to this and I get the expected results.
I appreciate any helpful hints here. I am sure I am missing something obvious, but it baffles me that the query works in PGAdmin and not via a PHP call.
This turned out to be a table-specific permissions issue. Granted SELECT permissions to PUBLIC for a problem table and the query via PHP worked. I found another pair of tables in a different database with this issue, and all was resolved by granting this permission.
I am using Server version 10.1.21-MariaDB and mysql for performing data operation. I am creating mini search tool for searching jokes.The overview of how i am performing it is shown in image. Everything works fine but when i try to execute sql statement,it shows syntax error. I tried to dig inside it but as i am quite new to this stuff,i lost here and there digging the exact way of using syntax. The error is shown in bold letter and code is provided below where error occurs.I think error must be inside the try block,please help me to sort this out.
//query logic
$select = 'SELECT id,joketext ';
$from = 'FROM joke_info';
$where = 'WHERE TRUE';
$placeholders = array();
if(isset($_GET['author']) != ''){
$where .= " AND authorid = :authorid";
$placeholders[':authorid'] = $_GET['author'];
}
if(isset($_GET['category']) != ''){
$from .= ' INNER JOIN jokecategory ON id = jokeid';
$where .= " AND categoryid = :categoryid";
$placeholders[':categoryid'] = $_GET['category'];
}
if ($_GET['text'] != '')
{
$where .= " AND joketext LIKE :joketext";
$placeholders[':joketext'] = '%' . $_GET['text'] . '%';
}
print_r($placeholders);
try
{
$sql = $select . $from . $where;
$s = $pdo->prepare($sql);
$s->execute($placeholders);
}catch (PDOException $e)
{
$error = 'Error fetching jokes. ';
echo $error.$e->getMessage();
exit();
}
Error Says this:
Array ( [:authorid] => 6 [:categoryid] => 10 [:joketext] => %been working% ) Error fetching jokes. SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'TRUE AND authorid = '6' AND categoryid = '10' AND joketext LIKE '%been working%'' at line 1
Your generated query after $sql = $select . $from . $where; will be something like SELECT id,joketext FROM joke_info WHERE TRUE
So there is no space between joke_info and Where clause
It should be $from = ' FROM joke_info' and $where = ' WHERE TRUE '; instead of $from = 'FROM joke_info' and '$where ='WHERE TRUE';
Ther is no space between $from and $where variables. Currently they are concatenating to: 'FROM joke_infoWHERE TRUE'. Try giving a space in the $from variable.
I have a table with all the cities in my country, but they are in uppercase. Im trying to convert the first letter to uppercase and the rest to lower case.
Some of them have the single quote accent (Example: Sao Martinho D'oeste) and they are the only ones that give me an error when i try to update the table after converting them.
$cidadeNome = strtolower($cidade['desc_cidade']);
$cidadeNome = ucwords($cidadeNome);
$sql = "UPDATE cidades SET desc_cidade = '".$cidadeNome."' WHERE cidade_id = ".$cidade['cidade_id']."";
$atualizado = $db->query($sql);
if (!$atualizado)
{
echo "Erro (" . $db->errno . ") " . $db->error . "\n";
$db->close();
exit;
}
My code is very simple. The error i get is
Erro em (1064) You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'oeste' WHERE cidade_id = 88382' at line 1
My code work for every city, unless it has an single quote.
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SET =test WHERE =test' at line 1] in EXECUTE("UPDATE SET =test WHERE =test")
$sql = 'UPDATE ' . $this->recipientDbTable . ' SET ' . $this->recipientDbColumn['result_id'] . '=' . 'test' . ' WHERE ' . $this->recipientDbColumn . '=' . 'test';
It looks like $this->recipientDbColumn['result_id'] is null or empty. Look at your error log with error_reporting(E_ALL), it may have an Undefined index error.
Also, echo out the actual SQL query and post it here, it should be obvious what the problem is.
Also, use prepared statements.
$this->recipientDbColumn['result_id'] and $this->recipientDbColumn as the error suggests return empty string.
... right syntax to use near 'SET =test WHERE =test' at line 1] in EXECUTE("UPDATE SET =test WHERE =test")
As you could see, the call returned empty string. Check the code for where you missed it!
According to the error, it seems that your $this->recipientDbTable & other variables doesn't contains the values.
Try
echo $this->recipientDbColumn;
Check if it prints the values
I have the next code (compilated in NetBeans)
$query = 'INSERT INTO grupos(nombre, descripcion)'
. ' VALUES ("' . utf8_decode($_POST['name']) . '", "' . utf8_decode($_POST['desc']) . '")';
$result = $con->query($query) or exit("Error: " . $con->errno . ": " . $con->error);
if ($result->affected_rows > 0) {
Why I got this: "Trying to get property of non-object "
if when I made a echo $result; display a '1'?
From the documentation of mysqli::query():
For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE.
Since your query is INSERT, not SELECT, it doesn't return a mysqli_result, it just returns TRUE. You should use $con->affected_rows instead of $result->affected_rows.