Displaying Mysql data using PHP gives me a blank page - php

I am trying to display a table containing data from my db in a php page.
No problems at all.
When I try to use css to make the table better looking the browser gives me simply a blank page.
Here's my code...
If I delete the id=csstest part after opening the table tag everything works, as soon as I add id=csstest I get a blank page...
What am I doing wrong?
<?php
include 'config.php';
if (!mysql_connect($db_host, $db_user, $db_pwd))
die("Can't connect to db");
if (!mysql_select_db($database))
die("Can't select db");
// sending query
$result = mysql_query("SELECT data, cur_timestamp FROM {$table}");
if (!$result) {
die("Check your SQL query");
}
$fields_num = mysql_num_fields($result);
echo "<h1>Tabella: {$table}</h1>";
echo "<table id="csstest"><tr>";
// printing table headers
for($i=0; $i<$fields_num; $i++)
{
$field = mysql_fetch_field($result);
echo "<td>{$field->name}</td>";
}
echo "</tr>\n";
// printing table rows
while($row = mysql_fetch_row($result))
{
echo "<tr>";
// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
foreach($row as $cell)
echo "<td>$cell</td>";
echo "</tr>\n";
}
mysql_free_result($result);
mysql_close($result);
?>
</table>

Change the following statement:
echo "<table id="csstest"><tr>";
to this:
echo "<table id=\"csstest\"><tr>";

you need to add slashes before your double quotes:
echo "<table id=\"csstest\"><tr>";

echo "<table id="csstest"><tr>";
above code generates parse error and your error reporting is off so it just showing blank page
try on of the below method
echo "<table id='csstest'><tr>";
echo '<table id="csstest"><tr>';
echo "<table id=\"csstest\"><tr>";

Related

How to use database table header for html <th> table header tag

I'm new to PHP. I use the code (below) to print a MySQL table as an HTML table.
However my code prints the table headers from the database.
How can I print HTML table header in hard coded format using the <th>header</th> tags and print the all rows from the table?
Thanks!
<?php
$db_host = 'localhost';
$db_user = 'my user';
$db_pwd = 'my pwd';
$database = 'my db';
$table = 'subcontractor';
if (!mysql_connect($db_host, $db_user, $db_pwd))
die("Can't connect to database");
if (!mysql_select_db($database))
die("Can't select database");
// sending query
$result = mysql_query("SELECT * FROM {$table}");
if (!$result) {
die("Query to show fields from table failed");
}
$fields_num = mysql_num_fields($result);
echo "<table class='table table-bordered table-striped mb-none' id='datatable-tabletools' data-swf-path='assets/vendor/jquery-datatables/extras/TableTools/swf/copy_csv_xls_pdf.swf' >";
// printing table headers
echo "<thead>";
for($i=0; $i<$fields_num; $i++)
{
$field = mysql_fetch_field($result);
echo "<th>{$field->name}</th>";
}
echo "</thead>";
// printing table rows
while($row = mysql_fetch_row($result))
{
echo "<tbody>";
echo "<tr>";
echo "</thead>";
// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
foreach($row as $cell)
echo "<td>$cell</td>";
echo "</tr>";
echo "</tbody>";
}
mysql_free_result($result);
?>
Thank you works perfect. Is there a way to make PHP skip a column from the table on the DB? Thanks –
Yes, just change the Query
$result = mysql_query("SELECT * FROM {$table}");
for example by
$result = mysql_query("SELECT `name`,`email`,`address` FROM {$table}");
You can change the headers by removing these lines of code.
for($i=0; $i<$fields_num; $i++)
{
$field = mysql_fetch_field($result);
echo "<th>{$field->name}</th>";
}
Replace the above lines for example by:
echo "<thead>";
echo "<th>My Header 1</th>";
echo "<th>My Header 2</th>";
echo "</thead>";

Add link to echo'd HTML from SQL data

