Jump to content

Displaying MySQL Results Code Error


phpdog

Recommended Posts

Hey guys,

Having a slight problem with part of the code in my index.php file

 

mysql_select_db('db_name', $con);

$result = mysql_query("SELECT * FROM spy ORDER BY id desc limit 25");
$resulto = mysql_query("SELECT * FROM spy ORDER BY id desc");
$count = mysql_num_rows($resulto);
while($row = mysql_fetch_array($result))
  {
?>
<div class="contentDiv">Someone is looking at <?=$row[title];?> Stats for  "<a href="/<?=$row[type];?>/<?=$row[code];?>/<?=$row[city];?>"><?=$row[code];?> <?=$row[city];?></a>"</div>
<?}?>
		</div>
	  <div id="login"></div>
          <? include("footer.php"); ?>
	</div>
</body>
</html>

 

I'm getting the following error when viewing the file

 

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in ~path to file/index.php on line 70

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in ~path to file/index.php on line 71

 

where lines 70 and 71 are

 

$count = mysql_num_rows($resulto);

while($row = mysql_fetch_array($result))

 

Any ideas on how to fix this?

Link to comment
Share on other sites

change these two lines

$result = mysql_query("SELECT * FROM spy ORDER BY id desc limit 25");
$resulto = mysql_query("SELECT * FROM spy ORDER BY id desc");

to

$result = mysql_query("SELECT * FROM spy ORDER BY id desc limit 25") or die(mysql_error());
$resulto = mysql_query("SELECT * FROM spy ORDER BY id desc") or die(mysql_error());

and let use know what errors you receive if any

Link to comment
Share on other sites

Solved it guys..

 

One more problem in the same sheet (not really a problem actually)

 

<form action="/search.php" method="get">
  <div id="form">

<input id="query" name="query" type="text" onfocus="if(this.value=='enter city name...')this.value='';" onblur="if(this.value=='')this.value='enter city name...';"/>
					<input id="submit" value="find" type="submit" />
				    <input name="action" type="hidden" value="make" />
    </div>
			</form>

 

The user has to type in a city name and click "find'.  Ive noticed you can click the button without actually typing anything in the box..is there any validation or something I could add so youre forced to type in that box (and if you click Find, it returns an error?)

Link to comment
Share on other sites

first, you need to add a name to your submit button so you can check to see if it was pressed via $_POST[]

Now to answer your question..what you'll want to do first is check if your submit button is pressed, then you'll want to make sure that the text field is filled out before processing your data. Try this

if(isset($_POST['submit']))  {

     if(empty($_POST['query']))  {

          echo "please fill out the necessary filelds!";
     } else {

           //process you data
        }
}

Link to comment
Share on other sites

first, you need to add a name to your submit button so you can check to see if it was pressed via $_POST[]

Now to answer your question..what you'll want to do first is check if your submit button is pressed, then you'll want to make sure that the text field is filled out before processing your data. Try this

if(isset($_POST['submit']))  {

     if(empty($_POST['query']))  {

          echo "please fill out the necessary filelds!";
     } else {

           //process you data
        }
}

 

Okay, where do I paste that though? Not too familiar with this :S

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.