PDO Suggest refine results different than wildcard symbol - php

I am trying to make a PDO query to be searchable not only by the whole string but also by first letter or last letter anything like this. My question is what approach I have to take to achieve this goal.
My original idea was to use wildcard symbol and something like the following:
SELECT * FROM idname WHERE field LIKE CONCAT('%', :field , '%')
but this option for me is not working since I am getting an error:
Warning: Division by zero in**
Warning: Division by zero in E:\xampp\htdocs\search-contact.php on line 111
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 'LIMIT 0, 30' at line 1' in E:\xampp\htdocs\dolche\admin\class\pagination.php:451 Stack trace: #0 E:\xampp\htdocs\dolche\admin\class\pagination.php(451): PDOStatement->execute() #1 E:\xampp\htdocs\search-contact.php(125): pagination->execute() #2 {main} thrown in E:\xampp\htdocs\class\pagination.php on line 451
My code ad the moment is the following:
try
{
$paginate = new pagination($page, 'SELECT * FROM idname WHERE field LIKE :field', $options);
}
catch(paginationException $e)
{
echo $e;
exit();
}
$paginate->bindValue(':field', $_POST['field'] , PDO::PARAM_STR);
$paginate->execute();
Any suggestions are welcome ?

When using PDO you have to put the wildcards in the parameter, so it would look like:
$paginate->bindValue(':field', '%'.$_POST['field'].'%' , PDO::PARAM_STR);

Related

Does not work my command UPDATE on framework FaaPz/Slim-PDO

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.

Adding column to table returns error saying error in SQL syntax

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

using LEAST in SQL query when using PHP PDO Prepared statements

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.

PDO Find a needle in a haystack SQL statement

I am using PDO, and am thrown an error when using the following code:
$stmt = $pdo->prepare("SELECT username FROM users WHERE
WHERE INSTR(`games`, '{$gameid}') > 0
");
$gameid = $gamedata['id'];
$stmt->execute(array(
':gameid'=>$gameid
));
$players = $stmt->fetch(PDO::FETCH_ASSOC);
Through looking at past answers this is supposed to work, however I am met with 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 'WHERE
INSTR(`games`, 'crysis') > 0' at line 2' in C:\xampp\htdocs\gs\gamepage.php:19 Stack
trace: #0 C:\xampp\htdocs\gs\gamepage.php(19): PDOStatement->execute(Array) #1 {main}
thrown in C:\xampp\htdocs\gs\gamepage.php on line 19
It also appears it's grabbing 'games' as a literal and not the column
What am I doing wrong?
You have a double WHERE:
SELECT username FROM users WHERE
WHERE
You're also doing some funny things with $gameid, namely setting the variable after substitution, and binding an unused :gameid parameter. You also have a SQL injection vulnerability and should really use a parameter to pass $gameid instead of creating dynamic SQL.
You have the word games encased in "back quotes" and not "single quotes" like the {$gameid} variable is using. They are probably making the db engine assume it is a column name instead of text.
$stmt = $pdo->prepare('SELECT `username` FROM `users`
WHERE INSTR(`games`, :gameid) > 0;');
And you should use $stmt->bindValue() or $stmt->bindParameter() before executing the query.
This won't work if gameid is an ... INTEGER ! ? ! ?

Simple PHP/MySQL ORM Code Not Executing

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

Categories