Jump to content

Outputting Data from a Database


stublackett

Recommended Posts

Hi,

 

I'm trying to output Data from a Database, Basically showing Users of the website in a table format

 

I'm getting the error : Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource

 

I'd also like to know, How you output this as a Table just so it looks neater, I'm retrieving Username, FullName and level from the Database

 

The code is as follows :

<?php
include ("dbconnect.php");
//Make DB Connection to retrieve User Info
mysql_connect($hostname, $db_user, $db_password)or die("cannot connect");
mysql_select_db($dbname)or die("cannot select DB");
echo $query;

//Query to retrieve User Info
$query="SELECT username, fullname, level FROM $db_table";
   $result=mysql_query($sql);
   
//Show Results
while ($result = mysql_fetch_array($query)) {
echo "<br />Username : " .$result['username']. " <br /> Fullname : " .$result['fullname']." <br /> Userlevel : " .$result['level']."";}

?>

 

I changed the $result to mysql_fetch_row and it output numerous lines of Username, Fullname and Userlevel

 

Any help is appreciated

Link to comment
https://forums.phpfreaks.com/topic/91736-outputting-data-from-a-database/
Share on other sites

First things first, why is

 

echo $query;

 

there.  $query doesn't exist yet so it won't get echo'd.  Not to mention your $result is trying to query $sql (which from what I can see should be $query).

 

Also, check out your mysql_fetch_array().  You're giving it $query, rather than $result. Also, change your loop variable, try this:

 

while($row = mysql_fetch_array($result)) {

 

Then remember to change all your $result['field'] to $row['field'].  As for the outputting as a Table, it just requires some simple HTML.  Remember you can end your PHP tags and use HTML within a loop:

 

<table width="600" border="1" align="center">
<tr><td width="200"><b>Username</b></td><td width="200"><b>Fullname</b></td><td width="200"><b>Userlevel</b></td></tr>
<?php
$query="SELECT username, fullname, level FROM $db_table";
$result=mysql_query($query) or die (mysql_error());
   
//Show Results
while ($row= mysql_fetch_array($result)) {

$username = $row['username'];
$fullname = $row['fullname'];
$userlevel = $row['level'];

?>

<tr><td width="200"><?php echo $username; ?></td><td width="200"><?php echo $fullname; ?></td><td width="200"><?php echo $userlevel; ?></td></tr>

<?php } ?>

</table>

The echo was me just trying to work out wether there was an MySQL Conflict, Was totally in the wrong place as you say

 

I've got that up and running now and sitting quite nicely in a table, Thanks for your help with that!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.