1

I want to display data from database with condition when data is not found will return string value "data not available or already deleted"

$Q = mysql_query("SELECT * 
                    FROM geosekolah 
                        JOIN kecamatan USING (id_kecamatan) 
                        JOIN kelurahan USING (id_kelurahan) 
                    where id_sekolah=".$id )
            or die(mysql_error());

if($Q){
  $posts = array();

  if(mysql_num_rows($Q))
  {
         while($post = mysql_fetch_assoc($Q)){
                 $posts[] = $post;
         }
  }
  $data = json_encode(array('results'=>$posts));

  if (!mysql_num_rows($Q)) {
    echo "data not available or already deleted";
    exit;
  }

}

above is the code I created but it did not work. when the data is contained in the database, always display the string "data not available or already deleted" how to solve the problem, thanks for everyone who has helped answer.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • Your Php version? – frz3993 May 02 '18 at 14:10
  • 2
    Every time you use [the `mysql_`](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) database extension in new code **[this happens](https://media.giphy.com/media/kg9t6wEQKV7u8/giphy.gif)** it is deprecated and has been for years and is gone for ever in PHP7. If you are just learning PHP, spend your energies learning the `PDO` or `mysqli` database extensions and prepared statements. [Start here](http://php.net/manual/en/book.pdo.php) – RiggsFolly May 02 '18 at 14:11
  • see [mysql_affected_rows](http://php.net/manual/en/function.mysql-affected-rows.php) – Nicholas Nur May 02 '18 at 14:11
  • First of all I would check if mysql_ is working because it is deprecated after PHP 5.5. If it works for you then I'll check if mysql_fetch_assoc is actually returning anything at all. You can always check $posts and it's sizeof() instead of mysql_num_rows in the condition. – Riccardo May 02 '18 at 14:16
  • @NicholasNur that function is not for SELECTs – Riccardo May 02 '18 at 14:17
  • 1
    For one thing, you're only echoing if there are no rows found. What is the version of php that this is running off of? – Funk Forty Niner May 02 '18 at 14:23
  • Plus, did you even connect successfully using the same mysql api? Does `$id` have value? – Funk Forty Niner May 02 '18 at 14:24
  • Love it when they either no longer respond or leave. – Funk Forty Niner May 02 '18 at 14:50

0 Answers0