fetch_assoc() works only once in a loop - php

I'm looping through 2 tables in MySQL DB (using fetch_assoc()). I would like to get the current id of the 1st table and all the ids of the second table on each iteration but I get the ids of the second table only on the first iteration. From the second iteration upwards, only the current id of the 1st table is returned. I would like to know what I'm doing wrong.
I've already tried for Loops and looked up similar questions here but none have really been of help.
<?php
$my_sqli = new mysqli('localhost', 'root', '', 'taskpro') or die(mysqli_error($my_sqli));
$data1 = $my_sqli->query("SELECT * FROM task_table") or die($my_sqli->error);
$data2 = $my_sqli->query("SELECT * FROM taskinfo") or die($my_sqli->error);
while ($row = $data1->fetch_assoc()) {
echo "<br>";
echo $row['id'];
echo "<br>";
while ($row2 = $data2->fetch_assoc()) {
echo $row2['id'];
} // end child loop
} // end parent loop
?>
This is the result I get
1
1234
2
3
4
5
6
7
8
9
10

The simplest thing to do is to read all the values from query2 into an array and then output the contents of the array in the loop:
$my_sqli = new mysqli('localhost', 'root', '', 'taskpro') or die(mysqli_error($my_sqli));
$data1 = $my_sqli->query("SELECT * FROM task_table") or die($my_sqli->error);
$data2 = $my_sqli->query("SELECT * FROM taskinfo") or die($my_sqli->error);
$rows2 = $data2->fetch_all(MYSQLI_ASSOC);
while ($row = $data1->fetch_assoc()) {
echo "<br>";
echo $row['id'];
echo "<br>";
foreach ($rows2 as $row2) {
echo $row2['id'];
} // end child loop
} // end parent loop
If for some reason you do need to iterate over the result set in the outer loop, you can use mysqli_data_seek to reset the pointer:
while ($row = $data1->fetch_assoc()) {
echo "<br>";
echo $row['id'];
echo "<br>";
while ($row2 = $data2->fetch_assoc()) {
echo $row2['id'];
} // end child loop
// reset $data2 result pointer
$data2->data_seek(0);
} // end parent loop

Basically you have exhausted the resultset of the second query after completing the first iteration of the outer query.
There are a few ways to do this, here are a couple of suggestions
First you could load all of the results from the second query into an array and then just re-process the array each time round the outer loop.
However, the fetch_all() funtion is only available if you have the MYSQL Native Driver installed. (MYSQLIND)
<?php
$my_sqli = new mysqli('localhost', 'root', '', 'taskpro');
if (!$my_sqli) {
die('Connect Error: ' . mysqli_connect_errno());
}
$data1 = $my_sqli->query("SELECT * FROM task_table") or die($my_sqli->error);
$data2 = $my_sqli->query("SELECT * FROM taskinfo") or die($my_sqli->error);
$allData2 = data2->fetch_all(MYSQLI_ASSOC);
while ($row = $data1->fetch_assoc()) {
echo "<br>";
echo $row['id'];
echo "<br>";
foreach ( $allData2 as $row ) {
echo $row2['id'];
}
} // end parent loop
?>
Or you could use the mysqli_result::data_seek() function to reset the pointer to the first row of the query result like this
<?php
$my_sqli = new mysqli('localhost', 'root', '', 'taskpro');
if (!$my_sqli) {
die('Connect Error: ' . mysqli_connect_errno());
}
$data1 = $my_sqli->query("SELECT * FROM task_table") or die($my_sqli->error);
$data2 = $my_sqli->query("SELECT * FROM taskinfo") or die($my_sqli->error);
while ($row = $data1->fetch_assoc()) {
echo "<br>";
echo $row['id'];
echo "<br>";
$data2->data_seek(0); // make sure the pointer is at the beginning before looping
while ($row2 = $data2->fetch_assoc()) {
echo $row2['id'];
}
}
?>

