Hi so im trying to put all of the useres in a database, table into a html table this is what i have:
<table class="table table-striped">
<thead>
<tr>
<th>UUID</th>
<th>Full Name</th>
<th>Email</th>
<th>Access Key</th>
<th>Phone Number</th>
<th>Activated</th>
<th>Role</th>
</tr>
</thead>
<tbody>
<?php
include_once('inc/conf/databaseConnect.php');
$query = mysql_query("SELECT * FROM list_users ORDER by id");
while($row = mysql_fetch_array($query)){
echo "<tr>";
echo "<td>".$row['uuid']."</td>";
echo "<td>".$row['firstname'].$rowtwo['lastname']."</td>";
echo "<td>".$row['email']."</td>";
echo "<td>".$row['security_key']."</td>";
echo "<td>".$row['phone_no']."</td>";
echo "<td>".$row['activated']."</td>";
echo "<td>".$row['role']."</td>";
echo "</tr>";
}
?>
</tbody>
</table>
This dosnt return anything, or any errors. It connects to the database correctly ive checked that its just not retrieving the users.
Image of database structure
databaseConnect.php:
<?php
//Create Connnection
$sqlLink = mysqli_connect('localhost', 'root', 'classified', 'user_details');
//If Error Connecting
if(!$sqlLink) {
die('<center><br><h3>Error connecting to servers Database.');
}
?>
After seeing your edit because you did not originally show us which connection method you were using; the almighty answer here (least the most important one) is that you can't mix different MySQL APIs.
Can I mix MySQL APIs in PHP?
You need to use the same one from connection to query.
Plus that $rowtwo should be $row as I stated in comments along with my asking about what was inside your databaseConnect.php file.
Get to work with prepared statements also to help protect against an SQL injection.
https://en.wikipedia.org/wiki/Prepared_statement
Thanks. I have fixed the issue i updated to using mysqli's methods
<?php
include_once('inc/conf/databaseConnect.php');
$query = $sqlLink->query("SELECT * FROM list_users ORDER by id");
while($row = $query->fetch_array()){
echo "<tr>";
echo "<td>".$row['uuid']."</td>";
echo "<td>".$row['firstname'].$row['lastname']."</td>";
echo "<td>".$row['email']."</td>";
echo "<td>".$row['security_key']."</td>";
echo "<td>".$row['phone_no']."</td>";
echo "<td>".$row['activated']."</td>";
echo "<td>".$row['role']."</td>";
echo "</tr>";
}
?>
At least check for errors:
if ($query = mysql_query("SELECT * FROM list_users ORDER by id");) {
...
} else {
echo '<b>MySQL error:</b><br>' . mysql_error() . '<br />';
}
You must use mysql_fetch_assoc() instead of mysql_fetch_array or fetch like this mysql_fetch_array($result, MYSQL_ASSOC).
Related
I have a php code to print out my table including its column name. The printing has to be dynamic because it has to print different size/length tables based on a user input. :
<table>
<?php
while ($row = mysqli_fetch_array($results)) {
while ($fieldInfo = mysqli_fetch_field($results)) { ?>
<th> <?php echo $fieldInfo->name; ?> </th>
<td> <?php echo $row[$fieldInfo->name] ?> </td>
<?php }
} ?>
</table>
this is the query for $results:
$tName = $_POST["tableNames"]; //this data is recieved from another page
require_once("conn.php");
$sql = "SELECT * FROM $tName";
$results = mysqli_query($conn, $sql)
or die ('Problem with query' . mysqli_error($conn));
my code correctly prints out the table name as well as the first row data but it is not formatted correctly here is how it looks:
additionally. for some reason it only prints out the first row even though im using a while loop.
My advice to you is to prepare two arrays:
First one: containing column names and second: containing data.
When use two foreach to generate first row with header and second one to display data. You have forgot to add <tr> tags to divide rows.
Use
The mysqli_fetch_field() function returns the next column in the result set as an object. It will only returns all column names not the records of table.
You need to use mysqli_fetch_array() for getting all records:
while ($info = mysqli_fetch_array($results,MYSQLI_ASSOC)) {
{
echo $info['rid'];
echo $info['level'];
....
}
I ended up with using a taras' suggestion of storing the column names in an array:
<table>
<?php
while ($fieldInfo = mysqli_fetch_field($results)) { ?>
<th> <?php echo $fieldInfo->name; ?> </th>
<?php
$colNames[] = $fieldInfo->name;
?>
<?php }
while ($row = mysqli_fetch_array($results)) { ?>
<tr>
<?php for ($i=0; $i<sizeof($colNames); $i++) { ?>
<td><?php echo $row[$colNames[$i]] ?>
<?php } ?>
</tr>
<?php } ?>
</table>
As of my understand, do you want to display all table and their columns?
So you can format like below
$sql = "SHOW TABLES FROM dbname";
$result_tables = mysqli_query($link, $sql);
echo "<table border=1>";
echo "<tr><td>Table name</td><td>Fields name</td></tr>";
while($row = mysqli_fetch_array($result_tables)) {
echo "<tr>";
echo "<td>".$row[0]."</td>";
$sql2 = "SHOW COLUMNS FROM ".$row[0];\\row[0] is used to get table name
$result_fields = mysqli_query($link, $sql2);
echo "<td>";
while($row2 = mysqli_fetch_array($result_fields)) {
echo $row2['Field'].',';
}
echo "</td>";
echo "</tr>";
}
I'm trying to display data from a MySQL table in a html table using php, I've looked at a few tutorials online including answers on StackOverflow... I've implemented it the way said tutorials have described but I am getting no output.
<table border="1px solid black" cellpadding="0px" cellspacing="0px" width="100%">
<thead>
<tr>
<th>Date</th>
<th>Monday</th>
<th>Tuesday</th>
<th>Wednesday</th>
<th>Thursday</th>
<th>Friday</th>
<th>Saturday</th>
<th>Sunday</th>
</tr>
</thead>
<?php
include('dbConnect.php');
$sql = "SELECT * FROM nurses";
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result)) {
echo "<tr>";
echo "<td>" . $row['idno'] . "</td>";
echo "<td>" . $row['surname'] . "</td>";
echo "</tr>";
}
?>
</table>
I know that my db connection is successful as I test for this. All that's getting output is the <thead> and then nothing. I don't understand why this isn't working :/
If you say you have tested this, then there should be no error when trying to execute mysql_query. To be sure that any data is being fetched though, please do a var_dump(mysql_fetch_assoc($result)) after the $result = mysql_query($sql); line to see if at least a single line is being returned from the database. Additionally, you should not use mysql_* functions anymore since they are being deprecated.
Check here how it works. http://php.net/manual/en/function.mysql-query.php
In the case that var_dump returns at least a single result, then most likely $row['idno'] is not a proper column name in your results (you can see what is in the array from the var_dump)
I have a site where members can mark other members as 'a favourite'. User are able to search the members table in various ways and I want to show from any of the results that are returned whether or not the users returned are favourites of the current user.
This is some very simplified code I have been using to try and get this query to work but I just can't figure it out. Whenever I add 'GROUP BY' to avoid duplicate results from my LEFT JOIN the 'if' statement does not work. The 'if' statment does work however, if I omit the 'GROUP BY' but I get all rows from members table and the favourites table. Thanks.
$result = mysqli_query($db_conx, "SELECT members.*, user_favourites.* FROM members LEFT JOIN user_favourites ON members.id = user_favourites.fav_id GROUP BY members.id");
echo "<table border=''>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
<th>A favourite of User</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['firstname'] . "</td>";
echo "<td>" . $row['lname'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
if ($visitor == $userid ){
$msgs = "x";
}
else { $msgs = "0";
}
echo "<td>". $msgs. "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
I suggest to you, tu use 2 query's insted one. Simple query is a fast querys. If your site scale up, your query will be slow query and it cause performance problems.
Idea:
For one hand: SELECT FROM members, get all and put it inside array, key=member.id
$members[$row['member_id']]=$row;
On the other hand: SELECT * FROM user_favourites, and put it inside the previous array, refereced by the key fav_id use distinc if you have duplicates.
$members[$row['fav_id']]['favourites']=$row;
Perfect now you have all you need, an array with all information, iterate it.
JilianJ something like this:
<?
//Prepare
$all_users=array()
$query_users='SELECT * from user';
$query_favourites='SELECT * FROM user_favourites';
//Now I find all members information
$users=mysqli_query($db_conx,$query_users);
while($user = mysqli_fetch_array($users)) {
$all_users[$user['id']]=$user;
}
//Now I add favourite information to the users information
$favourites=mysqli_query($db_conx,$query_favourites);
while($favourite = mysqli_fetch_array($favourites)) {
$all_users[$favourite['fav_id']]['fovourite'][]=$favourite['fav_id'];
}
?>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
<th>A favourite of User</th>
</tr>
<? foreach ($information as $key=>$item) {?>
<tr>
<td><?=$item['firstname'];?></td>
<td><?=$row['lname'];?></td>
<td><?=$row['email'];?></td>
<td>
<? foreach($item['favourites'] as $key2=>$item2) { ?>
<p><?=$all_members[$item2]['name];?></p>
<? } ?>
</td>
</tr>
<? } ?>
</table>
Good luck
First, I really hope I don't get beat down for asking this, but I'm willing to risk it in order to be pointed in the right direction so I can learn.
I have a .php script, and inside my PHP script I have written the code to connect to MySQL database.
Here's the question:
I have a table that will be used for a very simple display of food items. Currently I have:
<table width="1024" border="0" cellpadding="10" cellspacing="5" align="center">
<tr>
<td colspan="2"><font>Item Name</font></td>
<td colspan="2"><font>Item Name</font></td>
<td colspan="2"><font>Item Name</font></td>
</tr>
and it keeps going with other table row elements...
WHERE IT SAYS "ITEM NAME," HOW DO I INPUT CODE TO PULL DATABASE INFORMATION. The database name I have is "TEST" and I'd like each item name to list a different item from the "ITEM" table.
ANY HELP WOULD TRULY BE APPRECIATED.
You really need to do more research on this. Here is an example of Php code that pulls data from database courtesy of w3 schools. See here http://www.w3schools.com/php/php_mysql_select.asp.
<?php
$con=mysqli_connect("example.com","peter","abc123","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM Persons");
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
You have to iterate over the results from the database, otherwise you'll have access only to the first row (or where the array pointer is).
Your question is the basics of programming, and there are million of resources on this topic around the web. I'll just give you a small sample code, and from here you can start debugging and modifying to your taste.
//Connect to MySQL Database
$connection = mysql_connect('host', 'user', 'password') or die("Connection error. Details: ".mysql_error());
//Select the database
$db_select = mysql_select_db('dbname', $connection);
//Create the query
$query = mysql_query("SELECT * FROM `foods`") or die("Error in query. Details: ".mysql_error());
//Check if the query has results, and iterate over it
if (mysql_num_rows($query) > 0)
{
//Creates the table
echo '<table>';
//Iterate over the MySQL results and print the data
while ($result = mysql_fetch_assoc($query))
{
echo '
<tr>
<td>'.$result['item_name'].'</td>
</tr>
';
}
echo '</table>';
}
else
{
echo "Your query hasn't returned results";
}
Try this and don't forget to change the mysql host name, username, password, db name and table name.
Regards.
try this:
$conn = mysql_connect('host', 'user', 'password') or die("Connection error. Details:".mysql_error());
$db_select = mysql_select_db('<your_db_name', $conn);
$sql = mysql_query("SELECT * FROM `foods`")
if($sql)
{
echo '<table>';
while ($result = mysql_fetch_assoc($query))
{
echo '<tr><td>'.$result['<field_name1>'].'</td>';
echo '<td>'.$result['<field_name2>'].'</td>';
.....
.....
echo '</tr>';
}
echo '</table>';
}
else
{
echo "no results is there";
}
note: you should know your table column name , provide the same name in field_names
you can print any number of columns present in your table.
thanks
I am trying to display a table in php. I have established a valid connection. I get the error:
Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /Applications/XAMPP/xamppfiles/htdocs/project.php on line 17
The page's code:
<html>
<head>
<title>PHP Site Michael Mazur</title>
</head>
<body>
<?php
//connect to DB
$con=mysql_connect("localhost","mike","mike");
$db_found = mysql_select_db("my_guitar_shop2");
$result = mysql_query("SELECT firstName,lastName FROM customers");
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['firstName'] . "</td>";
echo "<td>" . $row['lastName'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
</body>
</html>
The rest of your while loop could look like this
while($row = mysql_fetch_array($result)){
print "<tr><td>".$row['Firstname']."</td><td>".$row['Lastname']."</td></tr>";
}
print "</table>";
Try putting these lines on a single line as i have done above.
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";
like
echo "<table border='1'><tr><th>Firstname</th><th>Lastname</th></tr>";
Other useful options here.
http://php.net/manual/en/function.mysql-fetch-array.php
http://php.net/manual/en/control-structures.foreach.php
Given that part of the page's code is missing, this is only a guess. But it looks like part of your problem is that you've double-selected the database (a no-no).
Also, the while statement looks slightly suspect (no opening brace to verify context).
Additionally, if you are going to pass $con to the database selector, you should also pass it to the mysql_query calls (good practice for readability).