Back with another quick question. I have this code below which echo's out product names from a database. What I want to do is make the echoed out product names a link to another page called product.php, each link needs to have a unique ID, for example
Product Name
How would I go about doing this? Many thanks. I will point out that I am very new to PHP.
<?php
//create an ADO connection and open the database
$conn = new COM("ADODB.Connection");
$conn->open("PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=C:\WebData\Northwind.mdb");
//execute an SQL statement and return a recordset
$rs = $conn->execute("SELECT product_name FROM Products");
$num_columns = $rs->Fields->Count();
echo "<table border='1'>";
echo "<tr><th>Name</th></tr>";
while (!$rs->EOF) //looping through the recordset (until End Of File)
{
echo "<tr>";
for ($i=0; $i < $num_columns; $i++) {
echo "<td>" . $rs->Fields($i)->value . "</td>";
}
echo "</tr>";
$rs->MoveNext();
}
echo "</table>";
//close the recordset and the database connection
$rs->close();
$rs = null;
$conn->close();
$conn = null;
?>
Assuming your Products table has a unique ID field called "id", change your select to:
$rs = $conn->execute("SELECT id, product_name FROM Products");
And when you want to create a link, use that field and pass it into the URL. So you'd have product.php?id=<?= $thatIdField; ?>.
Example code:
echo "<table border='1'>";
echo "<tr><th>Name</th></tr>";
while (!$rs->EOF) //looping through the recordset (until End Of File)
{
echo "<tr>";
for ($i=0; $i < $num_columns; $i++) {
echo "<td>" . $rs->Fields($i)->value . "</td>";
}
echo "</tr>";
$rs->MoveNext();
}
echo "</table>";

Need a HTML to show a SQL table

I need to make the HTML table to show an SQL table, but it won't show up.
I have no idea, why it won't show up.
Here's the code.
<h1> <img src="gag3.png" width="454" height="70" alt="Velkommen!"></h1>
<table width="100%" cellspacing="1" cellpadding="1" border="0" summary="">
<tbody>
<?php
$db_host = 'localhost';
$db_user = 'root';
$db_pwd = '';
$database = 'test1';
$table = 'text';
if (!mysql_connect($db_host, $db_user, $db_pwd))
die("Can't connect to database");
if (!mysql_select_db($database))
die("Can't select database");
// sending query
$result = mysql_query("SELECT * FROM {$table}");
if (!$result) {
die("Query to show fields from table failed");
}
$fields_num = mysql_num_fields($result);
// printing table headers
for($i=0; $i<$fields_num; $i++)
{
$field = mysql_fetch_field($result);
}
echo "</tr>\n";
// printing table rows
while($row = mysql_fetch_row($result))
{
echo "<tr>";
// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
foreach($row as $cell)
echo "<td>$cell</td>";
echo "</tr>\n";
}
mysql_free_result($result);
?>
</tbody>
</table>
Now i really want it to show inside the table there, i want it to show the PHP,
You have no opening <tr> in that snippet. Also consider using <th></th> for your table headings. You also need to echo the headings!
// printing table headers
<tr>
for($i=0; $i<$fields_num; $i++)
{
$field = mysql_fetch_field($result);
echo "<th>".$field."</th>
}
echo "</tr>\n";
You also don't need the \n in there; <tr></tr> automatically makes a line break for the next row. I don't know how those affect the HTML; consider removing them if it still doesn't work?

Displaying mysql table with php

