[英]PHP MySQLi if ID = 1 echo name from row
我正在嘗試制作僅顯示ID為1的行名稱的東西,但似乎無法正常工作。 我可以使它顯示所有名稱,但我只希望它顯示用戶ID 1的名稱。這是我當前的代碼,但是不起作用。
<a style="font-size: 17px; color: #ff0000;"><?php
$q = "SELECT * FROM `Team` WHERE id =1";
$result=mysqli_query($q);
$row = mysqli_fetch_array($result,MYSQLI_ASSOC);
if ($row != FALSE) {
echo '<br />$row is not false.';
$name = $row['name'];
echo $name;
} else{echo "it's false :(";};
?></a>
它返回:
這是錯誤的:(
您可能需要while()檢查。 嘗試類似:
您的數據庫連接:
$servername = "YOUR_HOST";
$username = "YOUR_USER";
$password = "YOUR_PASSWORD";
$dbname = "YOUR_DATABASE";
$mysqli = new mysqli($servername, $username, $password, $dbname);
if ($mysqli->connect_error) {
echo "There was a slight problem, please contact your webmaster before continuing.";
exit();
}
然后,您的主文件將顯示您想要的行:
// create query
$q = "SELECT * FROM Team WHERE id = 1";
// get the records from the database
if ($result = $mysqli->query($q))
{
// display records if there are records to display
if ($result->num_rows > 0)
{
// fetch the results
while ($row = $result->fetch_object())
{
$name = $row->name;
echo $name;
}
}
else
{
echo "No results to display!<br><hr><br>";
}
}
else
{ // show an error if there is an issue with the database query
echo "Error: " . $mysqli->error;
}
// close database connection
$mysqli->close();
mysqli_query要求第一個參數應該是連接字符串,第二個是查詢
mysqli_query($ link,“您的查詢”);
您需要添加連接參數!
$result=mysqli_query($db, $q);
代替
$result=mysqli_query($q);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.