我用mysqli_query回傳的是空值
程式碼如下:
$ql = "SELECT * FROM player";
echo $ql;
$result = mysqli_query($connect, $ql);
if (!$result)
{
echo "沒有值";
echo mysqli_error($connect);
}
用mysqli_error看報錯訊息如下:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
請問一下你用 cmd 去下這一道 sql 語句時也是報這個錯誤嗎?因為這一句 sql 感覺沒問題,也沒有用到保留字,真奇怪...
另外,建議改用 PDO
, mysqli_query
不安全。
建議一開始就用PDO啦,比mysqli輕鬆很多且也不用在意在新版PHP環境下無法相容的問題
<?php
$username = "dbusername";
$password = "yourpassword";
$servername = "127.0.0.1";
$dbname = "myfirstdb";
$dsn = "mysql:host=$servername;dbname=$dbname";
// 以上一大半都是在設定而已
$pdo = new PDO($dsn, $username, $password);
//產生資料庫連接,連接方式都統一成同一種語法,不分MS MY POST...
$stat = $pdo->prepare("SELECT * FROM myfirstdb.table WHERE.........");
$stat->execute();
//以上執行查詢
//以下逐行WHILE顯示資料內容
while ($row = $stat->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_NEXT)) {
echo $row[0] . "\t" . $row[1] . "\t" . $row[2] . "\n";
}
我以前在學校學電腦時,老師還在教BASIC,我已經自學到TC++了