here’s my simple example:
try {
$sql = 'SELECT itemandsize FROM itemandsize WHERE sub_category = :sub_category' ;
$stmt = $this->db->prepare($sql);
} catch (PDOException $e) {
echo $e->getMessage() . '<br>';
}
try {
$stmt->execute(array(':sub_category' => $var));
} catch (PDOException $e) {
echo $e->getMessage() . '<br>';
}
$var = ‘coffee / tea’
I’m certain that the slash is the problem, but don’t know why.
Do you know why?
I thought treating $var in the array auto resolved all formatting issues.
Obviously not.
$var = ‘coffee - tea’ works fine
Hi there,
Maybe if you wrap the values with back ticks it will work?
$var = "`coffee / tea`";
Hope it helps…
1 Like
There is an exception thrown?
chorn
March 2, 2018, 8:20am
4
No.
<?php
class def {
public function __construct()
{
$var = 'coffee / tea';
$this->db = new PDO('sqlite::memory:');
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->db->query('create table itemandsize(itemandsize int, sub_category text)');
$this->db->query('insert into itemandsize(itemandsize, sub_category) values(123, "'.$var.'")');
try {
$sql = 'SELECT * FROM itemandsize WHERE sub_category = :sub_category' ;
$stmt = $this->db->prepare($sql);
} catch (PDOException $e) {
echo $e->getMessage() . '<br>';
}
try {
$stmt->execute(array(':sub_category' => $var));
print_r($stmt->fetchAll());
} catch (PDOException $e) {
echo $e->getMessage() . '<br>';
}
}
}
new def;
Array
(
[0] => Array
(
[itemandsize] => 123
[0] => 123
[sub_category] => coffee / tea
[1] => coffee / tea
)
)
1 Like
With the same data, or with different data? How is the data inserted into the database, that is, with any escaping or do you use the same prepared statement method?
1 Like
chorn, are you saying the difference is in the insert?
turns out client manually entered coffee / tea.
chorn
March 5, 2018, 6:35am
7
I’m saying there is no problem. With the information you provided i get the expected results.
1 Like
Yes!
Turns out the problem was in my boostrap.
system
Closed
June 4, 2018, 10:01pm
9
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.