【问题标题】:SQLite3 Select where string querySQLite3 选择字符串查询的位置
【发布时间】:2013-12-04 01:31:01
【问题描述】:

我无法弄清楚最简单的事情。尝试了各种事情,但没有成功。我想简单地从表中选择一个具有特定名称的团队。此代码将只返回表的标题,并且不会打印任何内容,就好像我的查询是空的一样。

我尝试过以不同的方式更改引号,但它要么给我一个错误,要么什么也不打印。

SELECT * from League where Name='AS Roma'

SELECT * from League where Name=AS Roma

SELECT * from League where Name="AS Roma"

有人知道吗?

     $db = new SQLite3('test.db');
     $results = $db->query('SELECT * from League where Name="AS Roma"');

     echo "<table border='1'>";
     echo "<tr>";
     echo "<th>Position</th><th>Club Name</th><th>G</th><th>W</th><th>D</th><th>L</th><th>GF</th><th>GA</th><th>P</th>";
     echo "</tr>";

     while ($row = $results->fetchArray()) {
       echo "<tr>";
       echo "<td>", $row[0], "</td><td>", $row[1],"</td>", "<td>", $row[2], "</td><td>", $row[3],"</td>","<td>", $row[4], "</td><td>", $row[5],"</td>", "<td>", $row[6], "</td><td>", $row[7],"</td>",  "<td>", $row[8], "</td>";
       echo "</tr>";
     }
     echo "</table>";
     echo '<br>';

【问题讨论】:

  • 您确定您的数据库中有一个值为"AS Roma" 的字段吗?返回 0 行的查询不是错误。
  • 是的,当然,这不是问题。我知道,因为它返回空,这意味着找不到它,但它就在那里。我以为我有语法问题
  • 如果我在 SELECT * FROM League where Name='SS Lazio' 之后直接在 SQLite 管理器中输入,它仍然会给我一个空的东西,但它就在那里
  • 什么是 SELECT * FROM League;打印?
  • 包含内容的整个表格

标签: php sqlite


【解决方案1】:

我建议使用 SQLite3::prepare。

$stmt = $db->prepare('SELECT * from League where Name LIKE :name');
$stmt->bindValue(':name', 'AS Roma', SQLITE3_TEXT);

$result = $stmt->execute();
while ( $row = $result->fetchArray()) {
    ...
}

【讨论】:

  • 我不知道为什么,但会出现同样的问题。它只打印表格的标题,没有实际数据
【解决方案2】:

您可能想尝试交换引号并使用反引号。

之前:

$results = $db->query('SELECT * from League where Name="AS Roma"');

之后:

$results = $db->query("SELECT * from League where Name=`AS Roma`");

【讨论】:

  • 试过了 => QLite3::query():无法准备语句:1,没有这样的列:AS Roma
  • 反引号表示一个字段名;引号表示一个字符串
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-09-27
  • 2019-01-06
  • 2016-12-15
  • 1970-01-01
  • 2011-05-21
  • 1970-01-01
  • 2021-01-30
相关资源
最近更新 更多