It is showing that connection has been created. But I don't know what is the problem in query. After that query I tried to echo something and it is visible there in browser.
<html>
<head>
</head>
<body>
<?php
$servername = "localhost:8888";
$username = "root";
$password = "";
$dbname = "employees";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}else {
echo "Connected successfully";
}
$res = mysqli_query($conn,"select * from dept_manager");
echo"dept_manager";
while($row=mysqli_fetch_assoc($res))
{
?>
<table>
<tr>
<td><?php echo $row['emp_no'] ?></td>
<td><?php echo $row['dept_no']?></td>
<td><?php echo $row['from_date'] ?></td>
<td><?php echo $row['to_date'] ?></td>
</tr>
</table>
<?php
}
?>
</body>
</html>
$sql = "select * from dept_manager";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()){
// your code here
}
Try with this..
Try this in the place of while loop:
<?php
$res = mysqli_query($conn,"select * from dept_manager");
echo"dept_manager";
echo "<table>";
while($row=mysqli_fetch_assoc($res))
{
echo "<tr><td>" . $row['emp_no'] . "</td><td>" . $row['dept_no'] . "</td><td>" . $row['from_date'] . "</td><td>" . $row['to_date'] . "</td></tr>";
}
echo "</table>";
?>
You have mentioned servername:localhost:8888 just check once remove
8888 and try.
I have tried with your code in my local system,same code working fine.
Just i have entered my database name and table name,it's working fine.
So you just follow my example and test once.
COde:-
<html>
<head>
</head>
<body>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "mydashboard";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
else{
echo "Connected successfully";
}
$res = mysqli_query($conn,"select * from content_values");
while($row=mysqli_fetch_assoc($res))
{
?>
<table>
<tr>
<td><?php echo $row['name'] ?></td>
</tr>
</table>
<?php
}
?>
</body>
</html>
Output:-
Connected successfully
Name:
Test QA AndroidSD
image sampl
GMAIL
test PDF
Nat Geo Video
Related
I have managed to take information from my database of the website, however I have no idea how to present the results in a table and how to style it. I tried putting <table> outside of the whole PHP, clearly did not work. I tried echoing a <table> tag before the result echo and a closing </table> tag after it, but that did not do it. This is the code I am working with:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "onlib";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//Takes all the results from the table with genre 5.
$sql = "SELECT name, description, content FROM books WHERE genre='5'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<span style='color:white;'>"."<br> Name: ".$row["name"]."<br> Description: ".$row["description"]."<br> Content: ".$row["content"] ."<br>"."</p>";
}
} else {
echo "0 results";
}
$conn->close();
?>
I am still new in PHP, trying to understand how the whole thing works. Thanks in advance!
<?php
function tableV1 ($row) {
echo '<tr>';
echo '<td>' . $row['name'] . '</td>';
echo '<td>' . $row['description'] . '</td>';
echo '<td>' . $row['content'] . '</td>';
echo '</tr>';
}
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "onlib";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); }
?>
Always do Database Connection first, before Outputting anything, that way, you can create custom error message to show instead of a failed database conntection or no content at all.
<style type="text/css">
table {}
tbody {}
td {}
th {}
thead {}
tr {}
</style>
Style is used inside the <head></head> to style the table, CSS it's called.
<table>
<thead>
<th>Name</th>
<th>Description</th>
<th>Content</th>
</thead>
<tbody>
<?php
// Takes all the results from the table with genre 5.
$sql = "SELECT name, description, content FROM books WHERE genre='5'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// Output data of each row
while($row = $result->fetch_assoc()) {
tableV1($row);
}
} else {
echo '<tr><td colspan="3">0 results</td></tr>';
}
?>
</tbody>
</table>
Output contents from database.
<?php
$conn->close();
?>
Close database connection in the end. All together:
<?php
function tableV1 ($row) {
echo '<tr>';
echo '<td>' . $row['name'] . '</td>';
echo '<td>' . $row['description'] . '</td>';
echo '<td>' . $row['content'] . '</td>';
echo '</tr>';
}
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "onlib";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); }
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Page Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style type="text/css">
table {}
tbody {}
td {}
th {}
thead {}
tr {}
</style>
</head>
<body>
<table>
<thead>
<th>Name</th>
<th>Description</th>
<th>Content</th>
</thead>
<tbody>
<?php
// Takes all the results from the table with genre 5.
$sql = "SELECT name, description, content FROM books WHERE genre='5'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// Output data of each row
while($row = $result->fetch_assoc()) {
tableV1($row);
}
} else {
echo '<tr><td colspan="3">0 results</td></tr>';
}
?>
</tbody>
</table>
</body>
</html>
<?php
$conn->close();
?>
Try this:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "onlib";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//Takes all the results from the table with genre 5.
$sql = "SELECT name, description, content FROM books WHERE genre='5'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
echo "<table>";
echo "<thead>
<tr>
<th>Name</th>
<th>Description</th>
</tr>
</thead>";
echo "<tbody>";
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>".$row["name"]."</td>";
echo "<td>".$row["description"]."</td>";
echo "</tr>";
}
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
?>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "onlib";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//Takes all the results from the table with genre 5.
$sql = "SELECT name, description, content FROM books WHERE genre='5'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo '<table>';
echo '<tr><td>Name</td><td>Description</td><td>Content</td></tr>'
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>".$row["name"]."</td><td>".$row["description"]."</td><td>".$row["content"] ."</td></tr>";
}
echo '</table>';
} else {
echo "0 results";
}
$conn->close();
?>
The basic table format is this
for example:
<table>
<tr>
<td>row 1 item 1</td>
<td>row 1 item 2</td>
</tr>
<tr>
<td>row 2 item 1</td>
<td>row 2 item 2</td>
</tr>
</table>
so try:
if ($result->num_rows > 0) {
echo "<table>";
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>".$row["name"]."</td>";
echo "<td>".$row["description"]."</td>";
echo "<td>".$row["content"]."</td>";
echo "</tr>";
}
echo "</table>";
} else {
echo "0 results";
}
Try this approach:
[... some HTML header and code ...]
<table>
<thead>
<tr><th>Name</th><th>Description</th><th>Content</th></tr>
</thead>
<tbody>
<?php
[ ... extract something from the database ...]
while($row = $result->fetch_assoc())
{
echo "<tr><td>$row[name]</td><td>$row[description]</td><td>Content</td></tr>\n";
}
}
?>
</tbody>
</table>
[... some HTML footer ...]
Obviously, replace the [...] sections with your own code. The idea is here to use PHP to output the dynamic part of your HTML code, and is the power of PHP combined with HTML.
I try to avoid "echoing" to much HTML in my PHP, but it does work (reference other answers).
Detail: when setting up a <table> you should setup a header section <thead> <tfoot> (if applicable) and body section <tbody>. Ref What is the purpose for HTML's tbody?
I am trying to retrieve all data from my database and displaying it in a table. But i could not do that, am facing some problem.I am getting error as,
This page isn’t working.
localhost is currently unable to handle this request.
Here is my code,
<html>
<body>
<table style="width:100%">
<tr>
<th>Driverid</th>
<th>Truckid</th>
<th>Imagecount</th>
<th>Trainingstatus</th>
</tr>
<?php
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "IDdb";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT DriverID, TruckID, Imagecount, Trainingstatus FROM IDs";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$driverid = $row["Driverid"];
$truckid = $row["Truckid"];
$imagecount = $row["Imagecount"];
$trainingstatus = $row["Trainingstatus"];?>
<tr>
<td><?php echo $driverid; ?></td>
<td><?php echo $truckid; ?></td>
<td><?php echo $imagecount; ?></td>
<td><?php echo $trainingstatus; ?></td>
</tr>
</table>
<?php}
} else {
echo "0 results";
}
$conn->close();
?>
</body>
</html>
please replace your code
from
driverid = $row["Driverid"];
truckid = $row["Truckid"];
imagecount = $row["Imagecount"];
trainingstatus = $row["Trainingstatus"];
to
$driverid = $row["Driverid"];
$truckid = $row["Truckid"];
$imagecount = $row["Imagecount"];
$trainingstatus = $row["Trainingstatus"];
please reader the section about how to define variables in php on the PHP Manual ,after that, check the variables defined in your code.
you have not started table tag, also not in loop, and main part is sdd space <?php} to <?php }
if ($result->num_rows > 0) {
echo '<table>';
// output data of each row
while($row = $result->fetch_assoc()) {
$driverid = $row["Driverid"];
$truckid = $row["Truckid"];
$imagecount = $row["Imagecount"];
$trainingstatus = $row["Trainingstatus"];?>
<tr>
<td><?php echo $driverid; ?></td>
<td><?php echo $truckid; ?></td>
<td><?php echo $imagecount; ?></td>
<td><?php echo $trainingstatus; ?></td>
</tr>
<?php }
echo '</table>';
} else {
echo "0 results";
}
So I made my first database online. I used phpmyadmin, I created the table and the user.
Now I'd like to show the table on a page of my site, as well as giving the possibility to people to edit the database from the site.
My problem is that the database does not work: it doesn't connect. I have no idea what to do.
My database is called letstenf_santi and my table passeggeri.
This is the code I'm trying to use to show the table on the site.
<?php
//establishing connection
mysql_connect('localhost', 'root', '');
//selecting a database
mysql_select_db('letstenf_santi');
$sql = 'SELECT * FROM `letstenf_santi`.`passeggeri`';
$records=mysql_query($sql);
?>
<html>
<head>
<title>mostra</title>
</head>
<body>
<table width="300" border="1" cellpadding="10" cellspacing="1">
<tr>
<th>id pass</th>
<th>nome</th>
<th>eta</th>
<th>sesso</th>
</tr>
<?php
while($pass=mysql_fetch_assoc($records)){
echo "<tr>";
echo "<td>".$pass['idpasseggero']."</td>";
echo "<td>".$pass['nome']."</td>";
echo "<td>".$pass['eta']."</td>";
echo "<td>".$pass['sesso']."</td>";
echo "</tr>";
}
?>
</table>
</body>
</html>
instead of this,
mysql_connect('localhost', 'root', '');
mysql_select_db('letstenf_santi');
you can try this,
$connection=mysql_connect('localhost', 'root', '');
$db=mysql_select_db('letstenf_santi',$connection);
try this code
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "letstenf_santi";//your db name
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM tablename";//replace table name with your table name
$result = mysqli_query($conn, $sql); ?>
<html>
<head>
<title>mostra</title>
</head>
<body>
<table width="300" border="1" cellpadding="10" cellspacing="1">
<tr>
<th>id pass</th>
<th>nome</th>
<th>eta</th>
<th>sesso</th>
</tr>
<?php if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>".$row['idpasseggero']."</td>";
echo "<td>".$row['nome']."</td>";
echo "<td>".$row['eta']."</td>";
echo "<td>".$row['sesso']."</td>";
echo "</tr>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
?>
</table>
</body>
</html>
<?php
$servername = "localhost";
$username = "root";
$password = "admin";
$dbname = "sbsuite";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * from sys_frm WHERE ParamType='Title';";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo $row["Data"] . "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?
This is code that give me two results..
Client
Server
I want to insert them as link to another php page with image, depends on what we click on its goes to another php page...
Thanks for help!
This is a simple HTML. Replace the client/server php and jpg to what you want.
<img src="client.jpg" alt="Client" /><br />
<img src="server.jpg" alt="Server" />
With PHP
<?php
while ($row = $result->fetch_assoc()) {
?>
<img src="<?php echo $row["Data"]; ?>.jpg" alt="<?php echo $row["Data"]; ?>" /><br />
<?php
}
I am printing values in the address bar using the get variable,it works perfectly, then my problem is i want store the printed values into a mysql table, but it only stores null values, please help me where i am going wrong in below attached code.
<?php
session_start();
include('header.php');
?>
<form name="cart.php">
<table>
<tr>
<th> ID </th>
<th> Vehicle Description </th>
<th> Price </th>
</tr>
<tr>
<td><?php echo $_GET['id']; ?> </td>
<td><?php echo $_GET['name']; ?> </td>
<td><?php echo $_GET['price']; ?> </td>
</tr>
</table>
<?php
$servername = "";
$username = "";
$password = "";
$dbname = "";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$x=$_GET['id'];
$y=$_GET['name'];
$z=$_GET['price'];
$sql = "INSERT INTO products (id,description,price)
VALUES ('$x', '$y', '$y')";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
<form><input type="button" value="Go back" onClick="window.location.href='automobile_list.php'"></form>
</form>
<?php
include('footer.php');
?>
<?php
session_start();
include('header.php');
$x=$_GET['id'];
$y=$_GET['name'];
$z=$_GET['price'];
?>
<form name="cart.php">
<table>
<tr>
<th> ID </th>
<th> Vehicle Description </th>
<th> Price </th>
</tr>
<tr>
<td><?php echo $x; ?> </td>
<td><?php echo $y; ?> </td>
<td><?php echo $z; ?> </td>
</tr>
</table>
<?php
$servername = "";
$username = "";
$password = "";
$dbname = "";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "INSERT INTO products (id,description,price)
VALUES ('$x', '$y', '$y')";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
<form><input type="button" value="Go back" onClick="window.location.href='automobile_list.php'"></form>
</form>
<?php
include('footer.php');
?>
use the above code you will get your code working.
the problem is in query
so update that line as:
$sql = "INSERT INTO products (id,description,price)
VALUES ('".$x."', '".$y."', '".$y."')";