Retrieving Mysql data fails... Why? - php

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

Related

While loop if statement NULL issue

I can't seem to get this figured out. What am I doing wrong? I have a table in my database and some rows have an empty column for the "company" field.
I'm trying to get the "Users own uploaded files in the html table below. It belongs with "1629"
<?php
// Make a MySQL Connection
mysql_connect("localhost", "", "") or die(mysql_error());
//echo "Connected to MySQL<br />";
mysql_select_db("") or die(mysql_error());
//echo "Connected to Database";
$query = "SELECT company, dmy, COUNT(company) FROM AdTracking WHERE DATE(dmy) = CURRENT_DATE GROUP BY company";
$result = mysql_query($query) or die(mysql_error());
echo "<div style='margin-top:100px;'><center><h2>";
echo date('l jS \ F Y');
echo "<br />";
echo "</h2><center></div>";
echo '
<center> <table class="pure-table pure-table-horizontal">
<thead>
<tr>
<th>Company</th>
<th>Total</th>
</tr>
</thead>
<tbody>
';
// Print out result
while($row = mysql_fetch_array($result)){
if ($row['company'] == NULL) {
echo "Users own uploaded files";
};
echo "<tr>";
echo "<td><strong>" . $row['company'] . "</strong></td>";
echo "<td>" . $row['COUNT(company)'] . "</td>";
echo "</tr>";
}
echo '
</tbody>
</table> </center>
';
?>
You are printing outside of td and tr.
You have maintain format .
Try like this
echo "<tr>";
echo "<td><strong>" .($row['company'] == NULL ? "Users own uploaded files" : $row['company']). "</strong></td>";
echo "<td>" . $row['COUNT(company)'] . "</td>";
echo "</tr>";
you should use mysql_fetch_assoc() instead of mysql_fetch_array()
while($row = mysql_fetch_array($result)){
if (!$row['company']) // it check null or have something value
$value = "Users own uploaded files";
else
$value = $row['company'];
echo "<tr>";
echo "<td><strong>" . $value . "</strong></td>";
echo "<td>" . $row['COUNT(company)'] . "</td>";
echo "</tr>";
}
use mysqli or PDO instead of mysql because "mysql" is deprected.

Combine two rows together

Maybe someone can help me out. Any help is appreciated! I'm trying to Combine the "???" row with "unknown files" row. So the total would be 4245. Just one row.
I'm using a while loop. Here is my code
<?php
// Make a MySQL Connection
mysql_connect("localhost", "", "") or die(mysql_error());
//echo "Connected to MySQL<br />";
mysql_select_db("") or die(mysql_error());
//echo "Connected to Database";
$query = "SELECT company, username, COUNT(company), username FROM AdTracking WHERE DATE(dmy) = CURRENT_DATE GROUP BY company ORDER BY company ASC";
$result = mysql_query($query) or die(mysql_error());
echo "<div style='margin-top:100px;'><center><h2>";
echo date(' \ F jS Y - l');
echo "<br />";
echo "</h2><center></div>";
echo '
<center> <table class="pure-table pure-table-horizontal">
<thead>
<tr>
<th>Company</th>
<th>Total</th>
<th>Users</th>
</tr>
</thead>
<tbody>
';
// Print out result
while($row = mysql_fetch_assoc($result)){
echo "<tr>";
echo "<td><strong>" .($row['company'] == NULL ? "???" : $row['company']). "</strong></td>";
echo "<td>" . $row['COUNT(company)'] . "</td>";
echo "<td> ... </td>";
echo "</tr>";
}
echo '
</tbody>
</table> </center>
';
?>
You will have to calculate total of ??? and unknown file outside the while loop
$total = 0;
while($row = mysql_fetch_assoc($result))
{
if($row['company'] == NULL || $row['company'] == "unknown file")
$total += $row['COUNT(company)'];
}
Then you can use that total in the main output loop
while($row = mysql_fetch_assoc($result))
{
echo "<tr>";
echo "<td><strong>" .($row['company'] == NULL ? "???" : $row['company']). "</strong></td>";
if($row['company'] == "unknown file")
echo "<td>" . $total . "</td>";
else
echo "<td>" . $row['COUNT(company)'] . "</td>";
echo "<td> ... </td>";
echo "</tr>";
}

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.

Tables not showing in HTML database

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.

display records in table

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

Categories