Load SQL tables in HTML page - html

EDIT: Updated code, still not working
I'm trying to get tables to be displayed on html through a button click, unless it is possible to automatically load up tables on page initiation.
I keep running into an error: Fatal error: Uncaught Error: Call to undefined function mysql_fetch_array() on Line 15.
I am using a .php script
Here is my code:
<?php
include ("ConnDetails.php");
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$result = mysqli_query($conn, "SELECT * FROM Tracks");
if ($result !== false) {
echo "<table border='1'>
<tr>
<th>Tracks<th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['Tracks'] . "</td>";
echo "</tr>";
}
echo "</table>";
} else {
echo "You have encountered an error";
}
$conn->close();
?>
Thanks in advance

I think you can use:
while($row = $result->fetch_assoc())
{
echo "<tr>";
echo "<td>" . $row['Tracks'] . "</td>";
echo "</tr>";
}
echo "<table>";
} else {
echo "You have encountered an error";
}
$conn->close();
?>

Related

Trying to echo a table but it's not working

The table is called bookings. I'm trying to echo rows from the table, but I'm getting errors in some places according to user_id. Dreamweaver tells me the last few lines have errors, but I'm not sure what's the problem.
<?php
require 'connect.php';
include("auth.php");
$user = $_SESSION['username'];
$uid = $_SESSION['user_id'];
$sql = "SELECT * FROM bookings WHERE user_id =". $uid;
$result = mysqli_query($conn, $sql);
if ($result->num_rows > 0) { // if the mysqli_query performed above works
echo "<br>";
echo '<table border="1">';
echo '<tr>
<th>booking_id</th>
<th>"Room_name"</th>
<th>"booking_date"</th>
<th>"period_name"</th>
<th>"booking_id"</th>
<th>"Cancel Booking"</th>
</tr>';
while ($row = mysqli_fetch_assoc($result)) { // important line
echo '<tr>';
echo '<td>'.$row["booking_id"].'</td>';
echo '<td>'.$row["Room_name"].'</td>';
echo '<td>'.$row["booking_date"].'</td>';
echo '<td>'.$row["date_booked"].'</td>';
echo '<td>'.$row["period_name"].'</td>';
echo '<td>'.$row["booking_id"].'</td>';
echo "</tr>";
echo "</table>";
}
else echo '<p>You have no bookings.</p>';
I have made some change
And here is the code-
require 'connect.php';
include 'auth.php';
$user = $_SESSION['username'];
$uid = $_SESSION['user_id'];
$sql = "SELECT * FROM bookings WHERE user_id ='". $uid."'";
$result = mysqli_query($conn, $sql);
if ($result->num_rows > 0) { // if the mysqli_query performed above works
echo "<br>";
echo '<table border="1">';
echo '<tr>
<th>booking_id</th>
<th>Room_name</th>
<th>booking_date</th>
<th>period_name</th>
<th>booking_id</th>
<th>Cancel Booking</th>
</tr>';
while ($row = mysqli_fetch_assoc($result)) { // important line
echo '<tr>';
echo '<td>'.$row["booking_id"].'</td>';
echo '<td>'.$row["Room_name"].'</td>';
echo '<td>'.$row["booking_date"].'</td>';
echo '<td>'.$row["date_booked"].'</td>';
echo '<td>'.$row["period_name"].'</td>';
echo '<td>'.$row["booking_id"].'</td>';
echo '</tr>';
}
echo '</table>';
} else echo '<p>You have no bookings</p>';

JSON to HTML Table?

How can I generate html table form this url's json output?
http://megagrup.site/entegrasyon/hepsiburada.php
Update
I will find result like this;
<?php
$json=file_get_contents("url");
$data = json_decode($json);
if (count($data->listings)) {
// Open the table
echo "<table>";
// Cycle through the array
foreach ($data->listings as $idx => $stand) {
// Output a row
echo "<tr>";
echo "<td>".$stand->abc."</td>";
echo "<td>".$stand->def."</td>";
echo "</tr>";
}
// Close the table
echo "</table>";
}
I will find result like this;
<?php
$json=file_get_contents("url");
$data = json_decode($json);
if (count($data->listings)) {
// Open the table
echo "<table>";
// Cycle through the array
foreach ($data->listings as $idx => $stand) {
// Output a row
echo "<tr>";
echo "<td>".$stand->abc."</td>";
echo "<td>".$stand->def."</td>";
echo "</tr>";
}
// Close the table
echo "</table>";
}
?>
If I understand your question correctly you want to output the properties of the $stand object. You need an additional loop to iterate over the properties.
<?php
$json=file_get_contents("http://megagrup.site/entegrasyon/hepsiburada.php");
$data = json_decode($json);
if (count($data->listings)) {
// Open the table
echo "<table>\n";
// Cycle through the array
foreach ($data->listings as $idx => $stand) {
// Output a row
echo " <tr>\n";
// Output a cell for each property of the $stand object
foreach ($stand as $key => $value) {
echo " <td>" . $value . "</td>\n";
}
echo " </tr\n";
}
// Close the table
echo "</table>\n";
}
?>

PHP Mysql - adding an empty row can't figure it out

Here is the table code - when this is printed out the has one empty row at the bottom. I am not sure why the last row is added? Is it perhaps the if else statement?
<?php
$row = mysql_fetch_array($result);
if ($row['Price'] == '0.00') {
echo "<TABLE width='100%'>\n";
echo "<TR><TD id=tblrowsHead2>Item</TD><TD id=tblrowsHead2>Size</TD><TD
id=tblrowsHead2>Contact</TD></TR>\n";
for ($i = 0; $i < $numofrows; $i++) {
$row = mysql_fetch_array($result); //get a row from our result set
if ($i % 2) { //this means if there is a remainder
echo "<TR bgcolor=\"#E6E6FA\">\n";
} else { //if there isn't a remainder we will do the else
echo "<TR bgcolor=\"white\">\n";
}
echo "<TD id=Size>" . $row['Item'] . "</TD><TD id=Size>" . $row['Size']
. "</TD>
<TD id=Price>Contact Us</TD>\n";
}
} else {
echo "<TABLE width='100%'>\n";
echo "<TR><TD id=tblrowsHead2>Item</TD><TD id=tblrowsHead2>Size</TD><TD
id=tblrowsHead2>Price</TD></TR>\n";
for ($i = 0; $i < $numofrows; $i++) {
$row = mysql_fetch_array($result); //get a row from our result set
if ($i % 2) { //this means if there is a remainder
echo "<TR bgcolor=\"#E6E6FA\">\n";
} else { //if there isn't a remainder we will do the else
echo "<TR bgcolor=\"white\">\n";
}
echo "<TD id=Size>" . $row['Item'] . "</TD><TD id=Size>" . $row['Size']
. "</TD>
<TD id=Price>$" . $row['Price'] . "</TD>\n";
}
}
//now let's close the table and be done with it
echo "</TABLE>\n";
?>
Try this code instead:
<?php
$row = mysql_fetch_array($result);
$i = 0;
if ($row['Price'] == '0.00') {
echo "<TABLE width='100%'>\n";
echo "<TR><TD id=tblrowsHead2>Item</TD><TD id=tblrowsHead2>Size</TD><TD id=tblrowsHead2>Contact</TD></TR>\n";
do {
if ($i % 2) { //this means if there is a remainder
echo "<TR bgcolor=\"#E6E6FA\">\n";
} else { //if there isn't a remainder we will do the else
echo "<TR bgcolor=\"white\">\n";
}
echo '<TD id=Size>'.$row['Item'].'</TD><TD id=Size>'.$row['Size'].'</TD>';
echo "<TD id=Price>Contact Us</TD></TR>\n";
$i++;
} while ($row = mysql_fetch_array($result)); //get a row from our result set
} else {
do {
if ($i % 2) { //this means if there is a remainder
echo "<TR bgcolor=\"#E6E6FA\">\n";
} else { //if there isn't a remainder we will do the else
echo "<TR bgcolor=\"white\">\n";
}
echo '<TD id=Size>'.$row['Item'].'</TD><TD id=Size>'.$row['Size'].'</TD>';
echo "<TD id=Price>$" . $row['Price'] . "</TD></TR>\n";
$i++;
} while ($row = mysql_fetch_array($result)); //get a row from our result set
}
//now let's close the table and be done with it
echo "</TABLE>\n";
When you using the mysql_fetch_array at the first line of for-loop, you will lost the first record of your data-table, so you will have an empty line at the end

i created a table in database to display photos and i made it to display ,but i want them to display from the last to the first

<?php
$conn = mysql_connect("localhost","root","");
if(!$conn){
echo mysql_error();
}
$db = mysql_select_db("imagestore",$conn);
if(!$db ){
echo mysql_error();
}
$q = "SELECT * FROM imagetable";
$r = mysql_query("$q",$conn);
if($r)
{
while($row=mysql_fetch_array($r) )
{
header("Content-type: text/html");
echo "</br>";
echo $row['photoname'];
echo "</br>";
$type = "Content-type: ".$row['phototype'];
header($type);
echo "<img src=image.php?fotoid=". $row['fotoid']."width =300 height = 35. 300/>";
}
}
else{
echo mysql_error();
}
?>
I know ORDER by but is not working to display photos as i want in my page .
I am a beginner .
I used $q = "SELECT * FROM imagetable ORDER BY fotoid DESC";

Php mysql fetch result

I've made a script for movies but i have an issue.
The home of script is contain code like this :
<div class="box">
<div id="movie"></div><div id="movie"></div><div id="movie"></div>
</div>
I've do a mysql query but i don't know how to echo just 3 record per div .
Im using this code for mysql query :
$query=mysql_query("select * from movies where id=$id");
while($row=mysql_fetch_assoc($query))
{
echo $row['name'];
}
You will need something like this I think
$query = mysql_query("select * from movies");
$result = array();
while($r = mysql_fetch_assoc($query)) {
$result[$r['id']] = array($r['name'],$r['thumb']);
}
$i = 0;
foreach($result as $id => $data){
if($i == 0)
{
echo "<div class=\"box\">";
echo "<div id=\"movie\">";
echo "ID: $id";
echo "Name: $data[0]";
echo "Thumb: $data[1]";
echo "</div>";
$i = $i + 1;
}
elseif($i == 1)
{
echo "<div id=\"movie\">";
echo "ID: $id";
echo "Name: $data[0]";
echo "Thumb: $data[1]";
echo "</div>";
$i = $i + 1;
}
elseif($i == 2)
{
echo "<div id=\"movie\">";
echo "ID: $id";
echo "Name: $data[0]";
echo "Thumb: $data[1]";
echo "</div>";
echo "</div>";
$i = 0;
}
}
Multiple elements with the same id? Not a good idea.
You didn't say what you want to happen if you're query returns less then 3 rows.
You might try:
...
$x=0;
while($row=mysql_fetch_assoc($query) && ++$x<=3) {
echo $row['name'];
}