Page 1 of 1

mysql_fetch_array()

Posted: Wed Aug 15, 2007 3:06 pm
by metroid87706
Hi. With this code below, i get this error:

Code: Select all

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in MY PATHS BLOCKED/inbox.php on line 31
Please help me figure out why.
I am trying to connect the the table with the current users name, registered as a value. ($myusername)
Thanks for the help!

Code: Select all

<?
session_start();
if(!session_is_registered(myusername)){
header("location:loginfirst.php");
}
?>
<?php
$host="MY HOST"; // Host name
$username="MY USERNAME"; // Mysql username
$password="MY PASSWORD"; // Mysql password
$db_name="MY DATABASE"; // Database name
$tbl_name=".$myusername."; // Table name

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

$sql="SELECT * FROM $tbl_name OREDER BY id DESC";
// OREDER BY id DESC is order result by descending
$result=mysql_query($sql);
?>
<table width="90%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td width="6%" align="center" bgcolor="#E6E6E6"><strong>#</strong></td>
<td width="53%" align="center" bgcolor="#E6E6E6"><strong>Topic</strong></td>
<td width="15%" align="center" bgcolor="#E6E6E6"><strong>Sender</strong></td>
<td width="13%" align="center" bgcolor="#E6E6E6"><strong>Date/Time</strong></td>
</tr>

<?php
while($rows=mysql_fetch_array($result)){ // Start looping table row
?>
<tr>
<td bgcolor="#FFFFFF"><? echo $rows['id']; ?></td>
<td bgcolor="#FFFFFF"><a href="view_topic.php?id=<? echo $rows['topic']; ?>"><? echo $rows['topic']; ?></a><BR></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['sender']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['datetime']; ?></td>
</tr>

<?php
// Exit looping and close connection
}
mysql_close();
?>
<tr>
    <td colspan="5" align="right" bgcolor="#E6E6E6"><a href="create_topic.php"> </a></td>
</tr>
</table>

Posted: Wed Aug 15, 2007 3:09 pm
by TheMoose
Your query most likely returned an error.

Code: Select all

// try this
$result=mysql_query($sql) or die(mysql_error());

Posted: Wed Aug 15, 2007 3:10 pm
by miro_igov
It will be good habit to add or die(mysql_error()) syntax after mysql_query() but i see something is wrong with this $tbl_name=".$myusername.";

I think it should be $tbl_name=$myusername;

Posted: Wed Aug 15, 2007 3:55 pm
by feyd
Is this not the continuation of viewtopic.php?t=72441?

Posted: Thu Aug 16, 2007 2:20 am
by RhapX
You have this:

$sql="SELECT * FROM $tbl_name OREDER BY id DESC";

It should be this:

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

Simple typo :D