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);
Related
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.
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.
I am creating a database and when creating the HTML script the table is coming back with no boarders = Firstname Lastname ";
while($row = mysqli_fetch_array($result)) {
echo "";
echo "" . $row['Firstname'] . "";
echo "" . $row['Lastname'] . "";
echo "";
}
echo "";
mysqli_close($con); ?>
Here is the code I created, could someone look at it and tell me what I have done wrong?
<?php
$con=mysqli_connect("localhost","root","","Franchise_Call_Log");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM caller_info");
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
th>Franchise</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['Firstname'] . "</td>";
echo "<td>" . $row['Lastname'] . "</td>";
echo "<td>" . $row['Franchise'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Look at the tag
<th>Franchise</th>
Please change your file extension to .php it is not rendering your script. Html file not render your php, and also correct your markup as suggested by #suhail.
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>";
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.