Display database table in PHP array - php

I am trying to display my table from my MySQL database. It doesn't seem to work, I guess it's most likely something simple. If possible I would like to have the table look like just a normal table with no frills; just a border and to be able to fit in a normal sized page. This is what I have so far:
<html>
<head>
<title>Profile Database </title>
</head>
<body>
<?
$connection = "connect.php";
require $connection;
mysql_select_db("bank");
$sql = "SELECT * FROM profiles";
$result = mysql_query($sql);
?>
<table border="2" style= "background-color: #84ed86; color: #761a9b; margin: 0 auto;" >
<thead>
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>E-Mail</th>
<th>DOB</th>
<th>Age</th>
<th>City</th>
<th>State</th>
<th>Zip Code</th>
<th>Offence</th>
<th>Notes</th>
</tr>
</thead>
<tbody>
<?php
while( $row = mysql_fetch_assoc( $result ))
{
echo <tr>
<td>{$row\['id'\]}</td>
<td>{$row\['fname'\]}</td>
<td>{$row\['lname'\]}</td>
<td>{$row\['email'\]}</td>
<td>{$row\['dob'\]}</td>
<td>{$row\['age'\]}</td>
<td>{$row\['city'\]}</td>
<td>{$row\['state'\]}</td>
<td>{$row\['zip'\]}</td>
<td>{$row\['offence'\]}</td>
<td>{$row\['notes'\]}</td>
</tr>\n;
}
?>
</tbody>
</table>
<?php
mysql_close($connection);
?>
</body>
</html>

Give this a go:
<table>
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>E-Mail</th>
<th>DOB</th>
<th>Age</th>
<th>City</th>
<th>State</th>
<th>Zip Code</th>
<th>Offence</th>
<th>Notes</th>
</tr>
<?php
require "connect.php";
mysql_select_db("bank");
$result = mysql_query("SELECT * FROM profiles");
while($row = mysql_fetch_array($result, MYSQL_NUM)) {
echo '<tr>';
foreach($row as $column) {
echo '<td>', $column, '</td>';
}
echo '</tr>';
}
?>
</table>

WHILE($row = mysql_fetch_array($result))
should be either
WHILE($row = mysql_fetch_array($result, MYSQL_ASSOC))
OR
WHILE($row = mysql_fetch_assoc($result))
Also the mysql_* functions have been deprecated and relying on them is HIGHLY discouraged.
Kindly check the below discussions :-
Deprecated MySql Functions
How to successfully rewrite old mysql-php code with deprecated mysql_* functions?
http://php.net/manual/en/migration55.deprecated.php
Use either MySQLi or PDO.

<center><table border="1">
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>E-Mail</th>
<th>DOB</th>
<th>Age</th>
<th>City</th>
<th>State</th>
<th>Zip Code</th>
<th>Offence</th>
<th>Notes</th>
</tr>
<?
$connection = "connect.php";
require $connection;
mysql_select_db("bank");
$sql = "SELECT * FROM profiles";
$result = mysql_query($sql);
WHILE($row = mysql_fetch_array($result))
{
$id = $row ['id'];
$firstname = $row ['fname'];
$lastname = $row ['lname'];
$email = $row ['email'];
$dob = $row ['dob'];
$age = $row ['age'];
$city = $row ['city'];
$state = $row ['state'];
$zip = $row ['zip'];
$offence = $row ['offence'];
$nots = $row ['notes'];
echo '<tr>'; // change here
echo '<td>'.$id.'</td>';
echo '<td>'.$firstname.'</td>';
echo '<td>'.$lastname.'</td>';
echo '<td>'.$email.'</td>';
echo '<td>'.$dob.'</td>';
echo '<td>'.$age.'</td>';
echo '<td>'.$city.'</td>';
echo '<td>'.$state.'</td>';
echo '<td>'.$zip.'</td>';
echo '<td>'.$offence.'</td>';
echo '<td>'.$notes.'</td>';
echo '</tr>'; // change here
}
mysql_close();
?>
</table></center>

