When I select some data from my database (MySQL), it shows all the rows except the first one. How to fix it?
Code
// Create connection
$conn = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT id, title, image, description, tekst, address, website, telephone, email, openinghours FROM ExperienceUtrecht";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "<table border='1'> <tr> <th>ID</th> <th>Title</th> <th>Image</th> <th>Description</th> <th>Tekst</th> <th>Address</th> <th>Website</th> <th>Telephone</th> <th>Email</th> <th>Opening Hours</th> </tr>"; while($row = mysqli_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['id'] . "</td>"; echo "<td>" . $row['title'] . "</td>"; echo "<td>" . $row['image'] . "</td>"; echo "<td>" . $row['description'] . "</td>"; echo "<td>" . $row['tekst'] . "</td>"; echo "<td>" . $row['address'] . "</td>"; echo "<td>" . $row['website'] . "</td>"; echo "<td>" . $row['telephone'] . "</td>"; echo "<td>" . $row['email'] . "</td>"; echo "<td>" . $row['openinghours'] . "</td>"; echo "</tr>"; } echo "</table>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
?>
You have two while loops as #user2983401 pointed out. Get rid of the first one:
// Create connection
$conn = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT id, title, image, description, tekst, address, website, telephone, email, openinghours FROM ExperienceUtrecht";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
/*while($row = mysqli_fetch_assoc($result)) { Not needed*/
echo "<table border='1'> <tr> <th>ID</th> <th>Title</th> <th>Image</th> <th>Description</th> <th>Tekst</th> <th>Address</th> <th>Website</th> <th>Telephone</th> <th>Email</th> <th>Opening Hours</th> </tr>";
while($row = mysqli_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['id'] . "</td>"; echo "<td>" . $row['title'] . "</td>"; echo "<td>" . $row['image'] . "</td>"; echo "<td>" . $row['description'] . "</td>"; echo "<td>" . $row['tekst'] . "</td>"; echo "<td>" . $row['address'] . "</td>"; echo "<td>" . $row['website'] . "</td>"; echo "<td>" . $row['telephone'] . "</td>"; echo "<td>" . $row['email'] . "</td>"; echo "<td>" . $row['openinghours'] . "</td>"; echo "</tr>"; } echo "</table>";
/*} Not needed*/
} else {
echo "0 results";
}
mysqli_close($conn);
?>
You have two while loops. The first loop has moved The cursor to the first record. The second while loop moves the cursor again.
Suggestion: Move the the table tag and table heads directly into the If and remove the first loop.
//it will work. your logics was good but unfortunately you did just simple mistake. you used two while loops to fetch same record.
//first while loop fetch first record and after inner loop run, and second record and continue record fetch and you tried to print this record.
Related
this is my php code for sql query its giving me same value multi time
if (isset($_GET["q"]))
{
$q= $_GET["q"];
$link=mysqli_connect("localhost","root","");
mysqli_select_db($link,"onesports");
$result=mysqli_query($link,"select players.full_name, team.* from players,team where team.team_name='" .$q. " ' and players.team_name=team.team_name ");
echo "<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<a href=>" . $row['full_name'] . "</a>";
echo "<td>" . $row['image'] . "</td>";
echo "<td>" . $row['team_name'] . "</td>";
echo "<td>" . $row['prov'] . "</td>";
echo "<td>" . $row['city'] . "</td>";
echo "<td>" . $row['captain'] . "</td>";
echo "<td>" . $row['coach'] . "</td>";
echo "</tr>";
}
echo "</table>";
}
else {
echo "no result found.";
}
its giving me this output
enter image description here
i want only player name and remaining value want for one time
You should use INNER JOIN in this case.
just update your code with below code.
$result = mysqli_query($link, "SELECT `players`.full_name,`team`.* FROM `players` INNER JOIN `team` ON `players`.team_name= `team`.team_name AND `team`.team_name='".$q."'");
i wanted to ask I would I add up the points of all the drivers in the table produced below by the following code as this only displays points of all the drivers in each row ... There is a table Points which has points, position and a table Drivers having forename, surname.
<?php
session_start();
if ( !isset($_SESSION['loggedin']) ) {
header("Location: login.php");
}
$con=mysqli_connect("xxxxx","xxxxx","xxxxx","xxxx");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT Drivers.forename, Drivers.surname, Teams.name, Teams.engine, Points.points
From Drivers, Teams, Points");
echo "<table border='1'>
<tr>
<th>First name</th>
<th>Last name</th>
<th>Team Name</th>
<th>Engine</th>
<th>Points</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['forename'] . "</td>";
echo "<td>" . $row['surname'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['engine'] . "</td>";
echo "<td>" . $row['points'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
in your code, before the $while
$counter=0
while($row = mysqli_fetch_array($result))
{
$counter += $row['points'];
echo "<tr>";
echo "<td>" . $row['forename'] . "</td>";
echo "<td>" . $row['surname'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['engine'] . "</td>";
echo "<td>" . $row['points'] . "</td>";
echo "</tr>";
}
echo $counter; //echoes sum of points
<?php
// Create connection
$con=mysqli_connect("localhost","root","root");
mysql_select_db("test") or die( "Unable to select database");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//***************************** table 1 detail ***********
$query = "SELECT * FROM fraud WHERE a_number=" . $_GET["aNun_id"];
echo "<title>" . $_GET["aNun_id"] . "</title>";
echo "<table width='50%' border='5'>
<tr>
<th>Date & Time</th>
<th>A Number</th>
<th>B Number</th>
<th>Status</th>
</tr>";
$result=mysql_query($query) or die (mysql_error());
while($row=mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['a_number'] . "</td>";
echo "<td><a href='?bNun_id=".$row['b_number']."'>" . $row['b_number'] . "</a></td>";
echo "<td>" . $row['status'] . "</td>";
echo "</tr>";
}
echo "</table>";
//***************************** table 2 detail ***********
//Here i'm trying to use bNum_id in below query
$query = "SELECT * FROM fraud WHERE b_number=" . $_GET["bNun_id"];
echo "<table width='50%' border='5'>
<tr>
<th>Date & Time</th>
<th>A Number</th>
<th>B Number</th>
<th>Status</th>
</tr>";
$result=mysql_query($query) or die (mysql_error());
while($row=mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['a_number'] . "</td>";
echo "<td>" . $row['b_number'] . "</td>";
echo "<td>" . $row['status'] . "</td>";
echo "</tr>";
}
echo "</table>";
//connection close
mysqli_close($con);
?>
Here in table one , I used aNun_id to get the data from another PHP file with "GET" command.
In the table two, I want bNun_id to show its related data in that table. How do i call bNun _id data to the second table from table one. Pls help me..
Are those two tables in the same page?
If so, You need to append the bNum_id to the same query string to use both aNum_id & bNum_id.
echo "<td><a href='?aNun_id=".$_GET["aNun_id"]."&bNun_id=".$row['b_number']."'>" . $row['b_number'] . "</a></td>";
I'm trying to assign anID to the table I'm generating with PHP but it keeps returning an error. Here is the full code that works fine so far. All I want is to add an 'id' to the table so I can apply css styles to it in the relevant sheet
<?php
$con=mysqli_connect("localhost","<un>","<pw>","monitor");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM presnationalresults ORDER BY Percentage DESC");
echo "<table border='1'>
<tr>
<th>President</th>
<th>Party</th>
<th>Votes</th>
<th>Percentage</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['PresidentName'] . "</td>";
echo "<td>" . $row['PartyCode'] . "</td>";
echo "<td>" . $row['Votes'] . "</td>";
echo "<td>" . $row['Percentage'] . "</td>";
}
echo "</table>";
mysqli_close($con);
Any help? maybe I'm going about it the wrong way?
Please try below block of code . I have added table id and also close </tr> inside while loop
<?php
$con = mysqli_connect("localhost", "<un>", "<pw>", "monitor");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con, "SELECT * FROM presnationalresults ORDER BY Percentage DESC");
echo "<table border='1' id='table-id'>
<tr>
<th>President</th>
<th>Party</th>
<th>Votes</th>
<th>Percentage</th>
</tr>";
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['PresidentName'] . "</td>";
echo "<td>" . $row['PartyCode'] . "</td>";
echo "<td>" . $row['Votes'] . "</td>";
echo "<td>" . $row['Percentage'] . "</td>";
echo "</tr>"; // you forget close tr
}
echo "</table>";
mysqli_close($con);
I have MAMP (local hosted SQL,WEB etc server) the database name is :NKTDEBITS the table name is :Insurance and the column on the table is STATECOV. I know I'm close with this but still get a black in the field that should generate the total, anyone got a idea?
<?php
$con=mysqli_connect("localhost","root","root","KNTDebits");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM Insurance");
$result2 = mysqli_query($con,"SELECT * FROM Office");
$result3 = mysqli_query($con,"SELECT * FROM RichmondLocation");
$result4 = mysqli_query($con,"SELECT * FROM DanvilleLocation");
$result5 = mysql_query('SELECT SUM(STATECOV) AS STATECOV_sum FROM Insurance');
echo "<table border='1'>
<tr>
<th>Truck Number</th>
<th>VIN</th>
<th>Make</th>
<th>Model</th>
<th>State Coverage</th>
<th>Comprehinsive Coverage</th>
<th>Property Damage/th>
<th>Personal Injury</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['TNUM'] . "</td>";
echo "<td>" . $row['VIN'] . "</td>";
echo "<td>" . $row['MAKE'] . "</td>";
echo "<td>" . $row['MODEL'] . "</td>";
echo "<td>" . $row['STATECOV'] . "</td>";
echo "<td>" . $row['COMPRE'] . "</td>";
echo "<td>" . $row['PROPDMG'] . "</td>";
echo "<td>" . $row['PRSINJ'] . "</td>";
echo "</tr>";
}
echo "</table>";
//Table 2 Start
str_repeat(' ', 5); // adds 5 spaces
echo "<table border='5'>
<tr>
<th>Richmond</th>
<th>Date</th>
<th>Payment</th>
<th>Payer</th>
</tr>";
while($row3 = mysqli_fetch_array($result3))
{
echo "<tr>";
echo "<td>" . $row3[''] . "</td>";
echo "<td>" . $row3['DATE'] . "</td>";
echo "<td>" . $row3['PAYMENT'] . "</td>";
echo "<td>" . $row3['PAYER'] . "</td>";
echo "</tr>";
}
echo "</table>";
//Table 4 Start
str_repeat(' ', 5); // adds 5 spaces
echo "<table border='5'>
<tr>
<th>Danville</th>
<th>Date</th>
<th>Payment</th>
<th>Payer</th>
</tr>";
while($row4 = mysqli_fetch_array($result4))
{
echo "<tr>";
echo "<td>" . $row4[''] . "</td>";
echo "<td>" . $row4['DATE'] . "</td>";
echo "<td>" . $row4['PAYMENT'] . "</td>";
echo "<td>" . $sum . "</td>";
echo "</tr>";
}
echo "</table>";
//Table 5 Start
echo "<table border='5'>
<tr>
<th>Total</th>
</tr>";
$result = mysql_query('SELECT SUM(STATECOV) AS value_sum FROM Insurance');
$row = mysql_fetch_assoc($result);
$sum = $row['value_sum'];
while($row = mysql_fetch_assoc($result));
{
echo "<tr>";
echo "<td>" . $sum . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
You need a GROUP BY to go along with your SUM. I don;t know enough about you table to let you know what column you should use for this.
You should be handling potential error cases whenn you query the database, as you will quickly see when you are getting database/query errors.
You should also look to use mysqli or PDO instead of the deprecated mysql_* functions.