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
https://forums.phpfreaks.com/topic/236314-displaying-mysql-results-code-error/
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

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?)

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
        }
}

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.