Displaying MySQL data in a table - php

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;
?>

Related

How to fix a table causing "502: Bad Gateway" error in PHP

I am creating a simple database that can do basic CRUD operations (create, read, update, delete) using php. I am able to complete the create, and able to see the results if I directly query the mySQL DB in the back end. But, I am having trouble getting the table to display on the webpage. It is instead displaying a "Bad Gateway" error if I attempt to display the database entries.
I tried removing the reference to the table, specifically
<?php while ($row = mysqli_fetch_array($results)) { ?>
<tr>
<td><?php echo $row['name']; ?></td>...
and the web page on front end works fine. Albeit can only see the data if I query the backend.
<?php include('php_code.php'); ?>
...
...
...
<?php $results = mysqli_query($db, "SELECT * FROM info"); ?>
<table>
<thead>
<tr>
<th>Name</th>
<th>Address</th>
<th>City</th>
<th colspan="2">Action</th>
</tr>
</thead>
<?php while ($row = mysqli_fetch_array($results)) { ?>
<tr>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['address']; ?></td>
<td><?php echo $row['city']; ?></td>
<td>
<a href="test.php?edit=<?php echo $row['id']; ?>"
class="edit_btn" >Edit</a>
</td>
<td>
<a href="php_code.php?del=<?php echo $row['id']; ?>"
class="del_btn">Delete</a>
</td>
</tr>
<?php } ?>
</table>
<!--in php_code.php-->
//to retrieve records
$select_query = "SELECT * FROM info";
$result = mysqli_query($db, $select_query);
I should be able to see the table with data containing name, address and city. But I am getting a 502 error instead.
Try this
<?php
include('php_code.php');
$results = mysqli_query($db, "SELECT * FROM `info` ");
$return = <<<HTML
<table>
<thead>
<tr>
<th>Name</th>
<th>Address</th>
<th>City</th>
<th colspan="2">Action</th>
</tr>
</thead>
<tbody>
HTML;
while ($row = mysqli_fetch_array($results)) {
$return .= <<<HTML
<tr>
<td>{$row['name']}</td>
<td>{$row['address']}</td>
<td>{$row['city']}</td>
<td><a href="test.php?edit={$row['id']}" class="edit_btn" >Edit</a></td>
<td>Delete</td>
</tr>
HTML;
}
$return .= <<<HTML
</tbody>
</table>
HTML;
echo $return;
?>
your php_code.php should really only have the database config...
you are closing the php statements so you cannot retrieve the result of your query. Don't split php parts and just echo html like this
<?php
echo " <table>
<thead>
<tr>
<th>Name</th>
<th>Address</th>
<th>City</th>
<th colspan='2'>Action</th>
</tr>
</thead> ";
while ($row = mysqli_fetch_array($results)) {
echo "<tr>
<td>$row['name']</td>
<td>$row['address']</td>
<td> $row['city']</td>
<td>
<a href='test.php?edit=$row['id']'
class='edit_btn' >Edit</a>
</td>
<td>
<a href='php_code.php?del=$row['id']'
class='del_btn'>Delete</a>
</td>
</tr>";
}
echo "</table>";
?>

Print all table data from mysql into a html table

I've been trying to print all users into a html table. The objective is that the site administrator can edit the information.
I've done the following code so far, but I can't understand how I will print User1_Name, User1_Email and password into a table row. I want this for every user on the database.
<body>
<div class="edituser">
<h1> Edit Users </h1>
<table>
<tr>
<th>User Name</th>
<th>Email</th>
<th>Password</th>
</tr>
<?php include("include/headerhomeadmin.php");?>
<?php while($row = mysqli_fetch_assoc($result)) {
/*$line[]=$row;*/
echo
$userusername =$row['username'];
$useremail =$row['email'];
$userpassword=$row['password'];
?>
<td><?php echo implode(" ", $line)?></td>
<?php } ?>
</table>
</div>
Can someone help me?
UPDATE:
My code is now printing the first username, the email from the second user and the password from de third user.
The code is the following:
<body>
<div class="edituser">
<h1> Editar Utilizadores </h1>
<table>
<tr>
<th>Nome de Utilizador</th>
<th>Email</th>
<th>Password</th>
</tr>
<tr>
<?php while($row = mysqli_fetch_assoc($result)) {
$userusername =$row['username'];
?>
<td> <?php echo $userusername ?> </td>
<?php while($row = mysqli_fetch_assoc($result)) {
$useremail =$row['email'];
?>
<td> <?php echo $useremail ?> </td>
<?php while($row = mysqli_fetch_assoc($result)) {
$userpassword=$row['password'];
?>
<td> <?php echo $userpassword ?> </td>
<?php }
}
}?>
How can I fix this?
if ($result->num_rows > 0) {?>
<table border=1>
<tr>
<th>Name</th>
<th>Email</th>
<th>Password</th>
</tr>
<?php while($row = mysqli_fetch_assoc($result)) { ?>
<tr>
<td><?php echo $row['username']; ?></td>
<td><?php echo $row['email']; ?></td>
<td><?php echo $row['password']; ?></td>
</tr>
<?php }?>
</table>
<?php
}
else{
echo "0 results";
}
?>

How to create pie chart using PHP and MYSQL?

I have a table which shows my data and it works perfectly. I need to add in a pie chart also to show the exact same data that was shown on the table.
<thead>
<tr>
<th>Account Type</th>
<th>Account Number</th>
<th>Account Balance</th>
</tr>
</thead>
<?php
$query = "SELECT * FROM account WHERE user_id='$user_id'";
$result = mysqli_query($link, $query) or die(mysqli_error($link));
while ($row = mysqli_fetch_array($result)) {
$acc_type = $row ['account_type'];
$acc_num = $row ['account_number'];
$acc_bal = $row ['account_balance'];
?>
<tbody>
<tr>
<td><?php echo $acc_type ?></td>
<td><?php echo $acc_num ?></td>
<td>$ <?php echo $acc_bal ?></td>
<?php
}
?>
</tr>
</tbody>
<tr class="alt">
<?php
$query = "SELECT SUM(account_balance) AS account_total FROM account WHERE user_id='$user_id'";
$result = mysqli_query($link, $query) or die(mysqli_error($link));
while ($row = mysqli_fetch_array($result)){
$acc_total = $row ['account_total'];
?>
<td colspan="2" align="right"><b>Total: </b></td>
<td align="right"><b>$ <?php echo $acc_total ?> </b></td>
<?php
}
?>
</tr>
Use a 3rd party.
Here is one. Look at their Example #15 - Basic Pie graphs
http://pchart.sourceforge.net/documentation.php?topic=exemple15
Why don't you try the Google graphs, that is the easiest way.
Check out the link
https://developers.google.com/chart/interactive/docs/gallery/piechart
If anyone is still looking for a solution I really just went to this site
https://developers.google.com/chart/interactive/docs/drawing_charts
and echo the whole code and it works.

Display database table in PHP array

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>

displaying user's records from database

<?php
include "db.php";
$username=$_POST['username'];
$email=$_POST['email'];
$query="SELECT * FROM members where username = '".mysql_real_escape_string($username)."'";
$result=mysql_query($query)or die(mysql_error());
$user = mysql_fetch_assoc($result);
mysql_close();
?> <br /> <p></p>
Welcome back! Your details below: <br /><br />
<table border="1" cellspacing="2" cellpadding="5">
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>User Name</th>
<th>Email</th>
<th>Age</th>
</tr>
<?php
$firstname= $user['firstname'];
$lastname= $user['lastname'];
$username= $user['username'];
$email= $user['email'];
$age= $user['age'];
?>
<tr>
<td><? echo $firstname ?></td>
<td><? echo $lastname ?></td>
<td><? echo $username ?></td>
<td><? echo $email ?></td>
<td><? echo $age ?></td>
</tr>
</table>
guys, i use this code to display the user's details, BUT STILL its not displaying the records.
what's wrong with this code? hmm... there's no error, but its not working.
:'(
Try adding an echo(mysql_error()) to see if there's a MySQL error beyond just a bad query.
Try adding a loop like this below replacing the single record set with multiple. Also check your query and see if it's correct.
<?php
include "db.php";
$username=$_POST['username'];
$email=$_POST['email'];
$query="SELECT * FROM members where username = '".mysql_real_escape_string($username)."'";
$result=mysql_query($query)or die(mysql_error());
//$user = mysql_fetch_assoc($result);
?>
<br /> <p></p>
Welcome back! Your details below: <br /><br />
<table border="1" cellspacing="2" cellpadding="5">
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>User Name</th>
<th>Email</th>
<th>Age</th>
</tr>
<?php
while($user=mysql_fetch_array($result))
{
echo '<tr>';
echo '<td>'.$user['firstname'].'</td>
<td>'.$user['lastname'].'</td>
<td>'.$user['username'].'</td>
<td>'.$user['email'].'</td>
<td>'.$user['age'].'</td>';
echo '</tr>';
}
mysql_close();
?>
</table>
Put error_reporting(~0) at the very top to make sure you get reports on everything going less than perfect. Do a print_r($_POST) to make very sure you get in $_POST what you think you are getting.
If you then still haven't found the problem, provide more context! (like: script output)
(btw: you should echo htmlspecialchars($user[...]); or people can put very nasty stuff in there.)
And make sure you have "short_open_tag = On" in your php.ini, or use
<?php
instead of
<?
everywhere!

Categories