检索一列mysql

时间:2014-12-19 18:14:01

标签: php mysql

我试图从mysql数据库中检索一些colums。我想要检索行"锦标赛"和timeanddate"。我已创建此代码来执行此操作:

<?php
  include "admin/db.inc.php";//database connection
  $tournament_db ="SELECT tournament FROM sbc";
  $tournament = mysql_query($tournament_db) or die(mysql_error());
  $timeanddate_db ="SELECT timeanddate FROM sbc";
  $timeanddate = mysql_query($timeanddate_db) or die(mysql_error());
  ?>

对于我使用的输出:

<p><a href="turnering.php?id=6"><? echo mysql_result($tournament, 5)," (", mysql_result($timeanddate, 5),")"; ?></a></p>
            <p><a href="turnering.php?id=7"><? echo mysql_result($tournament, 6)," (", mysql_result($timeanddate, 6),")"; ?></a></p>
            <p><a href="turnering.php?id=8"><? echo mysql_result($tournament, 7)," (", mysql_result($timeanddate, 7),")"; ?></a></p>
            <p><a href="turnering.php?id=9"><? echo mysql_result($tournament, 8)," (", mysql_result($timeanddate, 8),")"; ?></a></p>

我的问题是:这是正确的方法,还是另一种方法,以便加载时间更快?

3 个答案:

答案 0 :(得分:1)

希望这是你想要做的事情:

&#13;
&#13;
$query = mysql_query("SELECT tournament, timeanddate FROM sbc") or die(mysql_error();
$row=mysql_fetch_array($query)
{

echo $row['tournament']." ".$row['timeanddate'];

}
&#13;
&#13;
&#13;

OR

如果在数据库中指定了id并且您想在页面上传递它,则可以尝试使用

&#13;
&#13;
											 $query = mysql_query("SELECT id,tournament, timeanddate FROM sbc") or die(mysql_error();
    $row=mysql_fetch_array($query)
    {

    <p><a href="<?php turnering.php?id=echo $row[id]; ?>"><? echo $row['tournament'].",".$row['timeanddate']; ?></a></p>

    }
&#13;
&#13;
&#13;

答案 1 :(得分:0)

非常低效,但它会起作用。

更好的方法是使用SINGLE查询:

$sql = "SELECT tournament, timeanddate FROM sbc"
$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_assoc($result)) {
   echo $row['tournment'];
   echo $row['timeanddate'];
}

答案 2 :(得分:-1)

不要使用已弃用的mysql,请使用pdo或mysqli 并在单个sql中使用这两列。不要将mysql_result用于大数据,因为它可能很慢; 使用mysql_array并根据您的要求检索数据。