PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
I have a strange problem pulling info from my database.
I use the code below and on my home servers I've used (previously windows, and now OS X) it works fine, but on my hosted (red hat) server I get this error message:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /thisisafakepath/filename.php on line 57
show us th wholw script. it'll be easier to see what is happening, because from the partial script you have given us, it looks like you have actually connected to the database.
<?
include("header.php");
require("config.php");
?>
<!--Content left and right colums-->
<link href="v5.css" rel="stylesheet" type="text/css">
<table width="800" border="0" cellpadding="0" cellspacing="0" class="td1">
<tr>
<!--LEft Column-->
<td width="216" align="center" valign="top" class="cell">
<br>
<!--soundfile sidebar-->
<table width="90%" border="0" align="center" cellpadding="0" cellspacing="0">
<? $boxtoptext ='Side info box title goes here';
include("box_top.php"); ?>
side info box text goes here, please note that box_top.php has the opening <td> required for this, so don't stress.
</td>
</tr>
</table>
<!--END soundfile sidebar-->
<br>
<!--Amazon box-->
<?
include("amazon_box.php");
?>
<!--End Amazon box-->
</td>
<!--End LEft Colums-->
<!--RIght Column-->
<td width="584" align="center" valign="top" class="cell">
<br>
<!--soundfile column-->
<?
$content = mysql_db_query($dbname, "SELECT * FROM current_round ORDER BY id DESC");
$Xcontent = mysql_fetch_array($content);
$filename = $Xcontent["filename"];
$monthname = $Xcontent["monthname"];
$year = $Xcontent["year"];
$month = $Xcontent["month"];
$day = $Xcontent["day"];
$length = $Xcontent["length"];
$filmlink = $Xcontent["filmlink"];
$transcript = $Xcontent["transcript"];
$length = $Xcontent["length"];
$filesize = $Xcontent["filesize"];
?>
<table width="90%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="100%" align="left" valign="top" class="td2">
<?
print "$monthname $year archived sound file...";
?>
</td>
</tr>
<tr>
<td width="80%" align="left" valign="top" class="td1">
<?
print "<b>Sound File:</b> <a href="/archives/$year/$month/$filename">$filename</a><br>";
print "<b>File Size:</b> $filesize Kb<br>";
print "<b>Length of clip:</b> $length seconds<br>";
print "<b>Film that clip is taken from:</b> <a href="$filmlink" target="new">Click to see film</a>";
?>
</td>
</tr>
</table>
<!--END soundfile right column-->
<br>
<!--transcript column-->
<table width="90%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="100%" align="left" valign="top" class="td2">
Transcript...
</td>
</tr>
<tr>
<td width="80%" align="left" valign="top" class="td1">
<?
print "$transcript";
?>
</td>
</tr>
</table>
<!--END transcript right column-->
<br>
<!--date due column-->
<table width="90%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="100%" align="left" valign="top" class="td2">
When was the deadline for this archived sound file?
</td>
</tr>
<tr>
<td width="80%" align="left" valign="top" class="td1">
This entry was for
<?
print "$monthname";
print " $year";
?>
</td>
</tr>
</table>
<!--END date due right column-->
</td>
<!--End RIght Colums-->
</tr>
</table>
<!--End Left and Right Columns-->
<!--Footer-->
<?
include("footer.php");
?>
<!--End Footer-->
What's interesting with this, is it's not even showing the database contents now, but on my work machine (windows box) I've tried it and it works a treat. I'm so confused now.
Thanks again, I hope the whole script helps. It's probably a bit messy for you pros, but I am a learner (and loving it!)
are you doing any validation on your mysql_connect or mysql_db_query statements, because that is what the error is associated with, are the password, usernames, dbname and ip address all correct?!?
what is on is line 60 now? (or whatever line number is in the error message)
What happens when you enter "SELECT * FROM current_round ORDER BY id DESC" in the MySQL Command Line Client or in a tool like PHPMyAdmin? I've found the most stupid errors this way, like typos in the table name and such.
Firstly, you should not be using mysql_db_query() as it is a deprecated function which the manual explicitely tells you not to use. Instead you should be using mysql_select_db() and mysql_query(). Add a bit of error handling to your code and see what you get:
@mysql_select_db($dbname) or die(mysql_error());
$sql = "SELECT * FROM current_round ORDER BY id DESC";
$content = mysql_query($sql) or die(mysql_error().'<p>'.$sql.'</p>');
I assume you are connecting to MySQL in one of your included files - if you're not then that'll be the problem.
Actually, if the code works on your system and not the hosting system, I would suspect that the userid does not have the select privileges on the database table on the hosting box.
Thanks for the reminder on the db_query redundancy Twigletmac.
ALL of my files have the old style. I'm a bit worried, if I leave them (due to the sheer time it will take to change them all) will this feature ever be... swtiched off so to speak? I sure hope not.
Also, thanks for the suggestion on the server side stuff Stoneguard. I'll talk to the admin on the server about that. Very helpful staff at netprohosting. They should give me some quick response...