I have a simple project that I am working on and I'm having a hard time finding the code I need to get line breaks in the following table:
<?php
// connect to the database
$host = '###';
$username = '###';
$pass = '####';
mysql_connect($host,$username,$pass) or die(mysql_error());
mysql_select_db("####") or die(mysql_error());
// select everything from the table
$query = "SELECT * FROM Employees";
$result = mysql_query($query) or die(mysql_error());
echo "<table>";
echo "<tr>";
while( ($row = mysql_fetch_array($result)))
{
echo "<td>".$row['employeeid']."</td>";
echo "<td>".$row['firstname']."</td>";
echo "<td>".$row['lastname']."</td>";
echo "<td>".$row['department']."</td>";
}
echo "</tr>";
echo "</table>";
// disconnect from the database
mysql_close();
?>
Everything works correctly and it grabs data from the correct database and table. But when it displays results it is all on the same line ("record1record2record3") and I'd like a line break between employee records.
I've searched this question and it seems like my results all show me an entirely different way of doing this. I've already got the code written and fussed with it a lot to get it working. Can I just make a simple alteration to the above code to get the breaks I want?
The <tr> tags must also be inside the while loop so that they are outputted for each row. They make the rows in the HTML table.
while( ($row = mysql_fetch_array($result)))
{
echo "<tr>";
echo "<td>".$row['employeeid']."</td>";
echo "<td>".$row['firstname']."</td>";
echo "<td>".$row['lastname']."</td>";
echo "<td>".$row['department']."</td>";
echo "</tr>";
}
Related
I am just running a SQL query to fetch a particular data from the database and would like to display in horizontal way inspite of the vertical format, Can we do something to display the data as required.
For Getting the clear understanding of the question i have attached the Output which i am getting and the output which i required after the SQL query is executed.
Thanks in Advance.
Query:
<?php
include 'connect-db.php';
$query = "SELECT distinct work_component FROM daily_progress_demo WHERE package_no='$package_no'";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
echo "<center>".$row['work_component']."</center>";
echo "<br />";
}
?>
you can do like this
echo "<table>"
echo "<tr>"
while($row = mysql_fetch_array($result)){
echo "<td>".$row['work_component']."</td>";
}
echo "</tr>";
echo "</table>";
echo $row['work_component']."<br>";
Heading come in all the rows need in first row
Need help with where i am going wrong
thank you
below is the code
<?php
$connection = mysql_connect('localhost', 'aria2', 'osvOSUxlY6wYLZzC'); //The Blank string is the password
mysql_select_db('torres');
$query = "SELECT * FROM reports"; //You don't need a ; like you do in SQL
$result = mysql_query($query) or die(mysql_error());;
echo "<table>"; // start a table tag in the HTML
while($row = mysql_fetch_array($result)){ //Creates a loop to loop through results
echo "<table><tr><th>CSQ_Name</th>
<th>CSQ_ID</th>.......
$row..............
//$row['index'] the index here is a field name
}
echo "</table>"; //Close the table in HTML
mysql_close(); //Make sure to close out the database connection
You only need to move the header portion out of the loop:
echo '<table><tr><th>CSQ_Name</th><th>CSQ_ID</th></tr>'; // start a table tag in the HTML
while ($row = mysql_fetch_array($result)) { //Creates a loop to loop
echo '<tr>';
// output data here
echo '<td>...</td><td>...</td>';
echo '</tr>';
}
echo "</table>"; //Close the table in HTML
I am trying to display results from a database onto my site. I have one entry in the database, and the result of that entry should be a "1". However, when I run the following code, it returns "1111111111111..." and continues to load ones. If there are 4 entries, I want the results to be displayed like:
1
2
3
4
I have looked around on this forum and others but cannot find anywhere where somebody had the same problem, and my code looks similar to the others. What did I do wrong?
functions.php:
$sqlgroup = mysql_query ("SELECT groupid FROM groups");
$grouprow = mysql_fetch_array ($sqlgroup);
Settings.php:
<?php while( ($row = $grouprow))
{
echo "<tr>";
echo "<td>".$row['groupid']."</td>";
echo "</tr>";
} ?>
First of all don't use mysql, use mysqli isntead:
$sqlgroup = mysqli_query ("SELECT groupid FROM groups");
while($row = mysqli_fetch_array($sqlgroup))
{
echo "<tr>";
echo "<td>".$row['groupid']."</td>";
echo "</tr>";
}
i have this code that will display the items perfectly but having a error pop up that i have to click "OK" too view the page
i have searched the web and from what i understand this has something to do with that the header table has more column than the data itself, i have tried to troubleshot this and got nowhere fast as i am a novice (noob) :/
if anyone can shed some light on this it would be great! :)
The Error
DataTables warning (table id = 'example'): Requested unknown parameter '0'
from the data source for row 0
My Code
<?php
//include database connection
include 'db_connect.php';
//query all records from the database
$query = "select * from repair_jobs";
//execute the query
$result = $mysqli->query( $query );
//get number of rows returned
$num_results = $result->num_rows;
//this will link us to our add.php to create new record
if( $num_results > 0){ //it means there's already a database record
//start table
//creating our table heading
echo "<table id='example' cellpadding='0' cellspacing='0' border='0' class='display' >";
echo "<thead><tr>";
echo "<th>Client</th>";
echo "<th>Type</th>";
echo "<th>Labour</th>";
echo "<th>ourcosts</th>";
echo "</thead></tr><tbody><tr class=\"gradeX\">";
//loop to show each records
while( $row = $result->fetch_assoc() ){
//extract row
//this will make $row['firstname'] to
//just $firstname only
extract($row);
//creating new table row per record
echo "<tr>";
echo "<td>{$client}</td>";
echo "<td>{$type}</td>";
echo "<td>{$labour}</td>";
echo "<td>{$ourcosts}</td>";
echo "</td>";
echo "</tr>";
}
echo "</table>";//end table
}else{
//if database table is empty
echo "No records found.";
}
//disconnect from database
$result->free();
$mysqli->close();
?>
it displays ok but this error puzzling me.
I am trying to loop though my users database to show each username in the table in their own row. I can only get it to show one user but loops through this the number of rows there are. Code is below
<?php
require_once ('../login/connection.php');
include ('functions.php');
$query = "SELECT * FROM users";
$results=mysql_query($query);
$row_count=mysql_num_rows($results);
$row_users = mysql_fetch_array($results);
echo "<table>";
for ($i=0; $i<$row_count; $i++)
{
echo "<table><tr><td>".($row_users['email'])."</td></tr>";
}
echo "</table>";
?>
Thanks
mysql_fetch_array fetches a single row - you typically use it in a while loop to eat all the rows in the result set, e.g.
echo "<table>";
while ($row_users = mysql_fetch_array($results)) {
//output a row here
echo "<tr><td>".($row_users['email'])."</td></tr>";
}
echo "</table>";
You're only fetching one row:
$row_users = mysql_fetch_array($results);
You're never calling a fetch again so you're not really looping through anything.
I'd suggest changing your loop to the following:
echo "<table>";
while ($row = mysql_fetch_array($results)) {
echo "<tr><td>".($row['email'])."</td></tr>";
}
echo "</table>";
The while loop will loop through the results and assign a row to $row, until you run out of rows. Plus no need to deal with getting the count of results at that point. This is the "usual" way to loop through results from a DB in php.
In the new MYSQLI, I use this coding
$query = "SELECT * FROM users";
$results=mysqli_query($con,$query);
$row_count=mysqli_num_rows($results);
echo "<table>";
while ($row = mysqli_fetch_array($results)) {
echo "<tr><td>".($row['id'])."</td></tr>";
}
echo "</table>";
mysqli_query($con,$query);
mysqli_close($con);
Your problem is in this line:
echo "<table><tr><td>".($row_users['email'])."</td></tr>";
You're echoing out a <table> tag again. Remove that so your code looks like this:
echo "<table>";
for ($i=0; $i<$row_count; $i++)
{
echo "<tr><td>".($row_users['email'])."</td></tr>";
}
echo "</table>";
You don't need to output a table within a table the way you're doing it.
$result = mysql_query("SELECT `email` FROM `users`");
$num_emails = mysql_num_rows($result);
echo "<table><caption>There are/is $num_emails email(s)</caption>";
while ($row = mysql_fetch_assoc($result)) {
echo "<tr><td>{$row['email']}</td></tr>";
}
echo '</table>';
Something like that should work.