Forum Moderators: coopster

Message Too Old, No Replies

Displaying Data

Can anyone help with a data display issue?

         

m4tt

6:13 am on Nov 21, 2005 (gmt 0)

10+ Year Member



Hi

I am trying to get the following code to act as a locator file ie:

locator.php?state=NV

meaning that it displays everything from that state..

This is the error message I get on locator.php?state=NV:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/blueline/public_html/wheretobuy/editNews.php on line 20

Here is the code from the index.php page:

<?
include("../connect.php");
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$state=$_POST['state'];
$query=" SELECT * FROM sa ";
$result=mysql_query($query);
$num=mysql_num_rows($result);
mysql_close();

$i=0;
while ($i < $num) {
$state=mysql_result($result,$i,"state");
$company=mysql_result($result,$i,"company");

?>

<p><? echo "$company";?> <a href="locator.php?state=<?echo $state;?>"></a>

<?
++$i;
}
?>

//End Code

Here is the code to display the states (locator.php?state=NV) (example):

<?
include("../connect.php");
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM sa WHERE state=$state";
$result=mysql_query($query);
$num=mysql_num_rows($result);
mysql_close();

$i=0;
while ($i < $num) {
$state=mysql_result($result,$i,"state");
$company=mysql_result($result,$i,"company");

?>

<? echo "$company";?><br />
<? echo "$address";?>
<? echo "$state";?>

<?
++$i;
}
?>

//End Code

Can anyone see my error or am I heading in the wrong direction?

Thanks

Anyango

7:59 am on Nov 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



while debugging this, i would first of all change this to

$result=mysql_query($query);

to

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

actually the query is for any reason not known to me, not returning the results you expect, so there might be any thing mysql_error() would tell you.

;)

m4tt

8:15 am on Nov 21, 2005 (gmt 0)

10+ Year Member



Thanks

I added that and got this:

You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

m4tt

8:28 am on Nov 21, 2005 (gmt 0)

10+ Year Member



Would that mean line 1 at the top of the whole page?

mcibor

10:31 am on Nov 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No, the 1 points to the first line in mysql query.

You have some error in the
$query="SELECT * FROM sa WHERE state=$state";
query

I would still change the code and would replace

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

with

$result=mysql_query($query) or die("Error: ".mysql_error()."<br>SQL: ".$query);

Then see the query and spot the mistake, or send it here. The could be something in the $state variable that could spoil the query.

Kind regards
Michal Cibor

dreamcatcher

11:23 am on Nov 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try enclosing your data in apostrophes:

$query="SELECT * FROM sa WHERE state='$state'";

Also, make sure that the $state variable is actually populated.

dc