Displaying results from mysql database in a html/php table - php

Hi I've got a page where I can view data output from a mysql database. It works good. But it's not stylish. So I decided to put in a html <table> tag. However it's not displaying the data.
I've tried putting in
<td> </td> etc between rows but it stops the code being parsed.
<?php
$servername = "******";
$username = "root";
$password = "********";
$dbname = "test2";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT first_name, last_name, email FROM person_list";
$result = $conn->query($sql);
//display table
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>";
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo
"<br> ". $row["first_name"]. " "
. $row["last_name"] ." "
. $row["email"] ." " ;
}
}
else {
echo "0 results";
}
echo "</table>";
$conn->close();
?>

Try this one, if it works for you..
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>";
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>". $row["first_name"] . "</td>";
echo "<td>". $row["last_name"] . "</td>";
echo "<td>". $row["email"] . "</td> " ;
echo "</tr>";
}
}
echo "</table>";

You might also be intrested in the foreach way.
Foreach would be quicker in larger data sets, but this is very similar to Narendrasingh Sisodia answer.
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>";
if($result->num_rows > 0){
foreach($result as $row){
echo "<tr>";
echo "<td>". $row["first_name"] . "</td>";
echo "<td>". $row["last_name"] . "</td>";
echo "<td>". $row["email"] . "</td> " ;
echo "</tr>";
}
}
echo "</table>";

Related

I'm trying to display data from a MySQL database onto an HTML table but doesn't register the column fields

I am new to PHP coding and don't understand where I have gone wrong, I'm trying to display data from a MySQL database onto an HTML table but doesn't register the column fields, keeps coming up with Notice: Undefined index: First, Notice: Undefined index: last etc. How do I define my column fields?
Edited: here are links to pictures of my database and the error codes I'm receiving: http://imgur.com/E5uohjr
http://imgur.com/BGDn9SQ
Here is my code:
</head>
$con = mysqli_connect('localhost', 'root', '', 'form_database') or die("Can not connect: " . mysqli_error());
$result = mysqli_query($con,"SELECT first, last, phone, class FROM form_submissions");
echo "<table border=1>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Phone Number</th>
<th>Class interested in</th>
</tr>";
while($row = mysqli_fetch_assoc($results)){
echo "<tr>";
echo "<td>" . $row['first'] . "</td>";
echo "<td>" . $row['last'] . "</td>";
echo "<td>" . $row['phone'] . "</td>";
echo "<td>" . $row['class'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
</body>
check if you do have results before displaying, them.
<?php
// Create connection
$con = mysqli_connect('localhost', 'root', '', 'form_database');
// Check connection
if (!$con) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM form_submissions";
$result = mysqli_query($con, $sql);
//check if you get any results
if (mysqli_num_rows($result) > 0) {
echo "<table border=1>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Phone Number</th>
<th>Class interested in</th>
</tr>";
// output data of each row
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>" . $row['first'] . "</td>";
echo "<td>" . $row['last'] . "</td>";
echo "<td>" . $row['phone'] . "</td>";
echo "<td>" . $row['class'] . "</td>";
echo "</tr>";
}
echo "</table>";
} else {
echo "No results found";
}
mysqli_close($con);

Execute MySQL Delete on Button Click

In my code, I am showing a table within my database called staff in a HTML table and I want to add a delete button to each row in the HTML table that when clicked, it will delete the record its associated with.
Based on searching other solutions, my code looks like this:
staff.php:
require_once('../connection.php');
//delete row on button click
if(isset($_GET["del"])){
$idc = $_GET["del"];
if($VisitorManagement->query("DELETE FROM staff WHERE id=$idc")){
header('Location: delete-thankyou.php');
} else {
echo "Failed to delete staff member.";
}
}
$result = mysqli_query($VisitorManagement, "SELECT * FROM staff ORDER BY fullName");
echo "<table id='staff'>
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th></th>
</tr>
</thead>";
while($row = mysqli_fetch_array($result))
{
echo "<tbody>";
echo "<tr>";
echo "<td>" . $row['fullName'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td><a class='button alert' href='staff.php?del=".$row["idc"]."'>Delete</a></td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
connection.php:
$hostname_VisitorManagement = "localhost";
$database_VisitorManagement = "visitor-management";
$username_VisitorManagement = "***";
$password_VisitorManagement = "***";
$VisitorManagement = mysqli_connect($hostname_VisitorManagement, $username_VisitorManagement, $password_VisitorManagement, $database_VisitorManagement);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
date_default_timezone_set('America/New_York');
Unfortunately, when I go to the click one of the buttons, it fails to delete the record and echoes the error message pre-defined in staff.php. Am I missing something to get this to work?
I was able to fix it by changing all instances of idc to id.
New code is:
require_once('../connection.php');
//delete row on button click
if(isset($_GET["del"])){
$id = $_GET["del"];
if($VisitorManagement->query("DELETE FROM staff WHERE id=$id")){
header('Location: delete-thankyou.php');
} else {
echo "Failed to delete staff member.";
}
}
$result = mysqli_query($VisitorManagement, "SELECT * FROM staff ORDER BY fullName");
echo "<table id='staff'>
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th></th>
</tr>
</thead>";
while($row = mysqli_fetch_array($result))
{
echo "<tbody>";
echo "<tr>";
echo "<td>" . $row['fullName'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td><a class='button alert' href='staff.php?del=".$row["id"]."'>Delete</a></td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";

display records in table

I am having some trouble with my first PHP project, I am trying to get the data from MySQL database (Has 3 records) and display it in tables. Problem is it only seem to display records 2 and 3, it skips the 1st record. Please see my code and display below.
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM unitstats");
while($row = mysqli_fetch_array($result)) {
echo "<table border='1' style='color:white'>
<tr>
<th>ID</th>
<th>Name</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "</tr>";
}
echo "</table>";
}
you are using two while loop which is unnecessary use following code
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
echo "</table>";
echo "<table border='1' style='color:white'>
<tr>
<th>ID</th>
<th>Name</th>
</tr>";
$result = mysqli_query($con,"SELECT * FROM unitstats");
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "</tr>";
}
echo "</table>";
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
echo "<table border='1' style='color:white'>
<tr>
<th>ID</th>
<th>Name</th>
</tr>";
$result = mysqli_query($con,"SELECT * FROM unitstats");
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "</tr>";
}
echo "</table>";
Basically you didn't need the initial loop, this would have mainly caused an issue because you would have been redeclaring $row with the second loop.
Just remove your outer while loop and just use this:
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM unitstats");
echo "<table border='1' style='color:white'>
<tr>
<th>ID</th>
<th>Name</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "</tr>";
}
echo "</table>";

Retrieving Mysql data fails... Why?

So this is my code but it does not work, I just get this
Title Article "; while($row = mysqli_fetch_array($result)) { echo ""; echo "" . $row['Title'] . ""; echo "" . $row['Article'] . ""; echo ""; } echo ""; mysqli_close($con); ?>
Any ideas?
<?php
$con=mysqli_connect("localhost","____","____","test_database");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM written");
echo "<table border='1'>
<tr>
<th>Title</th>
<th>Article</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['Title'] . "</td>";
echo "<td>" . $row['Article'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Tripple check your quotation marks are set correctly. Either
echo "<table border='1'>
or
</tr>";
has an error in it.
You may also try using echo as a function (bracelets), it should make spotting the error easier :-)
It would be helpfull to see the resulting markup instead of the text ;-)
You have to do this:
echo "<table border='1'><tr><th>Title</th><th>Article</th></tr>";
or
echo "<table border='1'>";
echo "<tr>";
echo "<th>Title</th>";
echo "<th>Article</th>";
echo "</tr>";
instead of
echo "<table border='1'>
<tr>
<th>Title</th>
<th>Article</th>
</tr>";

