Why my while loop not working, I also have a while loop on the other PHP page, but there's only one page that doesn't work with PHP's while loop. But it does not contain any errors. Here's my code:
$sqlquery = "SELECT * FROM tbl_accredited";
$result = $con->query($sqlquery);
$num = mysqli_fetch_array($result);
if($num <= 0){
echo "<h2>No records found.</h2>";
}
$x=0;
while($row = mysqli_fetch_assoc($result)){
$x++;
echo '
<tr>
<td>'.$x.'</td>
<td>'.$row['permitno'].'</td>
<td>'.$row['boarding_optr'].'</td>
<td>'.$row['boarding_addr'].'</td>
<td>'.$row['orno'].'</td>
<td>'.$row['boarding_name'].'</td>
</tr>
';
}
You were reading your first result row and incorrectly using that as a count of resulted rows, then ignoring its content.
$sqlquery = "SELECT * FROM tbl_accredited";
$result = $con->query($sqlquery);
$num = mysqli_fetch_array($result);
// this reads the first row of your result set and then of course gets lost
//$num = mysqli_fetch_array($result);
// use mysqli_num_rows instead
if(mysqli_num_rows($result) <= 0){
echo "<h2>No records found.</h2>";
} else {
$x=0;
// now this will get the first row, which you must have been missing before
while($row = mysqli_fetch_assoc($result)){
$x++;
echo '
<tr>
<td>'.$x.'</td>
<td>'.$row['permitno'].'</td>
<td>'.$row['boarding_optr'].'</td>
<td>'.$row['boarding_addr'].'</td>
<td>'.$row['orno'].'</td>
<td>'.$row['boarding_name'].'</td>
</tr>
';
}
}
$sqlquery = "SELECT * FROM tbl_accredited";
$result = $con->query($sqlquery);
$x=0;
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$x++;
echo '
<tr>
<td>'.$x.'</td>
<td>'.$row['permitno'].'</td>
<td>'.$row['boarding_optr'].'</td>
<td>'.$row['boarding_addr'].'</td>
<td>'.$row['orno'].'</td>
<td>'.$row['boarding_name'].'</td>
</tr>
';
}
}
else {
echo "<h2>No records found.</h2>";
}
Related
I'm trying to make an html table with the data in the database but when I write echo it just shows me a result. How can I echo all the elements?
$sql = "SELECT * FROM app_spot where company_id='$company_id'";
$result = mysqli_query($db, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_array($result)) {
$spot_id = $row["id"];
$spot_name = $row["spot_name"];
$store_location = $row["store_location"];
$spot_budget = $row["spot_budget"];
$spot_status = $row["spot_status"];
}
$spotcount = mysqli_num_rows($result);
} else {
echo "0 results";
}
Echo the table rows in the while loop.
if (mysqli_num_rows($result) > 0) {
echo "<table><tr><th>ID</th><th>Name</th><th>Location</th><th>Budget</th><th>Status</th></tr>";
while($row = mysqli_fetch_array($result)) {
$spot_id = $row["id"];
$spot_name = $row["spot_name"];
$store_location = $row["store_location"];
$spot_budget = $row["spot_budget"];
$spot_status = $row["spot_status"];
echo "<tr><td>$spot_id</td><td>$spot_name</td><td>$store_location</td><td>$spot_budget</td><td>$spot_status</td></tr>";
}
echo "</table>";
}
I have this small code to display 200 rows and I want to give each row a number a per the no of votes.
How can I do it?
This is what I thought of but it does not work.
$sql = 'SELECT * FROM table LIMIT 200;
$result = mysqli_query($conn, $sql);
while($row = mysqli_fetch_assoc($result)) {
$row = 1;
echo $row++;
echo $row['fullname'];
echo '<br>';
}
the output is making all row output to 1.
edit :
okay so some of the answers did the job but it does not work on the pagination. it counts from 1 on page 2,3,4,5.... heres the code.
<?php
$sql = "SELECT * FROM table";
$result = mysqli_query($conn, $sql);
$result_per_page = 20;
$no_of_result = mysqli_num_rows($result);
$no_of_page = ceil($no_of_result/$result_per_page);
if(!isset($_GET['page'])){
$page = 1;
}else{
$page = $_GET['page'];
}
$page_first = ($page-1)*$result_per_page;
$sql = 'SELECT * FROM table LIMIT ' . $page_first . ',' . $result_per_page;
$result = mysqli_query($conn, $sql);
$num = 1;
while($row = mysqli_fetch_assoc($result)) {
echo $num++;
echo $row['fullname'];
echo '<br>';
}
for($page=1;$page<=$no_of_page;$page++){
echo ''.$page.'';
echo ' ';
}
?>
Try to read your code line by line and see what it does.
while($row = mysqli_fetch_assoc($result)) { # > Set variable $row to the next result set or exit when there are none.
$row = 1; # Set variable $row to value 1.
echo $row++; # Increment variable $row to + 1.
echo $row['fullname']; # Row is now an integer, not an array. (You did that 2 statements ago)
echo '<br>'; #
} // end of loop, start again --------------|
A solution would be:
$id = 1;
while($row = mysqli_fetch_assoc($result)) {
echo $id++;
echo $row['fullname'];
echo '<br>';
}
As for the page, you're overwriting the previous value:
for($s=$page;$s<=$no_of_page;$s++){
echo ''.$s.'';
echo ' ';
}
I currently have this code set up:
$sql = "SELECT * FROM homework WHERE class = '$class'";
$result = mysqli_query($conn, $sql);
$data_exist = false;
if (mysqli_num_rows($result) > 0) {
// output data of each row
$data_exist = true;
while($row = mysqli_fetch_assoc($result)) {
$id = $row["id"];
$teacher_set = $row["teacher_set"];
$class = $row["class"];
$name = $row["name"];
$description = $row["description"];
}
}
And then:
<?php if ($data_exist){?>
<p><?php echo $id ?></p>
<p><?php echo $teacher_set?></p>
<p><?php echo $name?></p>
<p><?php echo $description?></p>
<?php
}?>
However, the issue is if there is multiple results in the database it only outputs one of them, how can I prevent this from happening and output two?
I want to make it so every row has their own section, like this: http://prntscr.com/hcgtqn so if there is only one result, one one will show etc.
You have to echo data in a loop. Right now you are reassigning values in while($row = mysqli_fetch_assoc($result)) iterations and printing just the last one.
You need to print each time you read a row from the database.
about the styles, you can represent it in many ways. In the code below I present it in a table.
<table>
<thead>
<tr>
<th>id</th>
<th>teacher set</th>
<th>name</th>
<th>description</th>
</tr>
</thead>
<tbody>
<?php
$sql = "SELECT * FROM homework WHERE class = '$class'";
$result = mysqli_query($conn, $sql);
$data_exist = false;
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_array($result)) {
$id = $row["id"];
$teacher_set = $row["teacher_set"];
$class = $row["class"];
$name = $row["name"];
$description = $row["description"];
// you need to print the output now otherwise you will miss the row!
// now printing
echo "
<tr>
<td>".$id."</td>
<td>".$teacher_set."</td>
<td>".$name."</td>
<td>".$description."</td>
</tr>";
}
}
else // no records in the database
{
echo "not found!";
}
?>
</tbody>
</table>
</body>
</html>
Hi I'm attempting to display data retrieved from a mysql table horizontally in an html table using php. The code below works well except for the fact that it leaves out the first record (starts at the second record) in my database. I'm sure it has something to do with the counter but I can't seem to figure out how to get it to stop doing this. If anyone can point out my error I'd really appreciate it!
$items = 5;
$query = "SELECT * FROM members ";
$result = mysql_query($query)
or die(mysql_error());
$row = mysql_fetch_array($result);
if (mysql_num_rows($result) > 0) {
echo '<table border="1">';
$i = 0;
while($row = mysql_fetch_array($result)){
$first_name = $row['first_name'];
if ($i==0) {
echo "<tr>\n";
}
echo "\t<td align=\center\">$first_name</td>\n";
$i++;
if ($i == $items) {
echo "</tr>\n";
$i = 0;
}
}//end while loop
if ($i > 0) {
for (;$i < $items; $i++) {
echo "<td> </td>\n";
}
echo '</tr>';
}//end ($i>0) if
echo '</table>';
}else {
echo 'no records found';
}
try and remove the 1st
$row = mysql_fetch_array($result);
you are calling it twice, that's why it skips 1 row in your while loop
try this simpler.
$items = 5;
$query = "SELECT * FROM members ";
$result = mysql_query($query) or die(mysql_error());
if (mysql_num_rows($result) > 0) {
echo '<table border="1">';
while($row = mysql_fetch_array($result)){
$first_name = $row['first_name'];
echo "<tr>";
for ($i=0 ; $i <= $items ;$i++) {
echo "<td align='center'>".$first_name."</td>";
}
}//end while loop
echo "</tr>";
echo '</table>';
}else{ echo 'no records found'; }
I have run into this issue before. Try the do while loop instead. Example
do {
// code
} while($row = mysql_fetch_array($result)); //end while loop
$row = mysql_fetch_array($result);
1) remove this line of code from ur scripts
2) only use while loop code instead.
The query I have below will only show me one result even if there are multiple matching entries (completely or partially matching). How do I fix it so it will return all matching entries:
//$allowed is a variable from database.
$sql = "SELECT `users`.`full_name`, `taglines`.`name`, `users`.`user_id` FROM
`users` LEFT JOIN `taglines` ON `users`.`user_id` = `taglines`.`person_id`
WHERE ( `users`.`user_settings` = '$allowed' ) and ( `users`.`full_name`
LIKE '%$q%' ) LIMIT $startrow, 15";
$result = mysql_query($sql);
$query = mysql_query($sql) or die ("Error: ".mysql_error());
$num_rows1 = mysql_num_rows($result);
if ($result == "")
{
echo "";
}
echo "";
$rows = mysql_num_rows($result);
if($rows == 0)
{
}
elseif($rows > 0)
{
while($row = mysql_fetch_array($query))
{
$person = htmlspecialchars($row['full_name']);
}
}
}
print $person;
Because your overwriting $person on each iteration.
Hold it in a $person[] array if your expecting more then one. Then loop through it with a foreach loop when you intend to output.
Not related but your also querying twice, you only need 1 $result = mysql_query($sql);
Update (Simple Outputting Example):
<?php
$person=array();
while($row = mysql_fetch_array($query)){
$person[] = array('full_name'=>$row['full_name'],
'email'=>$row['email'],
'somthing_else1'=>$row['some_other_column']);
}
//Then when you want to output:
foreach($person as $value){
echo '<p>Name:'.htmlentities($value['full_name']).'</p>';
echo '<p>Eamil:'.htmlentities($value['email']).'</p>';
echo '<p>FooBar:'.htmlentities($value['somthing_else1']).'</p>';
}
?>
Or an alternative way to is to build your output within the loop using concatenation.
<?php
$person='';
while($row = mysql_fetch_array($query)){
$person .= '<p>Name:'.$row['full_name'].'</p>';
$person .= '<p>Email:'.$row['email'].'</p>';
}
echo $person;
?>
Or just echo it.
<?php
while($row = mysql_fetch_array($query)){
echo '<p>Name:'.$row['full_name'].'</p>';
echo '<p>Email:'.$row['email'].'</p>';
}
?>