I am using a 'While' loop to run through an array of users' first names, last names, and emails. When my 'While' loop executes, and I echo the data, it successfully shows the row data from the database, but it does not show Row 1. It starts at row 2 and spits everything out properly, but why is it skipping row 1?
$query= "SELECT * FROM email_list";
$result=mysqli_query($dbc, $query);
$row=mysqli_fetch_array($result);
while ($row=mysqli_fetch_array($result)) {
echo $row['first_name'] . ' ' . $row['last_name'] . ' : '
. $row['email'] . '<br />';
}
mysqli_close($dbc);
One thing I have found, is if I put this section:
$row=mysqli_fetch_array($result);
echo $row['first_name'] . ' ' . $row['last_name'] . ' : '
. $row['email'] . '<br />';
above the 'While' loop, then it does successfully show the first row of data, and all the other rows too, so, if that's confusing, coding it like this (the long way - below) actually works, but it seems redundant and I want to use best DRY practices:
$query= "SELECT * FROM email_list";
$result=mysqli_query($dbc, $query);
$row=mysqli_fetch_array($result);
echo $row['first_name'] . ' ' . $row['last_name'] . ' : '
. $row['email'] . '<br />';
while ($row=mysqli_fetch_array($result)) {
echo $row['first_name'] . ' ' . $row['last_name'] . ' : '
. $row['email'] . '<br />';
}
mysqli_close($dbc);
You have 2 calls to $row=mysqli_fetch_array($result); before you print your result. One before the loop and the other as the expression in the while statement.
Try deleting the one before the while loop.
$result=mysqli_query($dbc, $query);
// kill this line $row=mysqli_fetch_array($result);
while ($row=mysqli_fetch_array($result)) {
echo $row['first_name'] . ' ' . $row['last_name'] . ' : '
. $row['email'] . '<br />';
}
Related
I am new to HTML CSS, I have tried many time but fail. Please help.
I want to create table like this:
I have inserted relevant data in mysql but i am unable to show data in given format/table.
<tbody>
<?php
foreach($record as $row)
{
$pre_stop = $row['pre_stop'];
$current_stop_id = $row['current_stop'];
$cur_stop_name=mysql_query("SELECT stop_name_urd FROM stop WHERE stop_id = '$current_stop_id'");
while($cur_stop=mysql_fetch_array($cur_stop_name))
{
$stop_name=$cur_stop[0];
}
$inter_distance = $row['inter_distance'];
$total_distance += $row['inter_distance'];
echo "<tr>";
echo "<td class='border' style='text-align:center;'>" . $inter_distance . "</td>";
echo "<td class='border' style='text-align:center;'>" . $stop_name . "</td>";
echo "<td class='border' style='text-align:center;'>".$total_distance."</td>";
for ($i=0; $i < 1; $i++) {
echo "<td rowspan='".$count."' class='noBorder' style='text-align:center;'>" . "" . "</td>";
echo "<td class='noBorder'>" . $stop_name . "</td>";
}
}
//echo "<td class='noBorder' style='text-align:center;'>" . $stop_name . "</td>";
//echo "<td></td>";
?>
</tr>
</tbody>
If you're new to coding HTML, then I recommend you try to manually create a smaller version of your table (say 3 rows) so that you know how the resulting HTML code is supposed to look and that you're fetching the SQL data correctly. Then you can transfer that knowledge into PHP code.
Do watch out for your loop, though:
for ($i=0; $i < 1; $i++) {
echo "<td rowspan='".$count."' class='noBorder' style='text-align:center;'>" . "" . "</td>";
echo "<td class='noBorder'>" . $stop_name . "</td>";
}
This will only loop once, which seems to defeat your purpose, as your logical check is odd. Your for loop reads:
$i=0 - Make a variable named i
$i < 1 - Stop if i ever becomes
greater than or equal to 1
$i++ - Add 1 to the value of i
This means that it will go through the loop one time only and then break out of the loop.
I don't see from your code example how you're getting your data but I imagine you'd want something like this:
for ($i=0; $i < $resultCount; $i++) {
echo "<td rowspan='".$count."' class='noBorder' style='text-align:center;'>" . "" . "</td>";
echo "<td class='noBorder'>" . $stop_name . "</td>";
}
Where $resultCount is equal to the number of rows returned by your SQL query.
It looks like you reversed the order of the columns when echo-ing out your html. Also, it would be easier to use colspan vs rowspan in this approach. Not knowing the initial value of count, we shall assume the header is 0 and all other rows are 1 through m (where m is the number of rows since we read a matrix as m x n (rows x columns)).
In conclusion, try this:
$count = 0;
$total_distance = 0;
// the header row
echo "<tr>" . PHP_EOL;
// Apply the space on the left
echo "\t<th colspan='" . (count($record) - $count) . "' class='noBorder' style='text-align:right;'></th>" . PHP_EOL;
// Provide headers on the last three columns
echo "\t<th class='border' style='text-align:center;'>Total Distance</th>" . PHP_EOL;
echo "\t<th class='border' style='text-align:center;'>Stop Name</th>" . PHP_EOL;
echo "\t<th class='border' style='text-align:center;'>Inter-Distance</th>" . PHP_EOL;
echo "</tr>" . PHP_EOL;
foreach($record as $row)
{
// get the values
$pre_stop = $row['pre_stop'];
$current_stop_id = $row['current_stop'];
$inter_distance = $row['inter_distance'];
$total_distance += $inter_distance;
// Assume the last stop in the results has the desired stop name
$cur_stop_name=mysql_query("SELECT stop_name_urd FROM stop WHERE stop_id = '$current_stop_id'");
while($cur_stop=mysql_fetch_array($cur_stop_name))
{
$stop_name = $cur_stop[0];
}
// Start the row
echo "<tr>" . PHP_EOL;
// Apply the space on the left
echo "\t<td colspan='" . (count($record) - $count) . "' class='noBorder' style='text-align:right;'>" . $stop_name . "</td>" . PHP_EOL;
// Apply the last three columns
echo "\t<td class='border' style='text-align:center;'>".$total_distance."</td>" . PHP_EOL;
echo "\t<td class='border' style='text-align:center;'>" . $stop_name . "</td>" . PHP_EOL;
echo "\t<td class='border' style='text-align:center;'>" . $inter_distance . "</td>" . PHP_EOL;
// End the row
echo "</tr>" . PHP_EOL;
// increment the count
$count++;
}
$minTime = "Select MIN(categoryTime) FROM List WHERE personStatus='Atendiendo' AND depName='Admisiones'";
$Time = mysqli_query($con,$minTime);
echo "<table>
<tr>
<th>Main Record</th>
<th>Tiempo Categoria</th>
<th>Tiempo Estimando</th>
<th>Time IN </th>
</tr>";
while($min = mysqli_fetch_array($Time))
{
echo "<tr>";
echo "<td>" . $min['categoryTime'] . "</td>";
echo "<td>" . $min['categoryTime'] . "</td>";
echo "<td>" . $min['categoryTime'] . "</td>";
echo "<td>" . $min['categoryTime'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
If I delete the MIN() function the query works perfectly...
The query work if I delete the Min() fuction. I tried Least() with no success.
Several issues here.
I think the snippet is missing:
$Time =mysqli_query($con,$minTime);
where $con is the connection.
When you add the MIN function around the categoryTime field, the label of the column in the resultset changes, so the reference to $min['categoryTime'] no longer returns anything. Try changing query to
SELECT MIN(categoryTime) as minCat ...
then change the php array column reference to $min['minCat']
HTH
Hi I have a code that print out some message if the data are successfully inserted into database.
This is the code:
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "Record saved!";
How do I make it so that instead of opening a new page and print them, I need them to just display a message box indicating whether it is successful or not.
Thank you.
Try this:
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
echo '<script language="javascript">';
echo 'alert("Record saving failed")';
echo '</script>';
}else{
echo '<script language="javascript">';
echo 'alert("Record saved")';
echo '</script>';
}
if (!mysql_query($sql,$con)){
echo "<script type='text/javascript'>alert('failed!')</script>";
}else{
echo "<script type='text/javascript'>alert('submitted successfully!')</script>";
}
Use this to get a message box.
I had created a table in mysql database. I am using php One column was for images, but it only stored images name. I wanted to print the whole table so did the coding but got got only the name of the images in the table.
Please explain how can I display images on the webpage that are stored in my PC. I don't want to save images in the database.
I used this code to print the table :
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("test", $con);
$query="SELECT * FROM images LIMIT 0,5";
$result = #mysql_query($query);
echo "<table border='1'>
<tr>
<th>S.No.</th>
<th>Image</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['image_id'] . "</td>";
echo "<td>" . $row['filename'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
Hope you got some answer....
All your code is doing is sending the filename to the screen as a string. You need to the use image tags.
You may find this page useful, http://www.htmlcodetutorial.com/images/_IMG.html
I made a function to print all the comments from a id-page. But I want to protect my site from being hacked with htmlspecialchars. So I put them arround my post that will be printed. The problem is that it isn't working? I can do whatever I want with the <>-signs.
CODE FUNCTION
public function GetAllComments($id)
{
$db = new Db();
$select = "SELECT * FROM tblcomments WHERE bug_id =" . $id . " ORDER BY comment_id DESC";
$result = $db->conn->query($select);
while($row = mysqli_fetch_assoc($result))
{
echo "<li class='description'>" . htmlspecialchars($row["comment_text"]) . "</li>";
echo "<li class='user'>" . $row['name'] . "</li>";
}
}
CODE PRINTING
if(isset($bugs)){
foreach ($bugs as $bug) {
echo " " .
$bug['bug_title'] . "" .
$bug['bug_status'] . "From:
" . $bug['name'] . " To: " .
$bug['bug_to'] . " Project: " .
$bug['project_title'] . "";
}
}
depends on what you want to save from you should use htmlspecialchars like this
htmlspecialchars(trim($variable), ENT_QUOTES);