First of all, you don't need the while loop at all. It is just confusing you. You could simply use foreach($data1 as $row). Having said that, it would not solve your problem.
To solve your problem call fetch_all(MYSQLI_ASSOC) on the result from query, or simply chain them together. Be careful, this works only with static queries. If you need to use variables in your SQL, you should use prepared statements.
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli('localhost', 'root', '', 'taskpro');
$mysqli->set_charset('utf8mb4');
$data1 = $mysqli->query("SELECT * FROM task_table")->fetch_all(MYSQLI_ASSOC);
$data2 = $mysqli->query("SELECT * FROM taskinfo")->fetch_all(MYSQLI_ASSOC);
foreach ($data1 as $row) {
echo "<br>";
echo $row['id'];
echo "<br>";
foreach ($data2 as $row2) {
echo $row2['id'];
} // end child loop
} // end parent loop
I have also removed the manual error checking from your code, which was only obstructing your code and should really not have place in your working code. Read How to get the error message in MySQLi?.

Related

Foreach inside foreach only showing one result

$con=mysqli_connect("localhost","root","","database");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
error_reporting(E_ALL ^ E_NOTICE);
$query = "SELECT * FROM TT_posts WHERE post_status='publish' AND
ping_status='open'";
$result = $con->query($query);
while($row1 = $result->fetch_assoc())
foreach ($result as $row1){
$image = "SELECT * FROM TT_posts WHERE post_title='$row1[post_name]'";
$result1 = $con->query($image);
while($row2 = $result1->fetch_assoc())
foreach ($result1 as $row2){
echo "<img src='".$row2[guid]."'>";
echo "<p>".$row1[post_title]."</p>";
}}
?>
actually, below query returns 8 results.
$query = "SELECT * FROM TT_posts WHERE post_status='publish' AND
ping_status='open'";
When it executed the loop, it stops at the first result. I don't know what exactly stops the code.
I think you've got your variable names mixed up. Your first while assigns a row to $row1, then your foreach refers to $result, not $row1. The variable $row1 is actually being overwritten every time it loops around the foreach.
You don't actually need either foreach:
while ($row1 = $result->fetch_assoc()) {
$image = "SELECT * FROM TT_posts WHERE post_title='$row1[post_name]'";
$result1 = $con->query($image);
while ($row2 = $result1->fetch_assoc()) {
echo "<img src='".$row2[guid]."'>";
echo "<p>".$row1[post_title]."</p>";
}
}

PHP loop through array returned by MySQL function

The code below works fine. However I would like to output the results using a loop. I can do it by going through each key individually or as $post[0] for example but not using a loop to go through all the returned fields. All I get is one value of "Array". It looks like the entire array is inserted into a variable I'm not sure what's going on. I have tried http://www.hackingwithphp.com/5/3/0/the-two-ways-of-iterating-through-arrays. Any suggestions appreciated and also if anyone could explain what is going on that would be great. Thanks.
$ID = $_POST['ID'];
function query($ID){
$servername = "x.x.x.x";
$username = "xxxxx";
$password = "xxxxx";
$dbname = "xxxxx";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$query = "SELECT ID, NAME, POSITION, TELEPHONE_NUMBER, EMAIL FROM GROUP WHERE ID = '$ID'";
$result = mysqli_query($conn, $sql);
while($row = mysqli_fetch_array($result)){
$resArr[] = $row;
}
return $resArr;
}
$person = query($ID);
foreach($person as $post) {
echo $post['ID'] . "<br>";
echo $post['NAME'] . "<br>";
echo $post['POSITION'] . "<br>";
echo $post['TELEPHONE_NUMBER'] . "<br>";
echo $post['EMAIL'] . "<br>";
}
?>
Its not totally clear what you are asking but I assume you want to loop through the individual fields of the selected rows. As you built an array containing the query results each result itself being an array mysqli_fetch_array($result) then you can just add an inner loop to process the individual row array like so :-
$persons = query($ID);
foreach($persons as $person) {
foreach ( $person as $fieldname => $value ) {
echo $fieldname . '-' . $value . "<br>";
}
}
Group is reserved word of mysql you can use it in backtics ``
$query = "SELECT ID, NAME, POSITION, TELEPHONE_NUMBER, EMAIL FROM `GROUP` WHERE ID = '$ID'";
$result = mysqli_query($conn, $sql);