Try this code:
<?php
$username = "your_name";
$password = "your_password";
$hostname = "localhost";
$dbconnect = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL");
$select=mysql_select_db("bank",$dbconnect) or die("Could not select bank");
$sql = "SELECT * FROM profiles";
$result = mysql_query($sql);
?>
<center><table border="1">
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>E-Mail</th>
<th>DOB</th>
<th>Age</th>
<th>City</th>
<th>State</th>
<th>Zip Code</th>
<th>Offence</th>
<th>Notes</th></tr>
<?php
while($row = mysql_fetch_array($result))
{
$id = $row ['id'];
$firstname = $row ['fname'];
$lastname = $row ['lname'];
$email = $row ['email'];
$dob = $row ['dob'];
$age = $row ['age'];
$city = $row ['city'];
$state = $row ['state'];
$zip = $row ['zip'];
$offence = $row ['offence'];
$nots = $row ['notes'];
echo '<tr>';
echo '<td>'.$id.'</td>';
echo '<td>'.$firstname.'</td>';
echo '<td>'.$lastname.'</td>';
echo '<td>'.$email.'</td>';
echo '<td>'.$dob.'</td>';
echo '<td>'.$age.'</td>';
echo '<td>'.$city.'</td>';
echo '<td>'.$state.'</td>';
echo '<td>'.$zip.'</td>';
echo '<td>'.$offence.'</td>';
echo '<td>'.$notes.'</td>';
echo '</tr>';
}
?>
</table></center>
<?php
mysql_close();
?>

Corrected Code:
<html>
<head>
<title>Profile Database </title>
</head>
<body>
<?php
$connection = "connect.php";
require $connection;
mysql_select_db("bank");
$sql = "SELECT * FROM profiles";
$result = mysql_query($sql);
?>
<table border="2" style= "background-color: #84ed86; color: #761a9b; margin: 0 auto;" >
<thead>
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>E-Mail</th>
<th>DOB</th>
<th>Age</th>
<th>City</th>
<th>State</th>
<th>Zip Code</th>
<th>Offence</th>
<th>Notes</th>
</tr>
</thead>
<tbody>
<?php
while( $row = mysql_fetch_assoc( $result ))
{
?>
<tr>
<td><?php echo $row['id'];?></td>
<td><?php echo $row['fname'];?></td>
<td><?php echo $row['lname'];?></td>
<td><?php echo $row['email'];?></td>
<td><?php echo $row['dob'];?></td>
<td><?php echo $row['age'];?></td>
<td><?php echo $row['city'];?></td>
<td><?php echo $row['state'];?></td>
<td><?php echo $row['zip'];?></td>
<td><?php echo $row['offence'];?></td>
<td><?php echo $row['notes'];?></td>
</tr>
<?php
}
?>
</tbody>
</table>
<?php
mysql_close($connection);
?>
</body>
</html>

Related

Passing ID variable to the modal page to perform date filter by include this php page

I have no idea why this is not working. Maybe I'm lack of PHP experience but I tried to follow as much as I can from the one which works, but it doesn't work.
What I wish to perform is that getting the ID from the list of page after the user presses the button and pass the variable to the bootstrap modal page. I manage to figure out on displaying the table of the ID.
But when it comes to date filtering, it has failed.
<?php
require_once 'dbconfig.php';
if (isset($_REQUEST['id'],$_POST["From"], $_POST["to"])) {
$result = '';
$id = intval($_REQUEST['id'],$_POST["From"], $_POST["to"]);
$query = "SELECT * FROM history WHERE ID=:id and date BETWEEN '".$_POST["From"]."' AND '".$_POST["to"]."'";
//
$stmt = $DBcon->prepare( $query );
$stmt->execute(array(':id'=>$id));
$row=$stmt->fetch(PDO::FETCH_ASSOC);
extract($row);
$result .='
<table class="table table-bordered">
<tr>
<th width="10%">Order Number</th>
<th width="35%">Customer Number</th>
<th width="40%">Purchased Item</th>
<th width="10%">Purchased Date</th>
</tr>';
while($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$result .='
<tr>
<td>'.$row["ID"].'</td>
<td>'.$row["location"].'</td>
<td>'.$row["currentStatus"].'</td>
<td>'.$row["date"].'</td>
</tr>';
}
$result .='</table>';
echo $result;
}?>
But it is working this way:
<?php
require_once 'dbconfig.php';
if (isset($_REQUEST['id'])) {
$id = intval($_REQUEST['id']);
$query = "SELECT * FROM history WHERE ID=:id";
$stmt = $DBcon->prepare( $query );
$stmt->execute(array(':id'=>$id));
$row=$stmt->fetch(PDO::FETCH_ASSOC);
extract($row);
}
?>
<div id="purchase_order">
<table class="table table-bordered">
<tr>
<th width="10%">ID</th>
<th width="20%">Location</th>
<th width="20%">Status</th>
<th width="10%">Date</th>
</tr>
<?php
// while($row= mysqli_fetch_array($sql))
// {
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
?>
<tr>
<td><?php echo $row["ID"]; ?></td>
<td><?php echo $row["location"]; ?></td>
<td><?php echo $row["currentStatus"]; ?></td>
<td><?php echo $row["date"]; ?></td>
</tr>
<?php
}
?>
I believe the problem is with your use of date in your SQL.
<?php
require_once 'dbconfig.php';
// enable error reporting
error_reporting(E_ALL);
ini_set('display_errors', 1);
if (isset($_REQUEST['id'], $_POST['From'], $_POST['to'])) {
$result = '';
// intval() only has 2 arguments
$id = intval($_REQUEST['id']);
$from = $_POST['From'];
$to = $_POST['to'];
// more likely the problem: date is a reserved word in MySQL -- so wrap it in backquotes
// also, use placeholders for From and To
$query = "SELECT * FROM history WHERE ID = ? AND `date` BETWEEN ? AND ?";
$stmt = $DBcon->prepare($query);
$stmt->execute([$id, $from, $to])); // simplified
$row = $stmt->fetch(PDO::FETCH_ASSOC);
// unnecessary and dangerous
//extract($row);
$result .= '
<table class="table table-bordered">
<tr>
<th width="10%">Order Number</th>
<th width="35%">Customer Number</th>
<th width="40%">Purchased Item</th>
<th width="10%">Purchased Date</th>
</tr>';
// you already fetched the row - no need to do it again
if ($row) {
$result .= '
<tr>
<td>'.$row["ID"].'</td>
<td>'.$row["location"].'</td>
<td>'.$row["currentStatus"].'</td>
<td>'.$row["date"].'</td>
</tr>';
}
$result .= '</table>';
echo $result;
}
?>
Useful links:
PDO and MySQL 'between'
Is “Date” a keyword in mysql?
Converting to boolean

