I'm printing all the data from one of the tables in my database using by using the mysql_fetch_query() to make an array and then the while function to print it.
The only thing is, it is skipping the first record and going straight to printing record 2. Record 1 does exist in the table, and is named as such.
Is there something I'm missing or need to add?
mysql_connect("$host", "$username", "$db_password")or die("cannot connect");
$data=mysql_query("SELECT * FROM users")or die(mysql_error());
$info=mysql_fetch_array($data);
while($info = mysql_fetch_array($data)){
echo "<br />";
echo "Record id: <strong>" . $info['id'] . "</strong>";
echo "<br />";
echo "Visit time and date: <strong>" . $info['visitDate'] . "</strong>";
echo "<br />";
echo "Previous destination: <strong>" . $info['cameFrom'] . "</strong>";
echo "<br />";
echo "Browser used: <strong>" . $info['browser'] . "</strong>";
echo "<br />";
echo "Location of user: <strong>" . $info['location'] . "</strong>";
echo "<p> </p>";
}
remove this line before while loop:
$info=mysql_fetch_array($data);
This will fetch the first record, and when you start while loop is starts from second row because you call mysql_fetch_array() again.
$info=mysql_fetch_array($data);
while($info = mysql_fetch_array($data)){
Leave only the condition, because you make one fetch before the loop, which is the first skipper row
you have called mysql_fetch_array() two times
**$info=mysql_fetch_array($data);
while($info = mysql_fetch_array($data))**
so your first record is already fetched with first call to mysql_fetch_array()
just remove first call and it will work ok.
Related
* if(!empty) retrieves first entry in array and shouldn't be used *
This script searches a database for a url that matches the one submitted by a user in a form. Currently the $query printed by the echo statement works fine in mysql and returns one entry. This script executes and prints the table header, but doesn't enter the while($row =.. ) loop.
Any thoughts or suggestions would be really appreciated. I've used this method before with no trouble so I'm sort of stumped right now.
//1. Query DB for results
$query = "SELECT * FROM projects WHERE projectsurl='".$url."';";
echo "<br>".$query;
$projects = mysqli_query($conn, $query);
// 2. Print Project Results
if(!empty(mysqli_fetch_assoc($projects))){
//Print Project Results
echo "Is this your project?<br>";
echo "<table style=width:'100%'>";
while ($row = mysqli_fetch_assoc($projects)){
echo $tabler . $thh . "Title: <br>" .$row['title'] . $thsp . "No. Rewards: <br>" . $row['rewards'] . $thf . $xtabler;
echo $tabler . $thh . "ID: " .$row['id'] . $thf . $xtabler;
// Echo two rows for the URL Strings because they are longer.
echo $tabler . $thh . "<a href='" . $row['projectsurl'] . "'>Projects</a>" . $thsp . "<a href='" . $row['rewardsurl'] . "'> Rewards " . $thf . $xtabler;
echo "<form id='confirmation' action='../index.php' method='POST'>
<input type='hidden' value='2' name = 'stage'>
<input type='hidden' value='".$row['id']."' name='id'>
<input type='hidden' value='".$row['title']."' name='title'>
<input type='submit' value='Confirm'></form>";
}
echo "</table>";
}else{
//trigger ruby script to search for lost file
echo "Project Not Found. <br>";
}
Oh and the random table stuff is defined elsewhere as
$tabler = "<tr>";
$xtabler = "</tr>";
$thh = "<th><b>";
$thsp = "</th><th>";
$thf = "</b></th>";
$csp = "</td><td>";
$ch = "<td>";
$cf = "</td>";
If there's only one row, then if(!empty(mysqli_fetch_assoc($projects))){ will fetch it and your while loop will not be entered. Instead, to check if a row exists, use mysqli_num_rows($projects).
Also, Reminder, escape your user submitted data before using it in your MySQL query if you haven't already. This can be done by: $query = "SELECT * FROM projects WHERE projectsurl='".mysqli_real_escape_string($conn, $url)."';";
EDIT: Alternatively, prepare your MySQL statements before executing them. This is the preferred method, although both protect you from injections.
I'm currently trying to put php results into an HTML table, and not sure why it is not working. The first block returns the data I need, so I know it's not a sql query issue. The second block (where I try to make it a table) returns the headers but no data.
Here is my code:
<h1>
<?php
while (odbc_fetch_row($weekadmissions)) // while there are rows
{
print odbc_result($weekadmissions, "date") . ", " .
odbc_result($weekadmissions, "attendance") .
"<br />\n";
}
?>
</h1>
<?php
print "<table><tr>";
print "<th>Date</th>";
print "<th>Attendance</th></tr>";
while (odbc_fetch_row($weekadmissions)) // while there are rows
{
print "<tr><td>" . odbc_result($weekadmissions,"date") . "</td>";
print "<td>" . odbc_result($weekadmissions, "attendance") . "</td></tr>";
}
print "</table>";
?>
Thanks!
The answer was that the result was consumed by the first loop - thank you FirstOne for the solution
I am trying to add three different css formats to data pulled from MySQL database.
I have formatted the first row fine but I cant get the other two working at all,
any ideas what I am doing wrong?
//run the query
$loop = mysql_query("SELECT * FROM employee")
or die (mysql_error());
while ($row = mysql_fetch_array($loop))
{
echo "<p style='color:#0000ff; font-size:22px'>".$row ['employee_name']."</p>" "<p style='color:#00000f; font-size:32px'>".$row['employee_id'] . "</p>" "<p style='color:#000000; font-size:42px'>". $row['employee_contact'] . " </p>" . "<br/>";
}
You have an unecessary gap:
$row['employee_id'] . "</p>" "<p style='color:#000000; font-size:42px'>"
^^^^^
here
Either get rid of the quotes or add a concatenation operator.
$row['employee_id'] . "</p> <p style='color:#000000; font-size:42px'>"
Your quotes are not balanced. Try this :
echo "<p style='color:#0000ff; font-size:22px'>".$row ['employee_name']."</p> <p style='color:#00000f; font-size:32px'>".$row['employee_id'] . "</p><p style='color:#000000; font-size:42px'>". $row['employee_contact'] . " </p> <br/>";
i have simple db that it is filled by a form from a page, all is ok in the entry.
i have 2 fields in the database are: all_students and current_students filled by the user in the form that i want to calculate
the trick is that i am echoing only the latest db record in the output page.. to give me only the latest data inserted in the form...
now, i want to create a new field that give me the absent students automatically (all - current)
what i have tried, i read that i can NOT create a new calculated field in the db, it is not an excel, so i am trying to echo the calculation results of these 2 fields, to a new value that is the "absent students"
what you suggest? please help, here is my code
<?php
$con=mysqli_connect("localhost","root","PasswordHere","DBnameHere");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT id, class, all_students, current_students FROM students ORDER BY id DESC LIMIT 1");
while($row = mysqli_fetch_array($result))
{
echo "Class: " . $row['class'] . "<br>";
echo "All students: " . $row['all_studnets'] . "<br>";
echo "Current students: " . $row['current_studnets'] . "<br>";
echo "Absent students: " . $row['all_studnets'] . - . $row['current_studnets'] . " <br>";
}
mysqli_close($con);
?>
You only put dot (.) if you want to concatenate a string. Please remove the dot since you are subtracting.
echo "Absent students: ";
echo floatval($row['all_studnets']) - floatval($row['current_studnets']);
echo "<br>";
Try this line
echo "Absent students: " . $row['all_studnets'] - $row['current_studnets'] . " <br>";
Insted of this
echo "Absent students: " . $row['all_studnets'] . - . $row['current_studnets'] . " <br>";
Hi I have the details of two car drivers getting pulled from a database but then I want to just grab one of the two and echo it out in php. I am able to take both drivers details and echo them both out using a while loop but not target just one of them:
By using the code below I echo out both records. Is there a way to just grab one of them?
while($row = mysql_fetch_array($result))
{
echo "A driver:<br />";
echo "Name:". $row['FirstName'] . " " . $row['LastName']."<br />";
echo "Latitude:". $row['CoordLat']."<br />";
echo "Longitude:". $row['CoordLong'];
$driverlat = $row['CoordLat'];
$driverlong = $row['CoordLong'];
}//end of while loop
Just remove the while loop ?
$row = mysql_fetch_array($result))
echo "A driver:<br />";
echo "Name:". $row['FirstName'] . " " . $row['LastName']."<br />";
echo "Latitude:". $row['CoordLat']."<br />";
echo "Longitude:". $row['CoordLong'];
$driverlat = $row['CoordLat'];
$driverlong = $row['CoordLong'];
You don't need a while loop at all.
Just do $row = mysql_fetch_array($result); on it's own separate line.
This will display the first row result.
You can just put a break; at the end of the while loop. Then only the first row is processed.
Better way would be to use no loop: Just assign the $row and do the render stuff.