gpt4 book ai didi

PHP MySQLi 如果 ID = 1 则回显行中的名称

转载 作者:行者123 更新时间:2023-11-29 10:44:42 24 4
gpt4 key购买 nike

我正在尝试制作一些只显示 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>

它返回:

it's false :(

最佳答案

您可能需要 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();

关于PHP MySQLi 如果 ID = 1 则回显行中的名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44829134/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com