gpt4 book ai didi

php - 从数据库中获取数据,没有得到正确的数据

转载 作者:搜寻专家 更新时间:2023-10-30 22:00:45 24 4
gpt4 key购买 nike

我有一个看起来像这样的数据库-

Image 1

我正在尝试根据时间获取前 10 个条目(在 time 列中具有前 10 个值的条目)。我有以下代码。

    <?php
include_once("connect.php");
$sql = "SELECT * FROM scores order by time desc limit 10";
$query = mysql_query($sql) or die("systemResult=Error");
$counter = mysql_num_rows($query);

if($counter>0)
{
print("systemResult=Success");
$array = mysql_fetch_array($query);

foreach($array as $data)
{
$athleteName = $data["athleteName"];
$email = $data["email"];
$time = $data["time"];
$timeStamp = $data["timeStamp"];
$country = $data["country"];

print "&athleteName=" . $athleteName;
print "&email=" . $email;
print "&time=".$time;
print "&timeStamp=".$timeStamp;
print "&country=".$country;
}
}
else
{
print("systemResult=Error");
}
?>

我得到的输出是

systemResult=Success&athleteName=7&email=7&time=7&timeStamp=7&country=7&athleteName=7&email=7&time=7&timeStamp=7&country=7&athleteName=4&email=4&time=4&timeStamp=4&country=4&athleteName=4&email=4&time=4&timeStamp=4&country=4&athleteName=G&email=G&time=G&timeStamp=G&country=G&athleteName=G&email=G&time=G&timeStamp=G&country=G&athleteName=n&email=n&time=n&timeStamp=n&country=n&athleteName=n&email=n&time=n&timeStamp=n&country=n&athleteName=2&email=2&time=2&timeStamp=2&country=2&athleteName=2&email=2&time=2&timeStamp=2&country=2&athleteName=I&email=I&time=I&timeStamp=I&country=I&athleteName=I&email=I&time=I&timeStamp=I&country=I

可以看出,我得到的输出不是数据库表中的内容。我得到了奇怪的值(value)观。我做错了什么?

最佳答案

你不需要在你的情况下使用 for each,如果是这样,只需打印 $data,尝试删除 foreach 循环,如果你想获取所有记录,那么使用 while:

 while($data = mysql_fetch_array($query))
{
$athleteName = $data["athleteName"];
$email = $data["email"];
$time = $data["time"];
$timeStamp = $data["timeStamp"];
$country = $data["country"];

print "&athleteName=" . $athleteName;
print "&email=" . $email;
print "&time=".$time;
print "&timeStamp=".$timeStamp;
print "&country=".$country;
}

关于php - 从数据库中获取数据,没有得到正确的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15321501/

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