How can I add bid option in table?

I am beginner in php...
How to create a bids option from the table:
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Current Location</th>
<th>Country</th>
<th>State</th>
<th>City</th>
<th>Preferred Treatment Location</th>
<th>Country</th>
<th>State</th>
<th>City</th>
<th>Treatment & Surgery</th>
<th>Enter date</th>
<th>Medical Condition of Patient</th>
<th>BIDS</th>
</tr>
<?php
//Create Connection with MySQL Database
$con = mysqli_connect('localhost','root','root123');
//Select Database
if(!mysqli_select_db($con,'medical'))
{
echo "Database Not Selected";
}
//Select Query
$sql = "SELECT * FROM user_detail";
//Execute the SQL query
$records = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($records))
{
echo "<tr>";
echo "<td>".$row['id']."</td>";
echo "<td>".$row['fname']."</td>";
echo "<td>".$row['lname']."</td>";
echo "<td>".$row['clocation']."</td>";
echo "<td>".$row['country']."</td>";
echo "<td>".$row['state']."</td>";
echo "<td>".$row['city']."</td>";
echo "<td>".$row['treatment']."</td>";
echo "<td>".$row['country1']."</td>";
echo "<td>".$row['state1']."</td>";
echo "<td>".$row['city1']."</td>";
echo "<td>".$row['surgery']."</td>";
echo "<td>".$row['date']."</td>";
echo "<td>".$row['medicond']."</td>";
}
?>
</table>
Can you please help...

PHP - How can I nest a while loop inside an if isset condition?

I have this table element with the following code:
<?php
if(isset($_POST["submit"])){
if (strlen($cIdMsg = 0) && strlen($cFirstNameMsg = 0) && strlen($cLastNameMsg = 0) && strlen($pCodeMsg = 0)) {
require_once("conn.php");
$sql2 = "SELECT * FROM customer;";
$results = mysqli_query($conn, $sql2)
or die ('Problem with query' . mysqli_error($conn));
echo "no errors found";
}
}
?>
<table>
<tr>
<th>Customer ID</th>
<th>FIrst Name</th>
<th>Last Name </th>
</tr>
<?php
while ($row = mysqli_fetch_array($results)) { ?>
<tr>
<td><?php echo $row["customerID"]?></td>
<td><?php echo $row["firstName"]?></td>
<td><?php echo $row["lastName"]?></td>
</tr>
<?php } ?>
</table>
Above this table I have the php code that makes the sql queries inside an if isset condition so that it only loads after pressing submit on the form. I would like to do the same to the table. That is to only make it load after pressing submit. because on page load it is trying to do the mysqli_fetch_array on a non existent $result yet
Wrap the whole table inside:
<?php if (isset($result)) { ?>
<table>
<tr>
<th>Customer ID</th>
<th>FIrst Name</th>
<th>Last Name </th>
</tr>
<?php
while ($row = mysqli_fetch_array($results)) { ?>
<tr>
<td><?php echo $row["customerID"]?></td>
<td><?php echo $row["firstName"]?></td>
<td><?php echo $row["lastName"]?></td>
</tr>
<?php } ?>
</table>
<?php } ?>
I have used isset($result) based on what you have said. You can check for the POST values by checking for count($_POST), or something similar (not a good idea to check for isset($_POST["submit"])). If you are fetching for AJAX Response, it is always better to use a different separate file for this.
<?php if(mysqli_num_rows($result)!=0) { ?>
<table>
<tr>
<th>Customer ID</th>
<th>FIrst Name</th>
<th>Last Name </th>
</tr>
<?php
while ($row = mysqli_fetch_array($results)) { ?>
<tr>
<td><?php echo $row["customerID"]?></td>
<td><?php echo $row["firstName"]?></td>
<td><?php echo $row["lastName"]?></td>
</tr>
<?php } ?>
</table>
<?php } ?>

