I'm trying to run a simple query with an ORM that is built on top of PDO.
Here's the code I'm trying to run:
$message = ORM::for_table("messages")
->where("to_user_id", $user_id)
->where("deleted", 0)
->where("reply_id", $message_id)
->where("read", 0)
->order_by_desc("time")
->limit(1)
->count();
(This is using j4mie's Idiorm, https://github.com/j4mie/idiorm)
This code seems like it would work, but I get the following MySQL error:
Error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]:
Syntax error or access violation:
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 'read = '0' ORDER BY time DESC LIMIT 1'
at line 1' in /Users/chromium/Documents/root/e119/lib/classes/ORM.class.php:492
Stack trace:
#0 /Users/chromium/Documents/root/e119/lib/classes/ORM.class.php(492): PDOStatement->execute(Array)
#1 /Users/chromium/Documents/root/e119/lib/classes/ORM.class.php(289): ORM->run()
#2 /Users/chromium/Documents/root/e119/app/models/Message.class.php(73): ORM->count()
#3 /Users/chromium/Documents/root/e119/app/views/Messages/IndexView.php(42): Message::conversation_changed('3', '4', true)
#4 /Users/chromium/Documents/root/e119/app/templates/GameTemplate.php(13): require('/Users/chromium...')
#5 /Users/chromium/Documents/root/e119/lib/classes/Load.class.php(83): require('/Users/chromium...')
#6 /Users/chromium/Documents/root/e119/app/controllers/M on line 492 of /Users/chromium/Documents/root/e119/lib/classes/ORM.class.php
read and time are reserved words in mySQL.
You'll have to rename the columns, or wrap backticks around the columnn names:
->order_by_desc("`time`")
->where("`read`", 0)
(provided the ORM allows that, of course.)
Related
does not work my mysql commad UPDATE, how to fix it?
this is code
$mysql_query = $db->update(array('`status`' => $cache['b']['status']))
->set(array('`cache`' => $packed_cache))
->set(array('`cache_time`' => $packed_times))
->table('`'.$lgsl_config['db']['table'].'`')
->where('`id`', '=', $mysql_row['id'])
->limit(1);
and bug is
Fatal error: Uncaught exception 'PDOException' with message '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 'OFFSET 0' at
line 1' in
/nfsmnt/hosting2_1/c/3/c33777c4-d67a-498e-a243-d730693c74c1/pallax.systems/sub/serverlist/vendor/slim/pdo/src/PDO/Statement/StatementContainer.php:535
Stack trace: #0
/nfsmnt/hosting2_1/c/3/c33777c4-d67a-498e-a243-d730693c74c1/pallax.systems/sub/serverlist/vendor/slim/pdo/src/PDO/Statement/StatementContainer.php(535):
PDO->prepare('UPDATE lgsl S...') #1
/nfsmnt/hosting2_1/c/3/c33777c4-d67a-498e-a243-d730693c74c1/pallax.systems/sub/serverlist/vendor/slim/pdo/src/PDO/Statement/StatementContainer.php(428):
Slim\PDO\Statement\StatementContainer->getStatement() #2
/nfsmnt/hosting2_1/c/3/c33777c4-d67a-498e-a243-d730693c74c1/pallax.systems/sub/serverlist/vendor/slim/pdo/src/PDO/Statement/UpdateStatement.php(86):
Slim\PDO\Statement\StatementContainer->exe in
/nfsmnt/hosting2_1/c/3/c33777c4-d67a-498e-a243-d730693c74c1/pallax.systems/sub/serverlist/vendor/slim/pdo/src/PDO/Statement/StatementContainer.php
on line 535
!!! i found bug ..
UPDATE `lgsl` SET `status` = ? , `cache` = ? , `cache_time` = ? WHERE `id` = ? LIMIT 1 OFFSET 0
at the end is OFFSET 0 but I do not know how to remove it
Remove ->limit(1) as it is not really necessary for your UPDATE statement.
Although it should work, I believe there is a bug with that method.
When I run this code:
$addUniverseColumn = $db->prepare("ALTER TABLE spaceships ADD :universe int");
$addUniverseColumn->bindParam(":universe", $name);
$addUniverseColumn->execute();
I get the following error:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 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 ''asfa' int' at line 1' in D:\XAMPP\htdocs\php\locationconfig.php:63 Stack trace: #0 D:\XAMPP\htdocs\php\locationconfig.php(63): PDOStatement->execute() #1 {main} thrown in D:\XAMPP\htdocs\php\locationconfig.php on line 63
Note: $addUniverseColumn->execute(); is the line 63.
I have little to no idea as to what the problem is. I've searched for an answer to the problem but I can't find anything. Any help would be appreciated. :)
Placeholders can only work for VALUES, never field/table names. You cannot use a placeholder for the field name in an ALTER query. You'll have to use good old string interpolation for it:
$db->prepare("ALTER TABLE spaceships ADD $name int");
i am trying to use this PHP PDO prepared statement to run an SQL Query:
$stmt = $pdo_conn->prepare("SELECT *, LEAST(:col_list) as num FROM callplandata WHERE number LIKE :number HAVING num != 0 ");
$stmt->execute(array(':col_list' => implode(',',$column_list), ':number' => '%'.$_POST["prefix"].'%'));
but its showing this error message:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1582 Incorrect parameter count in the call to native function 'LEAST'' in /home/integra/public_html/included_pages/call_tarrif_search.php:62 Stack trace: #0 /home/integra/public_html/included_pages/call_tarrif_search.php(62): PDOStatement->execute(Array) #1 /home/integra/public_html/index.php(119): include('/home/integra/p...') #2 {main} thrown in /home/integra/public_html/included_pages/call_tarrif_search.php on line 62
what am i doing wrong?
LEAST accepts 2 or more values and returns the least one.
You're passing a single value, that is the roots of the error.
PS: and as soon as you haven't explained the original issue - there is nothing to add here. Please don't ask "how to fix it" because we have no idea what you're trying to achieve.
Really struggling with these damn syntax errors. If anyone can see what's wrong with this please tell me:
Query string
INSERT INTO Categories (ParentId,Title,Desc) VALUES (?,?,?)
Print_R of array passed
Array
(
[0] => 1
[1] => Lolcats
[2] => an entire category dedicated to the language, lolcats!
)
Error:
<b>Fatal error</b>: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 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 'Desc) VALUES ('1','Lolcats','an entire category dedicated to the language, lolca' at line 1' in /usr/www/maxtingle/Blog/System/Core/Functions.php:168
Stack trace:
#0 /usr/www/maxtingle/Blog/System/Core/Functions.php(168): PDOStatement->execute(Array)
#1 /usr/www/maxtingle/Blog/System/Core/Category.php(12): Functions\Database->Insert('(ParentId,Title...', 3, Array)
#2 /usr/www/maxtingle/Blog/System/Category.php(9): Categories::CreateCategory(Object(CategoryIns))
#3 [internal function]: Category::Create()
#4 /usr/www/maxtingle/Blog/index.php(52): call_user_func_array(Array, Array)
#5 {main}
thrown in <b>/usr/www/maxtingle/Blog/System/Core/Functions.php</b> on line <b>168</b><br>
Desc is a keyword.To use it as column name use backticks
INSERT INTO Categories (ParentId,Title,`Desc`) VALUES (?,?,?)
The best practice is to use back ticks for all table names and column names so you will never run into keyword issues.
INSERT INTO `Categories` (`ParentId`,`Title`,`Desc`) VALUES (?,?,?)
The query (As echo'd)
DELETE * FROM BlogPosts WHERE Id=?
The error
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 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 '* FROM BlogPosts WHERE Id='6'' at line 1' in /usr/www/maxtingle/Blog/System/Core/Functions.php:177 Stack trace: #0 /usr/www/maxtingle/Blog/System/Core/Functions.php(177): PDOStatement->execute(Array) #1 /usr/www/maxtingle/Blog/System/Core/BlogPost.php(201): Functions\Database->Delete('*', 'Id=?', Array) #2 /usr/www/maxtingle/Blog/System/Blog.php(102): BlogPost->Delete() #3 [internal function]: Blog::DeletePost('6') #4 /usr/www/maxtingle/Blog/index.php(52): call_user_func_array(Array, Array) #5 {main} thrown in /usr/www/maxtingle/Blog/System/Core/Functions.php on line 177
A DELETE always deletes an entire row, so specifying the fields to delete makes no sense.
You want:
DELETE FROM BlogPosts WHERE Id=?
You don't need the *, you delete the whole row:
DELETE FROM BlogPosts WHERE Id=?