迭代获取的SQL数据

I am adding a database to my event website with the webpages mon-sun each with its own SQL table. Each page can have any number of events. The table has 6 columns (ID,name,club,location,host,description). Currently I am using a tiresome method to call the data. I'm looking for an addition to my code so that calling the data is a bit more automated. My code so far.

<?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);

?>

Then to call for the first event I use.

<?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']));?>

For the second event I use the same method but I'm looking for a way to loop through instead of declaring a new fetch array every time. Thank you.

</div>

Got it.

$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;
}

echo $name_array[0];
echo $club_array[0];
echo $location_array[0];
echo $host_array[0];
echo $description_array[0];

</div>

Something along these lines may help.

First you create an array that contains the days of the week, then you loop through the array in a foreach and perform a query on the database for each day. In this example I'm storing the results for each day in a single array with the Day name as the key (untested but hopefully it'll help).

$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'];
  }
}

You can use IN sql statement get rows with several different ids.

  $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 required if you want to get first row as row with id=1 (least ID), without it mysql (as well as any other db) doesn't guarantee any order.

If you need (for example) all rows with id less then 1000. You can use "less or equeal" comparison and save all rows into array.

  $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];

Also you don't need to materialize all results immediatly. Mysqli_result is traversable object. You can write foreach( $result as $row ) and get all rows any time. During foreach all raws will be fetched as assoc array.