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
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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.