【发布时间】:2018-04-27 22:25:39
【问题描述】:
下面是我的函数 listRecordsMatching($accNo) 连接到我的 mySQL 服务器以匹配具有特定帐号的所有记录。我知道我的问题在于我编写 SQL 的方式。
我的表结构如下:(int)id, (string)cheque-no, (double)amount, (date)cheque-date, (string)customer, (int)account。
注意:conn() 是我的配置文件中的一个全局函数,它返回连接到我的服务器的 mysqli 对象。
编辑:由于 conn() 函数会导致一些混乱:
EDIT2:我找到了答案。在执行 $stmt->execute() 之后,我必须将结果绑定到变量才能使用它们。 $stmt->bind_results($var1, $var2, ...)
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$GLOBALS['conn'] = $conn;
function conn(){
return $GLOBALS['conn'];
}
function listRecordsMatching($accNo){
//prepare statement
$stmt = conn()->prepare("SELECT * FROM `mfs-cheque-app`.`cheques` WHERE `account` = ?");
$stmt->bind_param('i', $accNo);
$stmt->execute();
$row=$stmt->fetch(PDO::FETCH_ASSOC);
var_dump($row) // outputs NULL
echo "<form id='records'>1";
while(true){
if (!$row) break;
$c = new Cheque($row['cheque-no'],$row['amount'],$row['cheque-date'],$row['customer'],$row['account']);
echo $c->list(); // outputs formatted html
}
echo "2</form>";
}
OUT>>> NULL<form id="records">12</form>
【问题讨论】:
-
您是否手动将查询输入到 mySQL 中?这样你就可以测试你的查询是否真的返回任何东西。另外,确认帐户字段是整数类型。
-
我已经这样做了,它返回了我想要的所有行。 mySQL 中的 account 字段是 int,可以是 -1、0、1、2 或 3。
-
我认为你不需要 () 在这个:$stmt = conn()->prepare
-
为什么这个标签是mysqli?
-
你也在混合不同的mysql api,你不能那样做。
标签: php sql mysqli prepared-statement php-7