I know this question has been asked multiple times but looking through and trying the solutions has not got me far.
I would like the table to have a format of:
First name | Last Name
Mike Hannover
Steve Dortmund
however I am not sure how to achieve this, it currently lays out like as I have taken the table coding out that I have tried.
FirstName: Mike - LastName: Hannover
FirstName: Steven - LastName: Dortmund
I have attached my PHP code below and thanks in advance for your time and help.
<body>
<h1>Clients information</h1>
<?php
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "lastgo";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT FirstName, LastName FROM info";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "FirstName: " . $row["FirstName"]. "
- LastName: " . $row["LastName"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
Probably the easier way is to build your table is to do it as a table.
// Just looking at this part of the code.
if ($result->num_rows > 0) {
// Begin a table
echo "<table>";
// Create a header row
echo "<tr><th>First Name</th><th>Last Name</th></tr>";
while($row = $result->fetch_assoc()) {
// output data for each row
echo "<tr><td>" . $row["FirstName"]. "</td><td>" .
$row["LastName"]. "</td></tr>";
}
// Close table
echo "</table>";
}
As far I understand you want to print your FirstnName and LastName like so
Change your while loop to:
echo "<table>";
echo "<th>FirstName: </th><th>LastName</th>";
while($row = $result->fetch_assoc()) {
echo "<tr>".
"<td>" .$row["FirstName"] ."</td>"
."<td>" . $row["LastName"]."</td>
.</tr>>";
}
echo "</table>";
you can use a table, then style it the way you want the records to appear
<table>
<tr>
<td>Name</td>
<td>Last Name</td>
</tr>
<?php foreach ($names as $value) : ?>
<tr><td><?php echo $value['Name']; ?></td><td><?php echo $value['LastName']; ?></td></tr>
<?php endforeach; ?>
</table>
Related
If there's duplicated topic - please, forgive me, but i haven't found a solution yet. First of all, I've got one php file (update.php). This file contains:
<form action='update.php' method='post'>
<table border='1'>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "exchange";
$conn = new mysqli($servername, $username, $password, $dbname);
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_set_charset($conn,"utf8");
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT ID, name, symbol, buy, sell FROM exchangevalues";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td><input type='text' name='ID' value='".$row["ID"]."'>";
echo "<td>".$row["name"]."</td>";
echo "<td>Buy: <input type='text' name='buy' value='".$row['buy']."'></td>";
echo "<td>Sell: <input type='text' name='sell' value='".$row['sell']."'></td>";
echo "</tr>";
}
}
$conn->close();
?>
<table>
There's 14 rows and 5 columns in my db. How do i update every row in columns 'buy' and 'sell' on 'click' (isset submit button) with new values? I've tried with ID[], buy[], but I've got problems with loop and i cannot handle it. I'm running MySQL on localhost, also i know that only last row will be updated without running loop, but still...
I am assuming you have corresponding php code to process the input, here is a sample of what you'll need to do for the HTML when creating the form:
if ($result->num_rows > 0) {
$i=0;
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td><input type='text' name='ID[" . $i . "]' value='".$row["ID"]."'>";
echo "<td>".$row["name"]."</td>";
echo "</tr>";
}
}
On the php side to process the input, you can do something like this:
$id_array = $_REQUEST["ID"];
foreach($id_array as $id){
//do insert with $id
}
The below code works perfectly and gives me the "id" "firstname" and "lastname". What I want is to use a loop to echo all the field values in the result row without having to quote each column name like
$row["id"]
below is the working code
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<br> id: ". $row["id"]. " - Name: ". $row["firstname"]. " " .
$row["lastname"] . "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
any thoughts please
This should work..
while($row = $result->fetch_array()) {
$i=0;
while($i<count($row)){
echo $row[$i];
$i++;
}
}
just use a foreach loop inside while like this
foreach($row as $key=>$value){
echo "<br> $key: ". $value. "<br>";
}
If I understand what you want to do is automate the output of the name of each column with its value (independent of the number of columns you get).
So do it like this :
if ($result->num_rows > 0) {
foreach($result->fetch_assoc() as $row) { // browse each records
foreach($row as $col => $value) { // browse each columns on a record
echo "<br>$col: $value<br>";
}
}
}
else {
echo "no result";
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have this problem with this while loop. I want to make a table where an administrator can see users and delete them. So I want this into a table. I have tried some echo's like:
echo <table>, echo <td>, echo </td>, echo </table>.
Unfortunately nothing of this al works.
Can anybody help me?
<?php
$account = 'Account:';
$password1 = 'Password:';
//check db connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Take everything from table and fill in $result
$sql = "SELECT * FROM login";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// Take all data
while($row = $result->fetch_assoc()) {
//
echo $account. " " .$row["username"]. "<br> " . $password1 . " " . $row["password"]. "<br><br>";
}
} else {
// nothing in DB is 0 results
echo "0 results";
}
$conn->close();
?>
Simply print a table -
<table>
<tr>
<td>Account</td>
<td>Password</td>
</tr>
<?php
while($row = $result->fetch_assoc()) {
echo "<tr><td>" .$row["username"]. "</td><td>" . $row["password"]. "</td></tr>";
}
?>
</table>
<?php
$account = 'Account:';
$password1 = 'Password:';
//check db connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Take everything from table and fill in $result
$sql = "SELECT * FROM login";
$result = $conn->query($sql);
echo"<table><tr><td>Username</td><td>Password</td><td>Action</td></tr>";
if ($result->num_rows > 0) {
// Take all data
while($row = $result->fetch_assoc()) {
echo"<tr><td>".$row['username']."</td><td>".$row['password']."</td><td> modify | delete </td></tr>";
}
} else {
// nothing in DB is 0 results
echo "0 results";
}
echo"</table>";
$conn->close();
?>
$sql = "SELECT * FROM login";
$result = $conn->query($sql);
echo "<table>";
echo "<tr>";
echo '<td>Username</td>';
echo '<td>Password</td>';
echo '<td>Action</td>';
echo "</tr>";
if ($result->num_rows > 0) {
// Take all data
while($row = $result->fetch_assoc()) {
//
echo $account. " " .$row["username"]. "<br> " . $password1 . " " . $row["password"]. "<br><br>";
echo '<tr>';
echo '<td>'.$row["username"].'</td>';
echo '<td>'.$password1.'</td>';
echo '<td>Delete</td>';
echo '</tr>';
}
} else {
// nothing in DB is 0 results
echo "0 results";
echo '<tr><td colspan="3" rowspan="" headers="">No records Found</td></tr>';
}
echo "</table>";
$conn->close();
Replace your while loop with :
echo "<table><tr>
<td>Account</td>
<td>Username</td>
<td>Password</td>
</tr>"
while($row = $result->fetch_assoc())
{
echo "<tr><td>".$account."</td><td>" .$row["username"]. "</td><td>" . $password1 . "</td><td>" . $row["password"]. "</td></tr>";
}
echo "</table>";
I'm not wholly proficient with PHP but I'm having a few problems echoing a gathered value from a MySQL query function.
I believe I know where the problem lies but I'm not competent enough to fix it, if you could please help it would be appreciated.
PHP Function (Works perfectly).
<?php
$dbhost = 'localhost';
$dbuser = 'user';
$dbpass = 'password';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$sql = 'SELECT Name, Role, Salary FROM `users-table`';
mysql_select_db('user_records');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
echo "--------------------------------<br>",
"Name: {$row['Name']} <br> ".
"Role: {$row['Role']} <br> ".
"Salary : {$row['Salary']} <br> ".
"--------------------------------<br>";
}
mysql_close($conn);
?>
This displays the data perfectly! However I'm now trying to include this in a nicely formatted HTML table. (Which is why I'm closing the php tag above).
I'm then trying to use a table like this:
<table >
<tr>
<td>
<?php echo $row['Name'] ?>
</td>
....
It outputs nothing, - I think this problem is caused because I close the first function and then try to reference $row and it doesn't know what to do...?
I think I need to tap in to while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
How can I re-factor this so that I can echo content from the above function to my table?
You didn't write the code you tried, but here is something functional:
$link = mysqli_connect("myhost", "myuser", "mypassw", "mybd");
echo "<table>";
while ($row = mysqli_fetch_array($link, $retval, MYSQL_ASSOC)) {
echo "
<tr>
<td>{$row['Name']}</td>
<td>{$row['Role']}</td>
<td>{$row['Salary']}</td>
</tr>
";
}
echo "</table>";
PS: Since MySQL is deprecated, I replaced your code with MySQLi, and I suggest you do the same :)
As #zessx said, yous should use MySQLi or PDO.
anyway to answer to your question, you have to change the loop in your php code from
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
echo "--------------------------------<br>",
"Name: {$row['Name']} <br> ".
"Role: {$row['Role']} <br> ".
"Salary : {$row['Salary']} <br> ".
"--------------------------------<br>";
}
to
echo '<table>';
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
echo " <tr>" .
" <td>Name: {$row['Name']}</td>".
" <td>Role: {$row['Role']}</td>".
" <td>Salary : {$row['Salary']}</td>".
" </tr>";
}
echo '</table>';
EDIT: This is what I am trying to achieve: http://i.imgur.com/KE9xx.png
I am trying to display the results from my database in two columns. I'm a bit new to PHP so I haven't the slightest clue on how to do this. Can anybody help me with this? Thanks in advance.
Here is my current code:
include('connect.db.php');
// get the records from the database
if ($result = $mysqli->query("SELECT * FROM todo ORDER BY id"))
{
// display records if there are records to display
if ($result->num_rows > 0)
{
// display records in a table
echo "<table width='415' cellpadding='0' cellspacing='0'>";
// set table headers
echo "<tr><td><img src='media/title_projectname.png' alt='Project Name' /></td>
<td><img src='media/title_status.png' alt='Status'/></td>
</tr>";
echo "<tr>
<td><div class='tpush'></div></td>
<td> </td>
</tr>"
while ($row = $result->fetch_object())
{
echo "<tr>";
echo "<td><a href='records.php?id=" . $row->id . "'>" . $row->item . "</a></td>";
echo "<td>" . $row->priority . "</td>";
echo "</tr>";
}
echo "</table>";
}
// if there are no records in the database, display an alert message
else
{
echo "No results to display!";
}
}
// show an error if there is an issue with the database query
else
{
echo "Error: " . $mysqli->error;
}
// close database connection
$mysqli->close();
A good idea would be storing your data into a simple array and then display them in a 2-columned table like this:
$con = mysql_connect('$myhost', '$myusername', '$mypassword') or die('Error: ' . mysql_error());
mysql_select_db("mydatabase", $con);
mysql_query("SET NAMES 'utf8'", $con);
$q = "Your MySQL query goes here...";
$query = mysql_query($q) or die("Error: " . mysql_error());
$rows = array();
$i=0;
// Put results in an array
while($r = mysql_fetch_assoc($query)) {
$rows[] = $r;
$i++;
}
//display results in a table of 2 columns
echo "<table>";
for ($j=0; $j<$i; $j=$j+2)
{
echo "<tr>";
echo "<td>".$row[$j]."</td><td>".$row[$j+1]."</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
<table>
<tr>
<td>ProjectName</td>
<td>Status</td>
<td>ProjectName</td>
<td>Status</td>
</tr>
<?php
while($row = $result->fetch_object()) {
echo "<tr>";
echo "<td>".$row->ProjectName."</td>";
echo "<td>".$row->Status."</td>";
echo "<td>".$row->ProjectName."</td>";
echo "<td>".$row->Status."</td>";
echo "</tr>";
}
?>
</table>
This is the thing on picture. With a bit CSS you can manipulate the tds.
Your function should look similar to this:
$query = "SELECT *
FROM todo
ORDER BY id";
$result = $mysqli->query($query);
while($row = $result -> fetch_array()) {
$feedback .= "<tr>\n<td>" . $row['item'] . "</td><td>" . $row['priority'] . "</td>\n</tr>";
}
return $feedback;
Then, in your HTML have the <table> already setup and where you would normally insert your <td> and <tr> put <?php echo $feedback?> (where $feedback is the assumed variable on the HTML page that retrieves the $feedback from the function). This isn't a complete fix, your code is hard to read, but by starting here, you should be able to continue on the path filling in all the extra information you need for the table, including your CSS.