I outputed the data from my database in a HTML Table and styled it with CSS. Everything is fine, but there is an awkward White space above the outputed Table. (see picture). What's wrong with my code?
Result:
<html>
<head>
<title>Create new user</title>
<style>
table {
border-collapse: collapse;
width: 100%;
}
th, td {
text-align: left;
padding: 8px;
}
tr:nth-child(even){background-color: #f2f2f2}
th {
background-color: #4CAF50;
color: white;
}
</style>
</head>
<body>
<div>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "users";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "select * from employee";
$result = $conn->query($sql);
echo "<table>";
echo "<th>Identifier</th><th>Name</th><th>Lastname</th>";
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>" . $row["id"] . "</td>";
echo "<td>" . $row["vorname"] . "</td>";
echo "<td>" . $row["nachname"] . "</td>";
echo "</tr>";
echo "</br>";
}
}
else {
echo "0 results";
}
echo "</table>";
$conn->close();
?>
</div>
</body>
</html>
Expected result : no white space above the Table
Many Thanks
Why are you adding a <br>
echo "</br>";
in your while loop.
Try this -
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>" . $row["id"] . "</td>";
echo "<td>" . $row["vorname"] . "</td>";
echo "<td>" . $row["nachname"] . "</td>";
echo "</tr>";
}
} else {
echo "0 results";
}
If this does not fix, try inspecting and see what is adding space. Also check for the padding and margins in the computed view in chrome inspect view.
If nothing works, try setting top:0 for the div and may be for table :)
I have created a database on phpmyadmin via xampp. I'm currently trying to make a music player and I am having difficulty linking my music folder to my php coding. I don't know the right syntax or the location I should be working on either within my coding to implement the music player.
This is how my php translate currently and if you look at the Play column, it references the audio file's name
And this is where all my music is stored
Someone please help me, please..1
Here's my coding as well
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
}
</style>
</head>
<body>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "jukebox";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM Music";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table>
<tr>
<th>Artist</th>
<th>Title</th>
<th>Album</th>
<th>Albumcover</th>
<th>Play</th>
</tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo
"<tr>
<td>" . $row["Artist"]. "</td>
<td>" . $row["Title"]. "</td>
<td>" . $row["Album"]. "</td>
<td><img src='/jukebox/img/" . $row["Albumcover"] ."' alt=".$row["Albumcover"]."></td>
<td>" . $row["Play"] . "></td>
</tr>";
}
echo "</table>";
} else {
echo "0 results";
}
?>
</body>
</html>
<td>
<img src='"<?php echo $_SERVER['DOCUMENT_ROOT']; ?>"'/jukebox/img/" . $row["Albumcover"] ."' alt=".$row["Albumcover"].">
</td>
try this one
I'm trying to create a table to show users in my database and it's echoing 0 results for some reason. There are over 40,000 entries in my database so there is definitely something wrong with my PHP code but I'm just not sure where.
<?php
require 'session.php';
require 'header.php';
include 'database.php';
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM clients ORDER BY time_edit DESC LIMIT 45";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table border=1>";
echo "<tr>";
echo "<th>Name</th>";
echo "<th>ID</th>";
echo "<th>IP</th>";
echo "<th>Connections</th>";
echo "<th>Last Seen</th>";
echo "<th>Rank</th>";
echo "</tr>";
while($row = $result->fetch_assoc()) {
$name=$row['name'];
$id=$row['id'];
$ip=$row['ip'];
$connections=$row['connections'];
$seen=$row['time_edit'];
$rank=$row['group_bits'];
echo "<tr>";
echo "<td align=center> $name </td>";
echo "<td align=center> $id </td>";
echo "<td align=center> $ip </td>";
echo "<td align=center> $connections </td>";
echo "<td align=center> $seen </td>";
echo "<td align=center> $rank </td>";
echo "</tr>";
}
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
?>
I have finally figured out how to make my SQL table display using PHP however I am having a hell of a time getting the styles to show up. Here's what I have so far, any pointers on how to style the echo? Nothing shows up. Is how I am doing this the improper way to style an echo? I am trying to style it with Bootstrap CSS.
<html>
<head>
<title>Abacus PHP SQL Server Connection</title>
</head>
<body>
<?php
$db_host = '***';
$db_user = '***'; //recommend using a lower privileged user
$db_pwd = '***';
$database = '***';
$table = '***';
$connectionInfo = array("UID" => $db_user, "PWD" => $db_pwd, "Database"=>$database);
$conn = sqlsrv_connect( $db_host, $connectionInfo);
if( !$conn )
{
echo "Connection could not be established.\n";
die( print_r( sqlsrv_errors(), true));
}
$tsql = "SELECT category, data_desc, data_source, update_cycle,ISNULL(datename(mm,last_abadata_update) + + ' ' + DATENAME(dd,last_abadata_update) + ', ' + DATENAME(yyyy, last_abadata_update),'') AS last_abadata_update, ISNULL(datename(mm,last_abadata2_update) + ' ' + DATENAME(dd,last_abadata2_update) + ', ' + DATENAME(yyyy,last_abadata2_update),'') AS last_abadata2_update FROM {$table} ORDER BY category, data_source, data_desc";
$result = sqlsrv_query( $conn, $tsql);
if (!$result) {
die("Query to show fields from table failed");
}
echo "<table >";
echo "<tr>";
// printing table headers with desired column names
echo "<td style='border=1px solid black;Font-size=18;Font-Weight=bold'>";
echo "Category";
echo "</td>";
echo "<td style='border=1px solid black;Font-size=18;Font-Weight=bold'>";
echo "Description";
echo "</td>";
echo "<td style='border=1px solid black;Font-size=18;Font-Weight=bold'>";
echo "Source";
echo "</td>";
echo "<td style='border=1px solid black;Font-size=18;Font-Weight=bold'>";
echo "Update Cycle";
echo "</td>";
echo "<td style='border=1px solid black;Font-size=18;Font-Weight=bold'>";
echo "Last AbaData Update";
echo "</td>";
echo "<td style='border=1px solid black;Font-size=18;Font-Weight=bold'>";
echo "Last AbaData 2.0 Update";
echo "</td>";
echo "</tr>";
while($row = sqlsrv_fetch_array($result))
{
echo "<tr>";
echo "<td style='border=1px solid black'>";
echo $row['category'];
echo "</td>";
echo "<td style='border=1px solid black'>";
echo $row['data_desc'];
echo "</td>";
echo "<td style='border=1px solid black'>";
echo $row['data_source'];
echo "</td>";
echo "<td style='border=1px solid black'>";
echo $row['update_cycle'];
echo "</td>";
echo "<td style='border=1px solid black'>";
echo $row['last_abadata_update'];
echo "</td>";
echo "<td style='border=1px solid black'>";
echo $row['last_abadata2_update'];
echo "</td>";
echo "</tr>\n";
}
echo "</table>";
sqlsrv_free_stmt( $result);
sqlsrv_close( $conn);
?>
</body>
</html>
CSS accepts : not = so changestyle='border=1px solid black' to style='border:1px solid black'.
Your CSS should not be used with Camel Case, try using lowercase styles.
Also you should be using colons not equals for the styles.
"<td style='border:1px solid black; font-size:18; font-weight:bold'>";
use css like this
<style>
.outside-while{
border:1px solid black;font-size:18;font-weight:bold;
}
.inside-while{
border:1px solid black;
}
</style>
And for all td outside the while loop give class as outside-while
Nd for td inside the loop, give class as inside-while
example
echo "<td class='outside-while'>";
echo "Category";
echo "</td>";
And i feel outside it should be <th> not <td>
Now inside while
echo "<td class='inside-while'>";
echo $row['category'];
echo "</td>";
I don't like when you styling like this html. Use CSS to style. Add id or class to elements. I even don't echo html elements but instead opening and closing php tags and writing elements in plane html and only when i need to present php var I use php. Someone will say maybe its not good but this is how I do.
Here is how i would do your code:
<html><head><title>Abacus PHP SQL Server Connection</title></head><body>
<?php
$db_host = '***';
$db_user = '***'; //recommend using a lower privileged user
$db_pwd = '***';
$database = '***';
$table = '***';
$connectionInfo = array("UID" => $db_user, "PWD" => $db_pwd, "Database"=>$database);
$conn = sqlsrv_connect( $db_host, $connectionInfo);
if( !$conn )
{
echo "Connection could not be established.\n";
die( print_r( sqlsrv_errors(), true));
}
$tsql = "SELECT category, data_desc, data_source, update_cycle,ISNULL(datename(mm,last_abadata_update) + + ' ' + DATENAME(dd,last_abadata_update) + ', ' + DATENAME(yyyy, last_abadata_update),'') AS last_abadata_update, ISNULL(datename(mm,last_abadata2_update) + ' ' + DATENAME(dd,last_abadata2_update) + ', ' + DATENAME(yyyy,last_abadata2_update),'') AS last_abadata2_update FROM {$table} ORDER BY category, data_source, data_desc";
$result = sqlsrv_query( $conn, $tsql);
if (!$result) {
die("Query to show fields from table failed");
}
?>
<table>
<tr>
<td style="border:1px solid black;Font-size:18;Font-Weight:bold">
Category
</td>
<td style="border:1px solid black;Font-size:18;Font-Weight:bold">
Description
</td>
<td style="border:1px solid black;Font-size:18;Font-Weight:bold">
Source
</td>
<td style="border:1px solid black;Font-size:18;Font-Weight:bold">
Update Cycle
</td>
<td style="border:1px solid black;Font-size:18;Font-Weight:bold">
Last AbaData Update
</td>
<td style="border:1px solid black;Font-size:18;Font-Weight:bold">
Last AbaData 2.0 Update
</td>
</tr>
<?php
while($row = sqlsrv_fetch_array($result))
{
?>
<tr>
<td style="border:1px solid black">
<?php
echo $row['category'];
?>
</td>
<td style="border:1px solid black">
<?php
echo $row['data_desc'];
?>
</td>
<td style="border:1px solid black">
<?php
echo $row['data_source'];
?>
</td>
<td style="border:1px solid black">
<?php
echo $row['update_cycle'];
?>
</td>
<td tyle="border:1px solid black">
<?php
echo $row['last_abadata_update'];
?>
</td>
<td style="border:1px solid black">
<?php
echo $row['last_abadata2_update'];
?>
</td>
</tr>
<?php
}
?>
</table>
sqlsrv_free_stmt( $result);
sqlsrv_close( $conn);
?>
</body></html>
$(document).ready(function(){
$('#display').click(function(){
$.ajax({
type:"GET",
url:"new1.php",
datatype:"html",
async: "false",
success:function(response)
{
$("#show").html(response);
}
});
});
});
</head>
<input type="text" name="mono" method="post" class="tbox" >
<input type="button" name="submit" id="display" value="check">
<div id="show"> </div>
new1.php
<?php
$username = "root";
$password = "";
$hostname = "localhost";
$vah = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL");
echo "Connected MySQL ";
mysql_select_db("students",$vah);
$pin =$_POST['mono'];
// $pin = isset($_POST['mono']) ? $_POST['mono']: '';
$qry= "SELECT *FROM shop WHERE pin='$pin'";
$res= mysql_query($qry);
echo "<table border='1' >
<tr>
<td align=center> <b>pin</b></td>
<td align=center><b>service</b></td>
<td align=center><b>days</b></td>
</td> ";
echo "</tr>";
while($data = mysql_fetch_array($res))
{
echo "<tr>";
echo "<td align=center>$data[0]</td>";
echo "<td align=center>$data[1]</td>";
echo "<td align=center>$data[2]</td>";
echo "</tr>";
}
echo "</table>";
?>
I hope your error may solve after updating your new.php like the following:
<?php
$username = "root";
$password = "";
$hostname = "localhost";
$vah = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL");
echo "Connected MySQL ";
mysql_select_db("students",$vah);
$pin =$_POST['mono'];
// $pin = isset($_POST['mono']) ? $_POST['mono']: '';
$qry= "SELECT *FROM shop WHERE pin='$pin'";
$res= mysql_query($qry);
$html = "<table border='1' >
<tr>
<td align=center> <b>pin</b></td>
<td align=center><b>service</b></td>
<td align=center><b>days</b></td>
</td> ";
echo "</tr>";
while($data = mysql_fetch_array($res))
{
$html += "<tr>";
$html += "<td align=center>$data[0]</td>";
$html += "<td align=center>$data[1]</td>";
$html += "<td align=center>$data[2]</td>";
$html += "</tr>";
}
$html += "</table>";
echo $html;
?>