Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.
Moderator: General Moderators
Glynn
Forum Newbie
Posts: 2 Joined: Sun Oct 17, 2004 1:08 pm
Post
by Glynn » Sun Oct 17, 2004 1:19 pm
nigma | Help us, help you. Please use Code: Select all
tags where approriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
Why do I get this error "Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource" on a live server when it works ok on localhost. Here is my code.Code: Select all
$SQL="SELECT COUNT(*) AS Total FROM listingsdb WHERE Active LIKE 'Yes'";
$SQL_Result=mysql_db_query($db_name, $SQL);
$SQL_Result_Array=mysql_fetch_array($SQL_Result);
$Total=$SQL_Result_Array['Total'];
/* Get the records */
$SQL="SELECT * FROM listingsdb ORDER BY ID DESC";
/* Limit the SET to the per page size */
if (empty($_GET['Result_Set']))
{
$Result_Set=0;
$SQL.=" LIMIT $Result_Set, $Per_Page";
} else
{
$Result_Set=$_GET['Result_Set'];
$SQL.=" LIMIT $Result_Set, $Per_Page";
}
/* Run the Query with a limit to get results */
$SQL_Result=mysql_db_query($db_name, $SQL);
$SQL_Rows=mysql_num_rows($SQL_Result);
timvw
DevNet Master
Posts: 4897 Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium
Post
by timvw » Sun Oct 17, 2004 1:47 pm
debugging 101
Code: Select all
error_reporting(E_ALL);
mysql_connect('host', 'user', 'pass') or die(mysql_error());
$query = 'select * from foo';
$result = mysql_query($query) or die(mysql_error());
if ($result)
{
while ($row = mysql_fetch_assoc($result))
{
print_r($row);
}
}
Glynn
Forum Newbie
Posts: 2 Joined: Sun Oct 17, 2004 1:08 pm
Post
by Glynn » Sun Oct 17, 2004 3:20 pm
nigma | Help us, help you. Please use Code: Select all
tags where approriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
I have tried to debug with the code you posted but as a newbie I don't understand.
Here is my full code for the page perhaps you can see your way clear to help.Code: Select all
<?php
include("../include/common.php");
loginCheck('User');
?>
<?php
/* Set Variables */
$db_host="localhost";
$db_name="**********";
$db_user="**********";
$db_pass="**********";
$Per_Page=25;
/* Open MySQL Connection */
$Connection=mysql_connect($db_host, $db_user, $db_pass);
?>
<!doctype html public "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="en">
<head>
<title>List All Entries</title>
<link rel="STYLESHEET" type="text/css" href="fish.css"/>
</head>
<body>
<div id="listcontainer">
<?php
echo "<h2>List All Entries</h2><p> </p>\n";
// Find out how many records there are, and get them
$SQL="SELECT COUNT(*) AS Total FROM listingsdb WHERE Active LIKE 'Yes'";
$SQL_Result=mysql_db_query($db_name, $SQL);
$SQL_Result_Array=mysql_fetch_array($SQL_Result);
$Total=$SQL_Result_Array['Total'];
/* Get the records */
$SQL="SELECT * FROM listingsdb ORDER BY ID DESC";
/* Limit the SET to the per page size */
if (empty($_GET['Result_Set']))
{
$Result_Set=0;
$SQL.=" LIMIT $Result_Set, $Per_Page";
} else
{
$Result_Set=$_GET['Result_Set'];
$SQL.=" LIMIT $Result_Set, $Per_Page";
}
/* Run the Query with a limit to get results */
$SQL_Result=mysql_db_query($db_name, $SQL);
$Rows=mysql_num_rows($SQL_Result);
/* Create Next Prev Links and $Result_Set Value */
echo "<div class="pagelimit"> \n";
if ($Total>0)
{
if ($Result_Set<$Total && $Result_Set>0)
{
$Res1=$Result_Set-$Per_Page;
echo "<A HREF="list_all.php?Result_Set=$Res1">Previous Page</A> ";
}
/* Calculate and Display Page # Links */
$Pages=$Total / $Per_Page;
if ($Pages>1)
{
for ($b=0,$c=1; $b < $Pages; $b++,$c++)
{
$Res1=$Per_Page * $b;
echo "<A HREF="list_all.php?Result_Set=$Res1">$c</A> \n";
}
}
if ($Result_Set>=0 && $Result_Set<$Total)
{
$Res1=$Result_Set+$Per_Page;
if ($Res1<$Total)
{
echo "<A HREF="list_all.php?Result_Set=$Res1"> Next Page</A>";
}
}
}
echo "</div><p> </p> \n";
/* Display Results using a For Loop */
echo "<Table width="90%" align="center" cellpadding="4" cellspacing="0">\n";
echo "<TR class="thd"><TD><b>ID</b></TD><TD><b>Property</b></TD><TD><b>Select</b></TD></TR>\n";
for ($a=0; $a < $SQL_Rows; $a++) {
$SQL_Array=mysql_fetch_array($SQL_Result);
if ($a%2) {
echo "<TR class="ev">\n";
} else {
echo "<TR class="od">\n";
}
echo '<td valign="top">'.$SQL_Array['ID'].'</td><td valign="top">'.$SQL_Array['Title'].'</td><td valign="top"><a href="confirm_upload.php?Result_Set='.$Result_Set.'&id='.$SQL_Array['ID']."">Upload</a></td></tr>\n";
}
echo "</Table><p> </p>\n";
echo "<div class="pagelimit"> \n";
/* Create Next Prev Links and $Result_Set Value */
if ($Total>0)
{
if ($Result_Set<$Total && $Result_Set>0)
{
$Res1=$Result_Set-$Per_Page;
echo "<A HREF="list_all.php?Result_Set=$Res1">Previous Page</A> ";
}
/* Calculate and Display Page # Links */
$Pages=$Total / $Per_Page;
if ($Pages>1)
{
for ($b=0,$c=1; $b < $Pages; $b++,$c++)
{
$Res1=$Per_Page * $b;
echo "<A HREF="list_all.php?Result_Set=$Res1">$c</A> \n";
}
}
if ($Result_Set>=0 && $Result_Set<$Total)
{
$Res1=$Result_Set+$Per_Page;
if ($Res1<$Total)
{
echo "<A HREF="list_all.php?Result_Set=$Res1"> Next Page</A>";
}
}
}
echo "</div><p> </p> \n";
/* Close Database Connection */
mysql_close($Connection);
?>
</div>
</body>
</html>
feyd | removed username/password
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Sun Oct 17, 2004 4:56 pm
add the "or die(mysql_error())" type stuff to your code to help you figure out what's going on.