I've got an MSSQL server running with several large tables. I'm trying to place them into an HTML table so that I can display all of the data in a nice CSS modified webpage. I'm using PHP to ferry the information from SQL to my HTML script, but the only way I've found so far to generate such a table is by hardcoding all of the SQL column names into my PHP-SQL query.
As you can see below, this is the setup required for just one such table. Is there any more concise way to get the information from SQL into a formatted HTML table? I've looked around for a number of hours perhaps for some sort of PHP scripted loop that can iterate through all of the SQL columns, but I haven't found anything. I greatly appreciate any input!
<table id="capacitors">
<thead>
<tr>
<td>Part Number</td>
<td>Capacitance</td>
<td>Capacitance Tolerance</td>
<td>Case Package</td>
<td>Case Package SI</td>
<td>Dielectric Char.</td>
<td>Dielectric Mat.</td>
<td>Halogen Free Stat.</td>
<td>Insulation Resistance</td>
<td>Lead Free Stat.</td>
<td>Lifecycle Stat.</td>
<td>Mounting Style</td>
<td>Operating Temp.</td>
<td>Packaging</td>
<td>Pin Count</td>
<td>Reach SVHC Comp.</td>
<td>Rohs Stat.</td>
<td>Size Height</td>
<td>Size Length</td>
<td>Size Thickness</td>
<td>Size Width</td>
<td>Temp. Coefficient</td>
<td>Voltage Rating DC</td>
</tr>
</thead>
<tbody>
<?php
foreach ($db->query($sql) as $rows){
?>
<tr>
<td><?php echo $rows['part_number']?></td>
<td><?php echo $rows['capacitance']?></td>
<td><?php echo $rows['capacitance_tolerance']?></td>
<td><?php echo $rows['case_package']?></td>
<td><?php echo $rows['case_package_si']?></td>
<td><?php echo $rows['dielectric_characteristic']?></td>
<td><?php echo $rows['dielectric_material']?></td>
<td><?php echo $rows['halogen_free_status']?></td>
<td><?php echo $rows['insulation_resistance']?></td>
<td><?php echo $rows['lead_free_status']?></td>
<td><?php echo $rows['lifecycle_status']?></td>
<td><?php echo $rows['mounting_style']?></td>
<td><?php echo $rows['operating_temperature']?></td>
<td><?php echo $rows['packaging']?></td>
<td><?php echo $rows['pin_count']?></td>
<td><?php echo $rows['reach_svhc_compliance']?></td>
<td><?php echo $rows['rohs_status']?></td>
<td><?php echo $rows['size_height']?></td>
<td><?php echo $rows['size_length']?></td>
<td><?php echo $rows['size_thickness']?></td>
<td><?php echo $rows['size_width']?></td>
<td><?php echo $rows['temperature_coefficient']?></td>
<td><?php echo $rows['voltage_rating_dc']?></td>
</tr>
<?php
}
?>
</tbody>
</table>
This is basic example using PHP Driver for SQL Server:
<?php
# Settings
$server = 'server\instance,port';
$database = 'database';
$user = 'user';
$password = 'password';
$tablename = 'tablename';
# Connection
$cinfo = array(
"Database" => $database,
"ReturnDatesAsStrings" => true,
"UID" => $user,
"PWD" => $password
);
$conn = sqlsrv_connect($server, $cinfo);
if($conn === false)
{
echo "Error (sqlsrv_connect): ".print_r(sqlsrv_errors(), true);
exit;
}
# SQL statement
$sql = "SELECT * FROM [".$tablename."]";
$stmt = sqlsrv_prepare($conn, $sql);
if($stmt === false) {
echo "Error (sqlsrv_prepare): ".print_r(sqlsrv_errors(), true);
exit;
}
# Columns names
echo '<table id="'.$tablename.'">';
echo "<thead>";
echo "<tr>";
$metadata = sqlsrv_field_metadata($stmt);
foreach($metadata as $field) {
echo "<td>".$field['Name']."</td>";
}
echo "</tr>";
echo "</thead>";
# Table rows
echo "<tbody>";
if (!sqlsrv_execute($stmt)) {
echo "Error (sqlsrv_execute): ".print_r(sqlsrv_errors(), true);
exit;
}
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
echo "<tr>";
foreach($row as $value) {
echo "<td>".$value."</td>";
};
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
# End
sqlsrv_free_stmt($stmt);
sqlsrv_close($conn);
?>
Maybe use SHOW COLUMNS or DESCRIBE
$columns = "SHOW COLUMNS FROM <table>";
$output = mysqli_query($conn,$sql);
while($row = mysqli_fetch_array($result)){
echo $row['Field']."<br>";
}
Related
I am trying to retrieve all data from my database and displaying it in a table. But i could not do that, am facing some problem.I am getting error as,
This page isn’t working.
localhost is currently unable to handle this request.
Here is my code,
<html>
<body>
<table style="width:100%">
<tr>
<th>Driverid</th>
<th>Truckid</th>
<th>Imagecount</th>
<th>Trainingstatus</th>
</tr>
<?php
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "IDdb";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT DriverID, TruckID, Imagecount, Trainingstatus FROM IDs";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$driverid = $row["Driverid"];
$truckid = $row["Truckid"];
$imagecount = $row["Imagecount"];
$trainingstatus = $row["Trainingstatus"];?>
<tr>
<td><?php echo $driverid; ?></td>
<td><?php echo $truckid; ?></td>
<td><?php echo $imagecount; ?></td>
<td><?php echo $trainingstatus; ?></td>
</tr>
</table>
<?php}
} else {
echo "0 results";
}
$conn->close();
?>
</body>
</html>
please replace your code
from
driverid = $row["Driverid"];
truckid = $row["Truckid"];
imagecount = $row["Imagecount"];
trainingstatus = $row["Trainingstatus"];
to
$driverid = $row["Driverid"];
$truckid = $row["Truckid"];
$imagecount = $row["Imagecount"];
$trainingstatus = $row["Trainingstatus"];
please reader the section about how to define variables in php on the PHP Manual ,after that, check the variables defined in your code.
you have not started table tag, also not in loop, and main part is sdd space <?php} to <?php }
if ($result->num_rows > 0) {
echo '<table>';
// output data of each row
while($row = $result->fetch_assoc()) {
$driverid = $row["Driverid"];
$truckid = $row["Truckid"];
$imagecount = $row["Imagecount"];
$trainingstatus = $row["Trainingstatus"];?>
<tr>
<td><?php echo $driverid; ?></td>
<td><?php echo $truckid; ?></td>
<td><?php echo $imagecount; ?></td>
<td><?php echo $trainingstatus; ?></td>
</tr>
<?php }
echo '</table>';
} else {
echo "0 results";
}
I'm kind of new in php and i'm searching a way to get some words from a specific column that i get from a sql server stored proc. These 'words' will me to use them as style for my css
Example :
Column HTML_CODE has sometime 'bold', 'green' or something else. Sometime they are in the same column, not always in the same order. I don't know how to use them in my loop. See what i has until now :
$result = sqlsrv_query($conn, $sql);
while ($row = sqlsrv_fetch_array($result))
{
//print_r( $row ); // debug code
if ($row['HTML_CODE'] == 'BOLD(), Green()'){
$couleur='green';
$font= 'bold';
}
else {
$couleur='black';
$font= 'normal';
}
?>
<tbody>
<tr>
<?php echo "<tr style=\"font-weight:$font; color:$couleur;\">"; ?>
<td><?php echo ($row['Nom']); ?></td>
<td><?php echo ($row['Quantite']); ?></td>
<td><?php echo ($row['Montant']); if (is_numeric($row['Montant'])) {
echo ' $';
}?></td>
<td><?php echo ($row['#GL']); ?></td>
<td><?php echo ($row['Debit']); if (is_numeric($row['Debit'])) {
echo ' $';
} ?></td>
<td><?php echo ($row['Credit']); if (is_numeric($row['Credit'])) {
echo ' $';
}?></td>
</tr>
</tbody>
if( strstr($row['HTML_CODE'], "BOLD()")){ $font= 'bold'; }
I have wrote a select query using function but it is not returning data from MySQL database... can any one help me in this regard. I'm uploading screenshots.
Here is the name of my database, 'classified'
this is conn.php file
$conn = mysql_connect('localhost','root','');
echo mysql_select_db('classified',$conn);
function to fetch record from database, 'adds' table
On index.php i've wrote this code.
include 'functions/crud_functions.php'
$adds = get_all_adds();
foreach($adds as $add) {
<tr>
<td><?php echo $add['id']; ?></td>
<td><?php echo $add['ad_title'];?></td>
<td><?php echo $add['cat_id'];?></td>
<td><?php echo $add['ad_description'];?></td>
<td><?php echo $add['avatar'];?></td>
<td><?php echo $add['price'];?></td>
<td><?php echo $add['contact'];?></td>
<td><?php echo $add['name'];?></td>
<td><?php echo $add['time'];?></td>
<td>Edit | Del </td>
</tr>
but this is not displaying records.
Change :
if ($result) {
return TRUE;
} else {
return FALSE;
}
into this :
if ($result) {
return $data;
} else {
return array();
}
Trying to list the data from mysql to a html table using php in main html file. I've been through all of the other questions on here and I'm sure I have mostly the same methods and code as them.
For some reason (which I suspect has something to do with mysql and not the code) the only result is a blank table with one row and five columns. Each time I try to implement the other codes they just seem to print text onto the site.
I'm very confused as I think I've done the right thing. I've even been able to list the data from mysql through php echo so I know it's there and that I can access it. Really would appreciate some help on this. Thank you.
<table border="1">
<tbody>
<?php
$connect = mysqli_connect("localhost", "daemon", "xampp", "test");
if (!$connect) {
die(mysqli_error());
}
$results = mysqli_query("SELECT title,url,details,file,submission_date FROM input");
while($row = mysqli_fetch_array($results)) {
?>
<td><?php echo $row['title']?></td>
<td><?php echo $row['url']?></td>
<td><?php echo $row['details']?></td>
<td><?php echo $row['file']?></td>
<td><?php echo $row['submission_date']?></td>
<?php
}
?>
</tbody>
</table>
You say this code is in your mail html file? And it is printing out onto the screen? Try changing your file to .php not .html! Php code won't run in a .html file, and will likely output your code directly onto the page.
Mysqli_query expect connection link as first parameter.
$results = mysqli_query($connect, "SELECT title,url,details,file,submission_date FROM input");
Just a quick not so related improvement. You can avoid inserting most of the php opening and closing clauses:
...
while($row = mysqli_fetch_array($results)){
echo "<td>".$row['title']."/td>";
echo "<td>".$row['url']"</td>";
...
}
?>
Use <tr> because table must have atleast one row(<tr>) and one column(<td>)
<table border="1">
<tbody>
<tr>
<?php
$connect = mysqli_connect("localhost", "daemon", "xampp", "test");
if (!$connect) {
die(mysqli_error());
}
$results = mysqli_query("SELECT title,url,details,file,submission_date FROM input");
while($row = mysqli_fetch_array($results)) {
?>
<td><?php echo $row['title']?></td>
<td><?php echo $row['url']?></td>
<td><?php echo $row['details']?></td>
<td><?php echo $row['file']?></td>
<td><?php echo $row['submission_date']?></td>
<?php
}
?>
</tr>
</tbody>
<table border="1">
<tbody>
<?php
$connect = mysqli_connect("localhost", "daemon", "xampp", "test");
if (!$connect) {
die(mysqli_error());
}
$results = mysqli_query($connect, "SELECT title,url,details,file,submission_date FROM input");
if (!$results) {
mysqli_error($results);
die();
}
while ($row = mysqli_fetch_array($results)) {
?>
<tr>
<td><?php echo $row['title'] ?></td>
<td><?php echo $row['url'] ?></td>
<td><?php echo $row['details'] ?></td>
<td><?php echo $row['file'] ?></td>
<td><?php echo $row['submission_date'] ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
I have the next issue, when I need the code show me all the users in the table always show me one data the first one or the last one if I change the ASC to DESC inside of SELECT..
I need to show me all users... can you please help me with this?
Here the code and the table with the row I need to show:
<?
include '../include/config.php';
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);
$sql = 'SELECT * FROM PACIENTES ORDER BY id_paciente ASC';
foreach ($conn->query($sql) as $row) {
$id_paciente = $row['id_paciente'];
$id_tipo = $row['id_tipo'];
$nombre = $row['nombre'];
$apellido = $row['apellido'];
$ciudad = $row['ciudad'];
$telefono = $row['telefono'];
$foto = $row['foto'];
}
?>
<tr>
<th><?php echo $id_paciente; ?></th>
<td><img src="../<?php echo $foto;?>" class="image_thumbnail" /></td>
<td><?php echo $nombre; ?></td>
<td><?php echo $apellido; ?></td>
<td><?php echo $id_tipo; ?></td>
<td><?php echo $ciudad; ?></td>
<td><?php echo $telefono; ?></td>
You are echoing your variables outside of the loop.
So, move it inside:
$sql = 'SELECT * FROM PACIENTES ORDER BY id_paciente ASC';
foreach ($conn->query($sql) as $row) {
?>
<tr>
<th><?php echo $row['id_paciente'] ?></th>
<td><img src="../<?php echo $row['foto']?>" class="image_thumbnail" /></td>
<td><?php echo $row['nombre'] ?></td>
<td><?php echo $row['apellido'] ?></td>
<td><?php echo $row['id_tipo'] ?></td>
<td><?php echo $row['ciudad'] ?></td>
<td><?php echo $row['telefono'] ?></td>
<tr>
<? } ?>
well I get my answer with my problem...
now I see all the users, the code neccesary is:
<?
$sql = 'SELECT * FROM PACIENTES ORDER BY id_paciente ASC';
$result = $conn->query($sql);
while($row = $result->fetch(PDO::FETCH_ASSOC)) {
?>
for somebody want to
Best Regards!