使用exec将SQL语句中的PHP变量传递给SQLite3

时间:2016-12-16 16:39:27

标签: php sqlite exec

我正在尝试将变量放入在SQLite DB上通过exec执行的SQL语句中。

给定here的示例使用EOF来放入SQL,但从我理解到现在为止,不可能将php变量放在这些标记中?

所以我试图让他们过来:

$sql = "INSERT INTO  `certsIssued` (clientID, userName, dateIssued, revocationDate) VALUES ('".$clientID."','".$userName."', '".$dateIssued."', '".$dateRevocation."')";
$ret = $db->exec($sql); // with no luck 500 error

$sql = "INSERT INTO  `certsIssued` (clientID, userName, dateIssued, revocationDate) VALUES ('\".$clientID.\"','\".$userName.\"', '\".$dateIssued.\"', '\".$dateRevocation.\"')";
$ret = $db->exec($sql); // with no luck 500 error

$sql = "INSERT INTO  `certsIssued` (clientID, userName, dateIssued, revocationDate) VALUES ({$clientID},{$userName}, {$dateIssued}, {$dateRevocation})";
$ret = $db->exec($sql); // with no luck 500 error

有人可以向我解释如何做到这一点吗?

通过一个示例页面开始创建一个新页面,除了我的查询字符串和变量之外,我保留了大部分原封不动的内容。我可以插入并查询结果。我的项目的周围代码中的某些内容一定是错误的

<?php
$number = 1;
$period = 'day';
$userName = 'meandmyself';
$clientID = uniqid();
$dateIssued = new \DateTime("now");
$dateIssued = $dateIssued->format('Y/m/d/h/i/s');
$dateRevocation = new \DateTime("+".$number." ".$period);
$dateRevocation = $dateRevocation->format('Y/m/d/h/i/s');

class MyDB extends SQLite3
{
  function __construct()
  {
     $this->open('db/test.db');
  }
}
$db = new MyDB();
if(!$db){
  echo $db->lastErrorMsg();
} else {
  echo "Opened database successfully\n";
}

 $sql = "INSERT INTO  `certsIssued` (clientID, userName, dateIssued, revocationDate) VALUES ('".$clientID."','".$userName."', '".$dateIssued."', '".$dateRevocation."')";
 $ret = $db->exec($sql);
 if(!$ret){
  echo $db->lastErrorMsg();
 } else {
  echo "Records created successfully\n";
 }
  $db->close();
 ?> 

0 个答案:

没有答案