display records in table - php

I am having some trouble with my first PHP project, I am trying to get the data from MySQL database (Has 3 records) and display it in tables. Problem is it only seem to display records 2 and 3, it skips the 1st record. Please see my code and display below.
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM unitstats");
while($row = mysqli_fetch_array($result)) {
echo "<table border='1' style='color:white'>
<tr>
<th>ID</th>
<th>Name</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "</tr>";
}
echo "</table>";
}

you are using two while loop which is unnecessary use following code
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
echo "</table>";
echo "<table border='1' style='color:white'>
<tr>
<th>ID</th>
<th>Name</th>
</tr>";
$result = mysqli_query($con,"SELECT * FROM unitstats");
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "</tr>";
}
echo "</table>";

if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
echo "<table border='1' style='color:white'>
<tr>
<th>ID</th>
<th>Name</th>
</tr>";
$result = mysqli_query($con,"SELECT * FROM unitstats");
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "</tr>";
}
echo "</table>";
Basically you didn't need the initial loop, this would have mainly caused an issue because you would have been redeclaring $row with the second loop.

Just remove your outer while loop and just use this:
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM unitstats");
echo "<table border='1' style='color:white'>
<tr>
<th>ID</th>
<th>Name</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "</tr>";
}
echo "</table>";

Related

Table not displaying any results in a array-query

I'm trying to display the results of a database on a webpage. Using this example, I have come up with this piece of php code below:
<?php
$con=mysqli_connect("217.199.187.70","cl52-domains","######","cl52-domains");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$results = mysqli_query($con, "SELECT * FROM domains");
echo "<table border='1'>
<tr>
<th>ID</th>
<th>Domain</th>
<th>Address</th>
<th>Price</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['domain_name'] . "</td>";
echo "<td>" . $row['domain_address'] . "</td>";
echo "<td>" . $row['price'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
However, when I refresh my webpage, it shows the table header (no errors), but 0 results. Here is my table specifications:
Can someone tell me why I'm not getting any results?
Change
while($row = mysqli_fetch_array($result))
To
while($row = mysqli_fetch_array($results))
You probably made a typo in referencing the $results variable.

call variable from while loop in PHP

<?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>";

Table ID in PHP

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);

Retrieving Mysql data fails... Why?

So this is my code but it does not work, I just get this
Title Article "; while($row = mysqli_fetch_array($result)) { echo ""; echo "" . $row['Title'] . ""; echo "" . $row['Article'] . ""; echo ""; } echo ""; mysqli_close($con); ?>
Any ideas?
<?php
$con=mysqli_connect("localhost","____","____","test_database");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM written");
echo "<table border='1'>
<tr>
<th>Title</th>
<th>Article</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['Title'] . "</td>";
echo "<td>" . $row['Article'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Tripple check your quotation marks are set correctly. Either
echo "<table border='1'>
or
</tr>";
has an error in it.
You may also try using echo as a function (bracelets), it should make spotting the error easier :-)
It would be helpfull to see the resulting markup instead of the text ;-)
You have to do this:
echo "<table border='1'><tr><th>Title</th><th>Article</th></tr>";
or
echo "<table border='1'>";
echo "<tr>";
echo "<th>Title</th>";
echo "<th>Article</th>";
echo "</tr>";
instead of
echo "<table border='1'>
<tr>
<th>Title</th>
<th>Article</th>
</tr>";

PHP get sum of SQL table

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.

Categories