A
A
amorusss2016-01-14 15:53:14
PHP
amorusss, 2016-01-14 15:53:14

How to do empty field validation in mysql php table?

It is necessary to validate a certain field. If the field does not contain anything, then show the inscription "not found", and if there is, then vice versa "found"
I tried using mysql_num_rows, the code does not work:

if($_GET['do'] == 'catalog'  && isset($_GET['company'])){
  $id = intval($_GET['company']);
  $query = "SELECT * FROM organizations WHERE `id` = '".$id."'";   
  $res = mysql_query($query);   
  if(mysql_num_rows($res) > null) {  
    echo "найден";   
  } else {echo 'не найден';}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton Tikhomirov, 2016-01-16
@Acuna

if ($_GET['do'] == 'catalog' and isset ($_GET['company'])) {

   $id = (int) $_GET['company'];

   $query = "SELECT * FROM `organizations` WHERE `id` = '".$id."' LIMIT 10";

   $res = mysqli_query ($query);

   while ($row = mysqli_fetch_assoc ($res)) {
     
     if (!$row['site'])
     echo 'Нет';
     else
     echo 'Есть';
     
   }
  
}

Constructive remarks:
1) (int) is faster than intval ()
2) It is enough just to check the variable for falsity, not for emptiness, because anything can be stored there, not necessarily emptiness.
3) Fields and columns must be in quotes.
4) Curly braces are optional if only one condition is checked.
5) Use line breaks. Always. This improves the readability of the code.
6) The word "No" does not exist.

R
romy4, 2016-01-14
@romy4

and check the field, not the number of rows

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question