如何在 Jquery mobile 中使用冒号转义 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 mobile。知道吗?

通过查看文档,您似乎不应该将query用于参数化查询:

$sth = $dbh->prepare('SELECT name, colour, calories
    FROM fruit
    WHERE calories < ? AND colour = ?');
$sth->execute(array(150, 'red'));
$red = $sth->fetchAll();
这只是

如何使用预准备语句的一个 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();
?>