PHP beginner. Trying to display UserId

I am accessing my database on PhpMyAdmin, all my names etc are correct, but for some reason UserId is not working. Can someone point me in the right direction?
I have tried printing it but nothing displays.
<?php session_start();
$username = $_GET['username'];
$password = $_GET['password'];
// Create connection
$con=mysqli_connect("localhost","root","","test");
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM user2 where username='$username' and password='$password'");
$row_cnt = mysqli_num_rows($result);
if($row_cnt >0){
while($row = mysqli_fetch_array($result)){
$UserId = $row['UserId'];
}
$sqlQuery2 = "SELECT ProductID, Name, Price, Description FROM product";
echo "Hello ".$username."<br>" .$UserId. "<br> This is a list of products";
$result2 = mysqli_query($con,$sqlQuery2);
echo "<table border='1'>
<tr>
<th>ProductID</th>
<th>Name</th>
<th>Price</th>
<th>Description</th>
<th>View</th>
</tr>";
while($row = mysqli_fetch_array($result2))
{
echo "<tr>";
echo "<td>" . $row['ProductID'] . "</td>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['Price'] . "</td>";
echo "<td>" . $row['Description'] . "</td>";
echo "<td><a href=\"detailview.php?ProductID=".$row['ProductID']."\"'>Detailed View</a></td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Update My Details
<?php } else{
echo "invalid login "; }
?>
use var_dump($var) to see what is in the variable
1st check what is returned in $result and after check $UserId before using it
after the while check if $UserId is set (if the condition is false, ou var is not set..) you should check $row_count too
you should indent your code
here is your code reindented :
<?php session_start();
$username = $_GET['username'];
$password = $_GET['password'];
// Create connection
$con=mysqli_connect("localhost","root","","test");
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM user2 where username='$username' and password='$password'");
$row_cnt = mysqli_num_rows($result);
if($row_cnt >0){
while($row = mysqli_fetch_array($result)){
$UserId = $row['UserId'];
}
$sqlQuery2 = "SELECT ProductID, Name, Price, Description FROM product";
echo "Hello ".$username."<br>" .$UserId. "<br> This is a list of products";
$result2 = mysqli_query($con,$sqlQuery2);
echo "<table border='1'>
<tr>
<th>ProductID</th>
<th>Name</th>
<th>Price</th>
<th>Description</th>
<th>View</th>
</tr>";
while($row = mysqli_fetch_array($result2))
{
echo "<tr>";
echo "<td>" . $row['ProductID'] . "</td>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['Price'] . "</td>";
echo "<td>" . $row['Description'] . "</td>";
echo "<td><a href=\"detailview.php?ProductID=".$row['ProductID']."\"'>Detailed View</a></td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Update My Details
<?php
}
else
{
echo "invalid login ";
}
?>

Categories