Loop through database using PHP and populate HTML table

I am trying to loop through a table in my database and show all the details in a table. Firstly, it should loop through my main table, 'TBook', and get the 'date', 'period', roomID', and 'teacherinitials'. Then, using the roomID, it should look in my other table, 'Rooms', to get the 'room' name and the 'description'. After that it should display the 'date', 'period', 'room' & 'description', and 'teacherinitials'.
This is my code:
<?php
// Create connection
$con=mysqli_connect("host","user","pass","database");
// Check connection
if (mysqli_connect_errno($con)) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//Get number of rows
$sql="SELECT * FROM TBook";
$result=mysqli_query($con, $sql);
$rowcount=mysqli_num_rows($result);
$sql2="SELECT room, description FROM Rooms WHERE roomID = $roomID";
$res=mysqli_query($con,$sql2);
//Start table
echo "<table>";
echo "<tr><th>Date</th><th>Period</th><th>Room</th><th>Teacher Initials</th></tr>";
// Loop through database
while ($row = $result->fetch_assoc()) {
$row = mysql_fetch_array($result);
$date = $row['date'];
$period = $row['period'];
$roomID = $row['roomID'];
$teacherinitials = $row['teacherinitials'];
while ($row2 = $res->fetch_assoc()) {
$room = $row2['room'];
$description = $row2['description'];
}
// Show entries
echo "<tr>
<td>".$date."</td>
<td>".$period."</td>
<td>".$room." (".$description.")</td>
<td>".$teacherinitials."</td>
</tr>";
}
echo "</table>";
?>
However, instead I get an error saying "Fatal error: Call to a member function fetch_assoc() on a non-object in /home/user/public_html/my_bookings-results.php on line 56". Line 56 is this line:
while ($row2 = $res->fetch_assoc()) {
It does show the table headers beneath that, but nothing else. What is going wrong?
change first while loop to
while ($row = mysql_fetch_array($result))
remove below line after while loop
$row = mysql_fetch_array($result);
change:
while ($row2 = $res->fetch_assoc()) {
to
while ($row2 = $res->fetch_array($res)) {

Displaying all records in a mysql table

The code below works fine for printing one record from a database table, but what I really want to be able to do is print all the records in the mysql table in a format similar to my code.
I.E.: Field Name as heading for each column in the html table and the entry below the heading. Hope this is making sense to someone ;)
$raw = mysql_query("SELECT * FROM tbl_gas_meters");
$allresults = mysql_fetch_array($raw);
$field = mysql_query("SELECT * FROM tbl_gas_meters");
$num_fields = mysql_num_fields($raw);
$num_rows = mysql_num_rows($raw);
$i = 1;
print "<table border=1>\n";
while ($i < $num_fields)
{
echo "<tr>";
echo "<b><td>" . mysql_field_name($field, $i) . "</td></b>";
//echo ": ";
echo '<td><font color ="red">' . $allresults[$i] . '</font></td>';
$i++;
echo "</tr>";
//echo "<br>";
}
print "</table>";
Just as an additional piece of information you should probably be using PDO. It has more features and is helpful in learning how to prepare SQL statements. It will also serve you much better if you ever write more complicated code.
http://www.php.net/manual/en/intro.pdo.php
This example uses objects rather then arrays. Doesn't necessarily matter, but it uses less characters so I like it. Difference do present themselves when you get deeper into objects, but not in this example.
//connection information
$user = "your_mysql_user";
$pass = "your_mysql_user_pass";
$dbh = new PDO('mysql:host=your_hostname;dbname=your_db;charset=UTF-8', $user, $pass);
//prepare statement to query table
$sth = $dbh->prepare("SELECT name, colour FROM fruit");
$sth->execute();
//loop over all table rows and fetch them as an object
while($result = $sth->fetch(PDO::FETCH_OBJ))
{
//print out the fruits name in this case.
print $result->name;
print("\n");
print $result->colour;
print("\n");
}
You probably also want to look into prepared statements. This helps against injection. Injection is bad for security reasons. Here is the page for that.
http://www.php.net/manual/en/pdostatement.bindparam.php
You probably should look into sanitizing your user input as well. Just a heads up and unrelated to your current situation.
Also to get all the field names with PDO try this
$q = $dbh->prepare("DESCRIBE tablename");
$q->execute();
$table_fields = $q->fetchAll(PDO::FETCH_COLUMN);
Once you have all the table fields it would be pretty easy using <div> or even a <table> to arrange them as you like using a <th>
Happy learning PHP. It is fun.
Thanks guys, got it.
$table = 'tbl_gas_meters';
$result = MYSQL_QUERY("SELECT * FROM {$table}");
$fields_num = MYSQL_NUM_FIELDS($result);
ECHO "<h1>Table: {$table}</h1>";
ECHO "<table border='1'><tr>";
// printing table headers
FOR($i=0; $i<$fields_num; $i++)
{
$field = MYSQL_FETCH_FIELD($result);
ECHO "<td>{$field->name}</td>";
}
ECHO "</tr>\n";
// printing table rows
WHILE($row = MYSQL_FETCH_ROW($result))
{
ECHO "<tr>";
// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
FOREACH($row AS $cell)
ECHO "<td>$cell</td>";
ECHO "</tr>\n";
}
while ( $row = mysql_fetch_array($field) ) {
echo $row['fieldname'];
//stuff
}
Try this :
$raw = mysql_query("SELECT * FROM tbl_gas_meters");
$allresults = mysql_fetch_array($raw);
$field = mysql_query("SELECT * FROM tbl_gas_meters");
while($row = mysql_fetch_assoc($field)){
echo $row['your field name here'];
}
Please note that, mysql_* functions are deprecated in new php version , so use mysqli or PDO instead.
Thanks! I adapted some of these answers to draw a table from all records from any table, without having to specify the field names. Just paste this into a .php file and change the connection info:
<?php
// Authentication detail for connection
$servername = "localhost";
$username = "xxxxxxxxxx";
$password = "xxxxxxxxxx";
$dbname = "xxxxxxxxxx";
$tablename = "xxxxxxxxxx";
$orderby = "1 DESC LIMIT 500"; // column # to sort & max # of records to display
// Create & check connection
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error); // quit
}
// Run query & verify success
$sql = "SELECT * FROM {$tablename} ORDER BY {$orderby}";
if ($result = $conn->query($sql)) {
$conn->close(); // Close table
$fields_num = $result->field_count;
$count_rows = $result->num_rows;
if ($count_rows == 0) {
die ("No data found in table: [" . $tablename . "]" ); //quit
}
} else {
$conn->close(); // Close table
die ("Error running SQL:<br>" . $sql ); //quit
}
// Start drawing table
echo "<!DOCTYPE html><html><head><title>{$tablename}</title>";
echo "<style> table, th, td { border: 1px solid black; border-collapse: collapse; }</style></head>";
echo "<body><span style='font-size:18px'>Table: <strong>{$tablename}</strong></span><br>";
echo "<span style='font-size:10px'>({$count_rows} records, {$fields_num} fields)</span><br>";
echo "<br><span style='font-size:10px'><table><tr>";
// Print table Field Names
while ($finfo = $result->fetch_field()) {
echo "<td><center><strong>{$finfo->name}</strong></center></td>";
}
echo "</tr>"; // Finished Field Names
/* Loop through records in object array */
while ($row = $result->fetch_row()) {
echo "<tr>"; // start data row
for( $i = 0; $i<$fields_num; $i++ ) {
echo "<td>{$row[$i]}</td>";
}
echo "</tr>"; // end data row
}
echo "</table>"; // End table
$result->close(); // Free result set
?>