I'm having trouble displaying my mysql table using php code. All it displays is the column names not the values associated with them. I know my username password and db are all correct but like I said the table is not displaying the values I added. Any help would be much appreciated This is my mysql code:
CREATE TABLE Guitars
(
Brand varchar(20) NOT NULL,
Model varchar(20) NOT NULL,
PRIMARY KEY(Brand)
);
insert into Guitars values('Ibanez','RG');
insert into Guitars values('Ibanez','S');
insert into Guitars values('Gibson','Les Paul');
insert into Guitars values('Gibson','Explorer');
And this is my php code:
<?php
$db_host = '*****';
$db_user = '*****';
$db_pwd = '*****';
$database = '*****';
$table = 'Guitars';
if (!mysql_connect($db_host, $db_user, $db_pwd))
die("Can't connect to database");
if (!mysql_select_db($database))
die("Can't select database");
// sending query
$result = mysql_query("SELECT * FROM {$table}");
if (!$result) {
die("Query to show fields from table failed");
}
$fields_num = mysql_num_fields($result);
echo "<table border='1'><tr>";
// printing table headers
for($i=0; $i<$fields_num; $i++)
{
$field = mysql_fetch_field($result);
echo "<td>{$field->name}</td>";
}
echo "</tr>\n";
// printing table rows
while($row = mysql_fetch_row($result))
{
echo "<tr>";
// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
foreach($row as $cell)
echo "<td>$cell</td>";
echo "</tr>\n";
}
mysql_free_result($result);
?>
Try this:
// printing table rows
while($row = mysql_fetch_row($result))
{
echo "<tr>";
echo "<td>$row[0]</td>";
echo "<td>$row[1]</td>";
echo "</tr>\n";
}
Update:
Note: You can't make brand as primary key since you gonna add same brand name for different models.
I don't see why you are using the fetch_field call. I'm assuming that you know ahead of time what the actual names of each field in your table is prior to calling it's data? I think for simplicity sake (less loops and nested loops) you should write the name of the fields manually, then loop through the data entering the values.
$feedback .= "<table border='1'><tr>";
$feedback .= "<th>Brand</th><th>Model</th></tr>";
while ($row = mysql_fetch_array($result)) {
$feedback .= "<tr><td>" . $row['Brand'] . "</td>";
$feedback .= "<td>" . $row['Model'] . "</td></tr>";
}
$feedback .= "</table>";
echo $feedback;
By the time you're done displaying the header, the query result's internal pointer will have reached the last row, so your mysql_fetch_row() calls fail because there are no more rows to fetch. Call mysql_data_seek(0); before printing the table rows, to move the internal pointer back to the first row.
You can also try for fetching data
while ($fielddata = mysql_fetch_array($result))
{
echo '<tr>';
for ($i = 0; $i<$fields_num; $i++) // $fields_num already exists in your code
{
$field = mysql_fetch_field($result, $i);
echo '<td>' . $fielddata[$field->name] . '</td>';
}
echo '</tr>';
}

MYsql in a HTML table. Delete row

I'm using PHP to display what is in my MYsql database in a table. I would like to add a delete button buy I don't know how. I would like the delete button right after the last column. Here is my code.
<?php
$db_host = 'localhost';
$db_user = 'root';
$db_pwd = '';
$database = 'coins_gage';
$table = 'coins';
if (!mysql_connect($db_host, $db_user, $db_pwd))
die("Can't connect to database");
if (!mysql_select_db($database))
die("Can't select database");
// sending query
$result = mysql_query("SELECT * FROM {$table}");
if (!$result) {
die("Query to show fields from table failed");
}
$fields_num = mysql_num_fields($result);
echo "<h1>Table: {$table}</h1>";
echo "<table border='1'><tr>";
// printing table headers
for($i=0; $i<$fields_num; $i++)
{
$field = mysql_fetch_field($result);
echo "<td>{$field->name}</td>";
}
echo "</tr>\n";
// printing table rows
while($row = mysql_fetch_row($result))
{
echo "<tr>";
// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
foreach($row as $cell)
echo "<td>$cell</td>";
echo "</tr>\n";
}
mysql_free_result($result);
?>
You have to do several things here to make the interface user friendly and perform your task. If I summarize the steps you have to do is like this.
1) Make sure you keep your table inside a form with POST method. And add following kind of hidden elements just before close the FORM tag.
<input type="hidden" name="hidDelete" id="hidDelete" value="" />
2) Add a column header to the table by modifying this section.
// printing table headers
for($i=0; $i<$fields_num; $i++)
{
$field = mysql_fetch_field($result);
echo "<td>{$field->name}</td>";
}
echo "<td>Delete</td>";
echo "</tr>\n";
3) Add the delete button to all the rows.
foreach($row as $cell)
echo "<td>$cell</td>";
echo "<td><input type=\"button\" value=\"Delete\" onclick=\"deleteThis({$field->id})\" /></td>"
echo "</tr>\n";
4) The create a javascript function to make the delete request. Before you post data you have to set a hidden value. If you use javascript library like jquery this will be much easier. Since I don't know which library you are using I will explain in pure javascript.
<script type="text/javascript">
function deleteThis(id)
{
document.getElementById("hidDelete").value = id;
document.yourFormName.submit();
}
</script>
5) Once you get the post request to your page, Make the deletion before you do select queries.
if(isset($_POST["hidDelete"]) $_POST["hidDelete"] != "")
{
$rowID = $_POST["hidDelete"];
// Write your delete queries
}

Categories