Log in

View Full Version : Weird problem?



xiofire
05-19-2009, 11:00 AM
So I am setting up a site, and need some help with the members list. For some odd reason, I get the error:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/USERNAME/alpha/members.php on line 36

Although, I didn't set this up any different than my other lists. Here is the code.


<?php
// Register your Members session
session_start();

// Connect to the Database
$host="localhost"; // Host name
$dbusername="CENSORED"; // Mysql username
$dbpassword="CENSORED"; // Mysql password
$db_name="CENSORED"; // Database name
$tbl_name="members"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$dbusername", "$dbpassword")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name ORDER BY id DESC";
// OREDER BY id DESC is order result by descending
$result=mysql_query($sql);
?>

<!-- Call the Header -->
<?php
include('include/header.php');
?>

<!-- Call the Navigation -->
<?php
include('include/navigation.php');
?>

<div class="main">
<h2>Members List</h2>
<table width="100%" border="0" align="center" cellpadding="3" cellspacing="1">
<?php
// Loop results
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td><? echo $rows['username']; ?></td>
</tr>
<?php
// Exit looping and close connection
}
mysql_close();
?>
</table>
</div>

<!-- Call the Sidebar -->
<?php
include('include/sidebar.php');
?>

<!-- Call the Footer -->
<?php
include('include/footer.php');
?>

Any help is greatly appreciated. :)

Nile
05-19-2009, 11:39 AM
That most likely means that:
1. Your connecting to the database has failed, or
2. You're query doesn't make sence instead of:


$result=mysql_query($sql);

Try:


$result=mysql_query($sql) or die(mysql_error());

xiofire
05-19-2009, 07:26 PM
The mysql_error seemed to help, but now its telling me that my table doesn't exist. :S

Nile
05-19-2009, 11:19 PM
Try:


$sql="SELECT * FROM `'$tbl_name'` ORDER BY `id` DESC";

If not, try:


$sql="SELECT * FROM `$tbl_name` ORDER BY `id` DESC";
Or:


$sql="SELECT * FROM '$tbl_name' ORDER BY `id` DESC";

VolcomMky
05-20-2009, 06:26 PM
Well how is your database tables laid out?

SQL statements are usually case sensitive so double check the name of the table.

and I would try this


$sql="SELECT * FROM " . $tbl_name . " ORDER BY `id` DESC";