Need help displaying mysql database contents to webpage

I need help displaying data from mysql to a webpage, I am coding in php.
My database consists of products which are cars(same type e.g Chevy), right now I have 2 rows (I can add more if I want to), each cars contains the image path, and description.
I can show one row (car) but I am unable to show all rows. I know I have to go through a loop to get all the data from the cars database but I am not sure how to implement it.
This is what I have so far. Assuming I already connected to my database
note: the image path I would like to show the picture in my website.
This is how i would like it to display in my webpage:
$query = "SELECT * FROM cars where cars.carType = 'Chevy' AND \
cars.active = 1";
$numberOfFieds = mysqli_num_fields($result);
$numberOfRows = mysqli_num_rows($result);
/* Gets the contents */
$row = mysqli_fetch_row($result);
$rows = mysqli_fetch_assoc($result);
$fieldcarssontable = array_keys($row);
echo "<table>";
while($row = mysqli_fetch_assoc($result)){
echo "<th>" . $fieldcarssontable[imgPath] . "</th>";
echo "<th>" . $fieldcarssontable[description] . "</th>";
}
echo "</tr>";
echo "</table>";
Just add a while loop. mysqli_fetch_assoc returns a row and moves the internal pointer to the next row until all rows are fetched, then it returns false and the while loop will stop
Pseudo syntax to understand while
while ( this is true ) {
execute this
}
So on your case you can say
while ( $row = mysqli_fetch_assoc( $result ) ) {
// process/output $row
}
mysqli_fetch_assoc and mysqli_fetch_row literally do the same, assoc gives you the array with your result field names as index so this is simpler to access ( $row['name'] rather than $row[0] when using fetch_row )
Have fun! :)
EDIT
// connect to your database server
$link = mysqli_connect('localhost', 'my_user', 'my_password', 'my_db');
// an error occured
if (!$link) {
die('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
// build your query
$query = "SELECT
* # select actual fields instead of *
FROM
cars
WHERE
cars.carType = 'Chevy'
AND
cars.active = 1";
// execute query
$result = mysqli_query($link, $query );
if ( !$result ) {
die( 'no result' );
}
// number of fields
$numberOfFields = mysqli_num_fields($result);
// the field names
$fieldNames = mysqli_fetch_fields($result);
// number of result rows
$numberOfRows = mysqli_num_rows($result);
// watch the content of fieldName and compare it with the table header in the output
print_r( $fieldName );
echo "<table>\n";
// table header, not neccessary to put this into a loop if the query isn't dynamic
// so you actually know your field names - you can echo the header without any variable.
// for the sake of learning about loops I added this
foreach( $fieldNames as $index => $fieldName ) {
echo "\t<th>field #" $index . ", name:" . $fieldName . "</th>\n";
}
// now it's time to walk through your result rows, since we only need to check for "true" a while loop does best
while ( $row = mysqli_fetch_assoc( $result ) ) {
echo "\t<tr><td>" . $row['imgPath'] . "</td><td>" . $row['description'] . "</td></tr>\n";
}
echo "</table>\n";
// remove the result from memory
mysqli_free_result( $result );
mysqli_close( $link );
You misspelled $numberOfFields in your loop, which means you're using a different variable for your loop control. Your loop won't work.
I recommend turning on the error reporting so PHP can catch this stuff for you.
Use this... just while loop
<?php
// Array
while($result = mysql_fetch_assoc($result)) {
//show you fields
echo $result["FieldName"];
}
?>
Or use this proper
<?php
// Edit it as per your query
$query = "SELECT * FROM cars";
if ($result = $mysqli->query($query)) {
/* fetch associative array */
while($row = $result->fetch_assoc()) {
//show you fields
echo $row["Name"];
}
/* free result set */
$result->free();
}
/* close connection */
$mysqli->close();
?>

Categories