0

So I have a PHP file where a user will search through questions that are in the database, if they search for something random ('ddwdwdwdw') etc. I want it to output a message saying that nothing was returned. However I'm having a little trouble, it might be because my code isn't set up very well?

I've tried doing:

if(mysql_num_rows($sql) < 1){
echo "blah blah blah";
} 

But it outputs a MySQL error because the sql fails(which obviously in this case isnt a bad thing). And it outputted the echo message with every type of search, so maybe it was in the wrong position. Thanks for any help, and please ask if I havent made sense.

Form Code:

<html>
<head>
<title>Physics</title>
</head>
<body>

<p> Search for a question type. </p>
<form action = "searchingQuestions.php" method= "POST">
<select name= "Type_DROP">
Question type:
  <option value = "NULL"></option>
  <option value="SUVAT">Suvat</option>
  <option value="FORCES">Forces</option>
  <option value="WORK">Work</option>
  <option value="Energy">Energy</option>
  <option value = "ALL"> All </option>
</select>

 <br>
 <br>
 <p> Or search for a certain text.</p>
<input type = "text" name = "text_try">  
<br><br>
<p>Would you like to search through revision questions as well, check if you would like to. </p>
<input type = "checkbox" name = "checkbox" value = "value1">
<br><br>
<input type = "submit" id="submit" value="search">
<div id= "name-data"></div>





</form>



</body>
</html>

php: http://pastie.org/10791058

6
  • 2
    What do you mean an SQL error isn't a bad thing in this case? An SQL error is always a bad thing. Commented Apr 9, 2016 at 15:28
  • 1
    @Chris Specific errors actually are a very good thing, since they offer a defined way to handle a situation that should never have occurred... Commented Apr 9, 2016 at 15:29
  • @arkascha Well sure in some sense it's better than failing silently, but it always indicates a problem rather than just being a normal part of operation. Commented Apr 9, 2016 at 15:31
  • We don't really need to see the entire HTML, just the MCVE... Commented Apr 9, 2016 at 15:31
  • imo, Run the query. Assume it will always return something. Always fetch the rows and display them. Count them when you display them (used for paging anyway). Output the 'no rows found' if count is zero. Note: there is no test for num_rows. imo, testing 'number of rows returned' is rarely useful as you want to process the data anyway and you always have an 'end of rows test' in the fetch loop. Commented Apr 9, 2016 at 15:33

2 Answers 2

2

Please take the time to read the documentation of the tools and functions you use before asking here:

http://php.net/manual/en/function.mysql-num-rows.php

It includes a simple example which shows how to use that function. In short: you first have to execute the query, get a resource handle back which you need to hand over to the mysql_num_rows() function as an argument:

<?php
$link = mysql_connect("localhost", "mysql_user", "mysql_password");
$result = mysql_query("SELECT * FROM table", $link);
$num_rows = mysql_num_rows($result);
Sign up to request clarification or add additional context in comments.

6 Comments

It might also be good to add that this function refers to an extension that has been deprecated for a long time, and totally removed in the most recent version of PHP. Consider using PDO instead.
@light Actually that is clearly and prominently stated right in the introductory text in the documentation I referenced. I don't see any point in doubling that here.
Per the pastie link OP is using mysqli not mysql_.
@chris85 That link was added much later, I cannot see any connection to the actual question asked. Looks more like an arbitrary script, not the actual code used by the OP...
Thanks for the help, ive got it working now and will make sure to look through the link. However now when I dont input anything into the boxes I get errors while before it would output everything? I want it to output everything, which it isnt doing now. Updated code: pastie.org/10791286 form is still the same.
|
0

The mysql_num_rows() expecting a mysql query object. Try this:

$result = mysql_query($sql, $link);
if(mysql_num_rows($result) < 1){
   echo "blah blah blah";
}

Note: mysql functions is deprecated in the latest php versions, instead use mysqli functions.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.