Put dropdown box and image link in table

<html>
<style>
table {
border-collapse: collapse;
}
table,th,td{
border: 1px solid black;
border-collapse: collapse;
}
tr:hover {background-color: #f5f5f5}
th,tr {
text-align: center;
}
table {
margin-left: auto;
margin-right: auto;
width: 75%;
}
th, td {
padding: 15px;
}
</style>
</html>
<?php
include 'dbLink.php';
include 'navi.php';
$sql = "SELECT * FROM employee";
$query = "SELECT * FROM employeerole ORDER BY role_id";
$result = mysqli_query($link, $sql);
$result1 = mysqli_query($link, $query);
$rowcount = mysqli_num_rows($result);
if ($rowcount > 0)
{?>
How do I input a drop down list and a image link in each table row? Also, when i run this page, only the data in the first row is reflected in the table. The rest of the data is not in the table. Any fixes?
<table>
<tr>
<th>Employee ID</th>
<th>First name</th>
<th>Last Name</th>
<th>Username</th>
<th>Employee Role</th>
<th>Edit role</th>
<th>Delete User</th>
</tr>
<?php
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{?>
<tr>
<td><?php echo $row["id"];?></td>
<td><?php echo $row["first_name"];?></td>
<td><?php echo $row["last_name"];?></td>
<td><?php echo $row["username"];?></td>
<td><?php echo $row["role"];?></td>
<td>//dropdwon list with data from employeerole table</td>
<td>//image link</td>
</tr>
</table>
<?php}?>
<?php
}
}
else
{
echo "0 results";
}
?>
Screenshot
You have mixed Object Oriented Style & Procedural Style for writing query. So, be particular about Object Oriented or Procedural way of writing query.
I've written a code for you in both way. Go Ahead.
1) Object Oriented Style
dbLink.php
<?
$link = new mysqli("localhost", "my_user", "my_password", "world");
?>
Edited Code
$sql = "SELECT * FROM employee";
$query = "SELECT * FROM employeerole ORDER BY role_id";
$result = $link->query($sql);
$result1 = $link->query($query);
$rowcount = $result->num_rows;
if ($rowcount > 0)
{?>
<table>
<tr>
<th>Employee ID</th>
<th>First name</th>
<th>Last Name</th>
<th>Username</th>
<th>Employee Role</th>
<th>Edit role</th>
<th>Delete User</th>
</tr>
<?php
while ($row = $result->fetch_array(MYSQLI_ASSOC))
{?>
<tr>
<td><?php echo $row["id"];?></td>
<td><?php echo $row["first_name"];?></td>
<td><?php echo $row["last_name"];?></td>
<td><?php echo $row["username"];?></td>
<td><?php echo $row["role"];?></td>
<td></td>
<td></td>
</tr>
<?php}?>
</table>
<?php }
else
{
echo "0 results";
}
?>
2) Procedural Style
dbLink.php
<?
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
?>
Edited Code
$sql = "SELECT * FROM employee";
$query = "SELECT * FROM employeerole ORDER BY role_id";
$result = mysqli_query($link, $sql);
$result1 = mysqli_query($link, $query);
$rowcount = mysqli_num_rows($result);
if ($rowcount > 0)
{?>
<table>
<tr>
<th>Employee ID</th>
<th>First name</th>
<th>Last Name</th>
<th>Username</th>
<th>Employee Role</th>
<th>Edit role</th>
<th>Delete User</th>
</tr>
<?php
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{?>
<tr>
<td><?php echo $row["id"];?></td>
<td><?php echo $row["first_name"];?></td>
<td><?php echo $row["last_name"];?></td>
<td><?php echo $row["username"];?></td>
<td><?php echo $row["role"];?></td>
<td></td>
<td></td>
</tr>
<?php }?>
</table>
<?php }
else
{
echo "0 results";
}
?>
User's Requirement : Updated Code
<?
$result = mysqli_query($link, $sql);
$result1 = mysqli_query($link, $query);
$rowcount = mysqli_num_rows($result);
if ($rowcount > 0)
{?>
<table>
<tr>
<th>Employee ID</th>
<th>First name</th>
<th>Last Name</th>
<th>Username</th>
<th>Employee Role</th>
<th>Edit role</th>
<th>Delete User</th>
</tr>
<?php
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{?>
<tr>
<td><?php echo $row["id"];?></td>
<td><?php echo $row["first_name"];?></td>
<td><?php echo $row["last_name"];?></td>
<td><?php echo $row["username"];?></td>
<td><?php echo $row["role"];?></td>
<td>//dropdwon list with data from employeerole table</td>
<td>//image link</td>
</tr>
<?php }?>
</table>
<?php
} else {
echo "0 results";
}
?>

