如何在 Jquery 移动设备中用冒号转义 SQL 查询?
How to escape a SQL query with colon in Jquery mobile?
我正在制作移动网络并尝试使用以下代码向我的服务器数据库发送查询:
$user="root";
$pass="";
$db = new PDO('mysql:host=localhost;dbname=the_restaurant', $user, $pass);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $db->query('SELECT * from table_availability where table_number= :table');
$statement->execute(array(':table' => $table_number));
// Set the fetch mode
$stmt->setFetchMode(PDO::FETCH_ASSOC);
while($row = $stmt->fetch())
{
echo "<tr class=\"clickable\" data-url=\"{orders.php}\"><td><p>" . $row['dish_name']. "</p></td>";
echo "<td><p>" . $row['quantity']. "</p></td>";
echo "<td><p>" . $row['price']. "</p></td>";
echo "<td><p>" . $row['ewt']. "</p></td></tr>";
}
可以执行此查询,但我的背景图片消失了。我追查了一下,罪魁祸首是语句中的冒号。无论如何我可以摆脱这个问题吗?或者任何替代方案?我的 UI 需要我的背景资料。我已经尝试过使用反斜杠,但还是一样:
$stmt = $db->query('SELECT * from table_availability where table_number= \:table');
我正在使用 jQuery 手机。有什么想法吗?
这只是一个 PHP.net 示例,说明如何按照您想要的方式使用准备好的语句。请根据您的需要调整此示例
<?php
$stmt = $dbh->prepare("INSERT INTO REGISTRY (name, value) VALUES (:name, :value)");
$stmt->bindParam(':name', $name);
$stmt->bindParam(':value', $value);
// insert one row
$name = 'one';
$value = 1;
$stmt->execute();
// insert another row with different values
$name = 'two';
$value = 2;
$stmt->execute();
?>
通过查看 docs,您似乎不应该使用 query
进行参数化查询:
$sth = $dbh->prepare('SELECT name, colour, calories
FROM fruit
WHERE calories < ? AND colour = ?');
$sth->execute(array(150, 'red'));
$red = $sth->fetchAll();
我正在制作移动网络并尝试使用以下代码向我的服务器数据库发送查询:
$user="root";
$pass="";
$db = new PDO('mysql:host=localhost;dbname=the_restaurant', $user, $pass);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $db->query('SELECT * from table_availability where table_number= :table');
$statement->execute(array(':table' => $table_number));
// Set the fetch mode
$stmt->setFetchMode(PDO::FETCH_ASSOC);
while($row = $stmt->fetch())
{
echo "<tr class=\"clickable\" data-url=\"{orders.php}\"><td><p>" . $row['dish_name']. "</p></td>";
echo "<td><p>" . $row['quantity']. "</p></td>";
echo "<td><p>" . $row['price']. "</p></td>";
echo "<td><p>" . $row['ewt']. "</p></td></tr>";
}
可以执行此查询,但我的背景图片消失了。我追查了一下,罪魁祸首是语句中的冒号。无论如何我可以摆脱这个问题吗?或者任何替代方案?我的 UI 需要我的背景资料。我已经尝试过使用反斜杠,但还是一样:
$stmt = $db->query('SELECT * from table_availability where table_number= \:table');
我正在使用 jQuery 手机。有什么想法吗?
这只是一个 PHP.net 示例,说明如何按照您想要的方式使用准备好的语句。请根据您的需要调整此示例
<?php
$stmt = $dbh->prepare("INSERT INTO REGISTRY (name, value) VALUES (:name, :value)");
$stmt->bindParam(':name', $name);
$stmt->bindParam(':value', $value);
// insert one row
$name = 'one';
$value = 1;
$stmt->execute();
// insert another row with different values
$name = 'two';
$value = 2;
$stmt->execute();
?>
通过查看 docs,您似乎不应该使用 query
进行参数化查询:
$sth = $dbh->prepare('SELECT name, colour, calories
FROM fruit
WHERE calories < ? AND colour = ?');
$sth->execute(array(150, 'red'));
$red = $sth->fetchAll();