0

I have a function that retrieve some data from table and echo it. The function is...

function myfunction(){
  global $db,$con;
  $qry = "SELECT field1 FROM tbl ORDER BY fieldId ASC";
  mysql_select_db($db,$con);
  $qry_res = mysql_query($qry,$con);
  $numRow = mysql_num_rows($qry_res);

  if($numRow > 0){
      while ($res = mysql_fetch_array($qry_res)) {
          echo '$res['field1']';
      }        
  } else {
      echo 'nothing found';
  }

}

My table is not empty.

the above function is giving an warning Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\iktt\app\_include\db\functions.php on line 70

Could not find the problem. What should I do ?

Raja
  • 760
  • 1
  • 13
  • 36
  • does `tbl` have `field1` and `fieldId` columns? – Mureinik Aug 12 '14 at 21:16
  • 3
    The query failed and returned false instead of a result, also use `mysqli` functions since `mysql` has been deprecated. – Ende Neu Aug 12 '14 at 21:16
  • The message tells you that the query is not valid. – msfoster Aug 12 '14 at 21:17
  • 2
    Stop using deprecated mysql_* functions. The php mysql extension is unsupported. Convert your script to use PDO. – Charlotte Dunois Aug 12 '14 at 21:17
  • @Mureinik Yes the tbl have field1 and fieldId columns and the table is not empty – Raja Aug 12 '14 at 21:17
  • If you have to use the deprecated functions, maybe try `$qry_res = mysql_query($qry,$con) or die(mysql_error());` Since the query is failing, this should tell you what error the MySQL server gave. – chilyashev Aug 12 '14 at 21:18
  • @chilyashev He doesn't have to use deprecated functions. Either he wants to use them or he's too lazy to use new and better functions. Or both. – Charlotte Dunois Aug 12 '14 at 21:19
  • 1
    @CharlotteDunois While I fully agree, changing database engines won't prevent the query itself from being malformed. – Niet the Dark Absol Aug 12 '14 at 21:19
  • @chilyashev that would at least reveal what might have gone wrong in the query, but it would also kill script execution. There are far better ways of dealing with failures than simply dying. – GordonM Aug 12 '14 at 21:19
  • @NiettheDarkAbsol I think you're getting the wrong idea. MySQLi or PDO are PHP extensions and **not** database engines. – Charlotte Dunois Aug 12 '14 at 21:21
  • @Raj I suggest that you consider using a title, which is more specific to the problem that you are trying to solve. Imagine others also using similar titles "I cannot solve the problem", "Database not working", etc. It would be difficult to identify the question clearly. Please see http://stackoverflow.com/help/how-to-ask. It is recommended that you even give a title after writing out the question. – Joseph B Aug 12 '14 at 21:23
  • @CharlotteDunois What's in a name here? :p You're right, I meant database drivers, but the point still stands: if the query is wrong, it doesn't matter how you execute it. – Niet the Dark Absol Aug 12 '14 at 21:24
  • @Raj Curious; any particular reason why you're using `mysql_*` functions? Seems like some here are *particularly* offended/bothered by your use of it. Personally, any MySQL API is worthy, just as long as proper precautions are taken to guard against SQL injection. – Funk Forty Niner Aug 12 '14 at 21:24
  • 1
    @CharlotteDunois I've had projects where I had to use old PHP versions with tons of legacy code and using deprecated functions was the least of my problems, maybe that's the case here :) – chilyashev Aug 12 '14 at 21:26
  • @GordonM of course there are better ways, but since it's just an example of the problem, dying is the simplest and quickest solution. – chilyashev Aug 12 '14 at 21:27

0 Answers0