PHP

Getting Started String Date If Else Loop Array Functions Session Files Super Global Variables Debugging MySQL MySQL Read Data JSON Markdown
O O

Weather Controls

Time Of Day
Rain
Wind Speed
Wind Direction
Clouds

PHP : MySQL Read Data

2018-03-08

Reading Data From MySQL

Get Record Count

$rsData = mysqli_query($db, "SELECT * FROM robsTable3  WHERE type = 3 "); 
echo mysqli_num_rows($rsData); 

Show meta data like row/column count.

Show information like number of columns, number of rows and current count. These both show the exact same data, but in slightly different format.

var_dump($rsData); 
print_r($rsData);    //use this one

Show Me Some Data

// this will only show 1 piece of data from 1 random record. no good
$row = mysqli_fetch_array($rsData);
print_r($row[0]); 

//this will show an array of all data, but only for 1 record.
$row = mysqli_fetch_assoc($rsData);
print_r($row); 

// this will me show me all the data and all the records (view in page source for better formating) 
// in key/value pairs
while ($row = mysqli_fetch_assoc($rsData)) {
    print_r($row); 
}

// to show it with numbered index instead of keys
while ($row = mysqli_fetch_array($rsData,MYSQLI_NUM)) {
    print_r($row); 
}

Comprehensive List of Array loops

// number indexed array
$row=mysqli_fetch_array($rsData,MYSQLI_NUM);
print_r($row[0]); 

// key inexed array
$row=mysqli_fetch_array($rsData,MYSQLI_ASSOC);
$row=mysqli_fetch_assoc($rsData);
print_r($row["name"]); 

// allos reference by either key or index, or both. DEFAULT
$row=mysqli_fetch_array($rsData,MYSQLI_BOTH);
$row=mysqli_fetch_array($rsData);
print_r($row[0]); 
print_r($row["name"]); 

Put All the data in an aray, then show me the array


//rows are number based, columns are key based
$recordArray = array();
while ($row = mysqli_fetch_assoc($rsData)) {
    $recordArray[] = $row;
}
print_r($recordArray); 
echo $recordArray[0]["name"];

//rows and columns are number based
$recordArray = array();
while ($row=mysqli_fetch_array($rsData,MYSQLI_NUM)) {
    $recordArray[] = $row;
}
print_r($recordArray); 
echo $recordArray[0][0];

Display Results in Table

echo "<table>";
while($row = mysqli_fetch_assoc($rsData)) {
    echo "<tr><td>Name:</td><td> {$row["name"]}</td><td>Description:</td><td>{$row["description"]}</td></tr>";    
}
echo "</table>";