How to read a table using a while loop

crazyal

Maddy
Hi All,
I'm trying to figure out how to read in a table of records. Table is indexed with a key but I don't need to access it by key, but rather sequentially and store all the fields of each record in one string. It's a static table of only 17 -20 records so not much data,
Here is what I have but I can't figure out how to get it to loop thru all the records. All I get back is one line of 5 commas
I even have 4 lines to find the count of records as I tried using a FOR loop but I couldn't figure out how to do that. Any help is much appreciated.
So just to clarify I want ALL the records sent out in one CSV string.

Thank you

My apologies for the lines not being indented. The editor is pushing everything left

else if ($functionflag == 'LEVELREQ')
{
$query = mysqli_query($conn, "SELECT Level.Key_ID FROM Level WHERE Level.Key_ID='$Key_ID'");
$numrows = mysqli_num_rows($query);

$sql="select count(*) as total from Level";
$result=mysqli_query($conn,$sql);
$data=mysqli_fetch_assoc($result);
echo $data['total'];


if ($numrows)
{
$query = mysqli_query($conn, "SELECT
Level.Key_ID,
Level.Lvl_Desc,
Level.Lvl_Name,
Level.Lvl_Lower,
Level.Lvl_Upper
FROM Level
WHERE Level.Key_ID = 'Key_ID'");

// $row = mysqli_fetch_row($query);
$message = "LEVELSEND";
echo "$message";

while ($row = mysqli_fetch_array($query));
{
$Key_ID = $row['Key_ID'];

echo ",",$row[0],",",$row[1],",",$row[2],",",$row[3],",",$row[4];
}
}
}
 
Hi All,
I'm trying to figure out how to read in a table of records. Table is indexed with a key but I don't need to access it by key, but rather sequentially and store all the fields of each record in one string. It's a static table of only 17 -20 records so not much data,
Here is what I have but I can't figure out how to get it to loop thru all the records. All I get back is one line of 5 commas
I even have 4 lines to find the count of records as I tried using a FOR loop but I couldn't figure out how to do that. Any help is much appreciated.
So just to clarify I want ALL the records sent out in one CSV string.

Thank you

My apologies for the lines not being indented. The editor is pushing everything left

else if ($functionflag == 'LEVELREQ')
{
$query = mysqli_query($conn, "SELECT Level.Key_ID FROM Level WHERE Level.Key_ID='$Key_ID'");
$numrows = mysqli_num_rows($query);

$sql="select count(*) as total from Level";
$result=mysqli_query($conn,$sql);
$data=mysqli_fetch_assoc($result);
echo $data['total'];


if ($numrows)
{
$query = mysqli_query($conn, "SELECT
Level.Key_ID,
Level.Lvl_Desc,
Level.Lvl_Name,
Level.Lvl_Lower,
Level.Lvl_Upper
FROM Level
WHERE Level.Key_ID = 'Key_ID'");

// $row = mysqli_fetch_row($query);
$message = "LEVELSEND";
echo "$message";

while ($row = mysqli_fetch_array($query));
{
$Key_ID = $row['Key_ID'];

echo ",",$row[0],",",$row[1],",",$row[2],",",$row[3],",",$row[4];
}
}
}
OK SO GOOD NEWS! Found a few samples with, one could say, a better or more current format that worked perfectly. Here is a clip of my new while loop in case it helps anyone in the same predicament as myself



$sql = $conn->query("SELECT * FROM Level");
$product_count = mysqli_num_rows ($sql);
if ($product_count > 0)
{
while($row = $sql->fetch_assoc())
{
echo ",",$row['Key_ID'],",",$row['Lvl_Desc'],",",$row['Lvl_Name'],",",$row['Lvl_Lower'],",",$row['Lvl_Upper'];
}
}

 
Back
Top