迭代获取的SQL数据

时间:2016-08-11 11:13:12

标签: php

我在我的活动网站上添加了一个数据库,其中每个网页都有自己的SQL表。每个页面可以包含任意数量的事件。该表有6列(ID,名称,俱乐部,位置,主机,描述)。目前我使用一种无​​聊的方法来调用数据。我正在寻找我的代码的补充,以便调用数据更自动化。我的代码到目前为止。

<?php
  $dbhost  = 'localhost';    
  $dbname  = 'events';   
  $dbuser  = 'max';   
  $dbpass  = '';   
  $appname = "Dundaah"; 

  $connection = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
  if ($connection->connect_error) die($connection->connect_error);

  function queryMysql($query)
  {
    global $connection;
    $result = $connection->query($query);
    if (!$result) die($connection->error);
    return $result;
  }
  $row = queryMysql("SELECT * FROM monday WHERE ID=1");
  $one = $row->fetch_array(MYSQLI_ASSOC);
  $row2 = queryMysql("SELECT * FROM monday WHERE ID=2");
  $two = $row2->fetch_array(MYSQLI_ASSOC);

?>

然后调用我使用的第一个事件。

<?php echo ucwords(strtolower($one['name']));?>
<?php echo ucwords(strtolower($one['club']));?>
<?php echo ucwords(strtolower($one['location']));?>
<?php echo ucwords(strtolower($one['host']));?>
<?php echo ucwords(strtolower($one['description']));?>

对于第二个事件,我使用相同的方法,但我正在寻找一种循环方式,而不是每次都声明一个新的获取数组。谢谢。

3 个答案:

答案 0 :(得分:2)

这些方面的某些内容可能有所帮助。

首先创建一个包含一周中几天的数组,然后在foreach中遍历数组,并在每天对数据库执行查询。在这个例子中,我将每天的结果存储在一个数组中,其中Day名称作为密钥(未经测试,但希望它有帮助)。

$dbhost  = 'localhost';    
  $dbname  = 'events';   
  $dbuser  = 'max';   
  $dbpass  = '';   
  $appname = "Dundaah"; 

  $connection = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
  if ($connection->connect_error) die($connection->connect_error);

$daysOfTheWeek = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];

$resultsFromDB = [];

foreach ($daysOfTheWeek as $day) {
    $query = "SELECT * FROM events WHERE day = '$day'";
    $result = $connection->query($query);
    $resultsFromDB[$day] = $result->fetch_all(MYSQLI_ASSOC);
}

// Display the results in a page
echo "<h1>Events</h1>";

foreach ($daysOfTheWeek as $day) {
  echo "<h2>$day</h2>";

  foreach ($resultsFromDB[$day] as $event) {
    echo $event['name'];
  }
}

答案 1 :(得分:1)

您可以使用IN sql语句获取具有多个不同ID的行。

  $row = queryMysql("SELECT * FROM monday WHERE ID IN (1, 2) ORDER BY ID ASC");
  $one = $row->fetch_assoc(); // same as fetch_array( MYSQLI_ASSOC )
  $two = $row2->fetch_assoc();

ORDER BY ID ASC如果你想把第一行作为id=1(最少ID)的行,而不是它mysql(以及任何其他数据库)不保证任何订单,则需要。{/ p >

如果您需要(例如)所有id小于1000的行。您可以使用“less or equeal”比较并将所有行保存到数组中。

  $result = queryMysql("SELECT * FROM monday WHERE ID<=1000 ORDER BY ID ASC");
  $all_rows = $result->fetch_all( MYSQLI_ASSOC );

  // then
  //$one = $all_rows[0];
  //$two = $all_rows[1];

此外,您无需立即实现所有结果。 Mysqli_result是可遍历的对象。您可以随时编写foreach( $result as $row )并获取所有行。在foreach期间,所有原始数据都将作为assoc数组获取。

答案 2 :(得分:0)

知道了。

&#13;
&#13;
$query = "SELECT * FROM events WHERE day='mon'";

$result = $conn->query($query);
if (!$result) die($conn->error);

$rows = $result->num_rows;
$j = 0;

$name_array='';
$club_array='';
$location_array='';
$host_array='';
$description_array='';

while ($j < $rows )
{
$result->data_seek($j);
$row = $result->fetch_array(MYSQLI_ASSOC);

$event_name = $row['name'];
$event_club = $row['club'];
$event_location = $row['location'];
$event_host = $row['host'];
$event_description = $row['description'];

$name_array[]=$event_name;
$club_array[]=$event_club;
$location_array[]=$event_location;
$host_array[]=$event_host;
$description_array[]=$event_description;

++$j;
}
&#13;
&#13;
&#13;

&#13;
&#13;
echo $name_array[0];
echo $club_array[0];
echo $location_array[0];
echo $host_array[0];
echo $description_array[0];
&#13;
&#13;
&#13;