Displaying MySQL data in a table

I'm trying to neatly organize data from my MySQL database into tables. I'm almost there, but experiencing some troubles with my use of a while loop.
Posting my simple code:
<link type="text/css" rel="stylesheet" href="style.css"/>
<?PHP
$connect = mysql_connect("localhost","root","");
mysql_select_db("users");
$query = mysql_query("SELECT * FROM users");
WHILE($rows = mysql_fetch_array($query)):
$username = $rows['username'];
$password = $rows['password'];
$email = $rows['email'];
echo "
<table>
<thead>
<th>Username</th>
<th>Password</th>
<th>Email Adress</th>
</thead>
<tr>
<td>$username</td>
<td>$password</td>
<td>$email</td>
";
endwhile;
?>
This gives me an output of something like this:
Username Password Email Adress
test 123 test#test.no
Username Password Email Adress
testies 123 testies#test.no
Username Password Email Adress
tested 12345 tested#test.no
The obvious problem here is that my headers are printed inside the while loop, and thus I get new headers for every entry. This looks really stupid though, and I would prefer to have the headers only appear once. Any ideas on how to do this?
I have tried to start the table with headers before the while loop, but then the later <td>s wont correspond with the headers width, since the program doesn't recognize them as the same table, and thus not needing to match.
Use this
<link type="text/css" rel="stylesheet" href="style.css"/>
<?PHP
$connect = mysql_connect("localhost","root","");
mysql_select_db("users"); ?>
<table>
<thead>
<th>Username</th>
<th>Password</th>
<th>Email Adress</th>
</thead><?php
$query = mysql_query("SELECT * FROM users");
WHILE($rows = mysql_fetch_array($query)) { ?>
<tr>
<td><?php echo $rows['username']; ?></td>
<td><?php echo $rows['password']; ?></td>
<td><?php echo $rows['email']; ?></td>
</tr>
<?php } ?>
?>
change your code as follows.
<?PHP
$connect = mysql_connect("localhost","root","");
mysql_select_db("users");
$query = mysql_query("SELECT * FROM users");
echo "
<table>
<thead>
<th>Username</th>
<th>Password</th>
<th>Email Adress</th>
</thead>";
WHILE($rows = mysql_fetch_array($query)):
$username = $rows['username'];
$password = $rows['password'];
$email = $rows['email'];
echo"<tr>
<td>$username</td>
<td>$password</td>
<td>$email</td>
";
endwhile;
?>
print the headers before the loop.
<table>
<thead>
<tr>
<th>Username</th>
<th>Password</th>
<th>Email Adress</th>
</tr>
</thead>
<?php
$connect = mysql_connect("localhost","root","");
mysql_select_db("users");
$query = mysql_query("SELECT * FROM users");
WHILE($rows = mysql_fetch_array($query)):
$username = $rows['username'];
$password = $rows['password'];
$email = $rows['email'];
echo "
<tr>
<td>$username</td>
<td>$password</td>
<td>$email</td>
</tr>
";
endwhile;
?>
</table>
Try this:
<link type="text/css" rel="stylesheet" href="style.css"/>
<?php
$connect = mysql_connect("localhost","root","");
mysql_select_db("users");
$query = mysql_query("SELECT * FROM users");
?>
<table>
<thead>
<th>Username</th>
<th>Password</th>
<th>Email Adress</th>
</thead>";
<?php
WHILE($rows = mysql_fetch_array($query)):
$username = $rows['username'];
$password = $rows['password'];
$email = $rows['email'];
echo "<tr>
<td>$username</td>
<td>$password</td>
<td>$email</td>
";
endwhile;
?>

Categories