Error if empty

Hi guys need help with this…i have registration table,when table have data everything is ok but if empty i got error…

code:

 <?php 
                            $query = "SELECT * FROM prijava WHERE prijava_category_id = $prijava_category_id";
                            $select_all_prijava_query = mysqli_query($connection, $query);

                            while($row = mysqli_fetch_assoc($select_all_prijava_query)) {
                            $prijava_id= $row['prijava_id'];
                            $prijava_category_id = $row['prijava_category_id'];
                            $comp_name = $row['comp_name'];
                            $comp_licence = $row['comp_licence'];
                            $comp_class = $row['comp_class'];
                            $comp_cat = $row['comp_cat'];
                            $comp_club = $row['comp_club'];
                            $comp_country = $row['comp_country'];
                            $prijava_count = $row['prijava_count'];
                          
 
           
                
               echo "<tr>";   
					
					
                 ?>
  
 <?php
  
    echo "<td>$prijava_id</td>";
    echo "<td>$comp_name</td>";
    echo "<td>$comp_licence</td>";
 
    $query = "SELECT * FROM categories WHERE cat_id = $prijava_category_id";
    $select_categories_id = mysqli_query($connection,$query);

    while($row = mysqli_fetch_assoc($select_categories_id)) {
    $cat_id= $row['cat_id'];
    $cat_title = $row['cat_title'];
    }
    echo "<td>$cat_title</td>";
    echo "<td>$comp_class</td>";
    echo "<td>$comp_club</td>";
    echo "<td>$comp_country</td>";
             
 echo "</tr>";
     }
       ?>           
</table>
         <table id="reg-num">   
         <br>       
          <?php 
    $query = "SELECT * FROM prijava WHERE prijava_count = $prijava_count";
    $send_prijava_query = mysqli_query($connection, $query);
    $row = mysqli_fetch_array($send_prijava_query);
    $prijava_id = $row['prijava_id']; 
    $count_prijava = mysqli_num_rows($send_prijava_query);


            echo "<tr>";
            echo "<th>Ukupno / Total</th>"; 
            echo "<td>$count_prijava</td>"; 
            echo "</tr>";
           
            
        ?> 
    </table>

Your query failed. Echo it out and try it in the terminal. If you read the document it clearly states mysqli_query returns false when the query fails.

The part here (hint: the docs will typically have the args and return values for every function). This is referred to as the signature.

Just added
$prijava_count = 0;
thats all

For now but what happens when the script fails again or requires updating?

I am in the process of writing a complicated MySqli project that has numerous queries because tables have to be dropped, created, populated from a CSV file, indexed, extra columns added and updates appended on a daily basis.

I have found using PHP Heredoc strings very helpful because they are easy to read AND the echoed $query can be copied and pasted directly into PhpMyAdmin.

BEWARE:
It is also well worth taking the time to learn how to use PHP HEREDOC, the syntax has to be exact and fails if any characters, including spaces follow the start and finish lines.

It is worth noting that complicated queries can be pasted into the PhpMyAdmin SQL text box and clicking the “Format” button make complicated queries far easier to debug. After running the query, “Get auto-saved query” will recall the last query.

I use a common function instead of calling mysqli_query($connection, $query); because the script fails gracefully and optionally shows a formatted query which can be time consuming.

I was procrastinating and decided to modify your script:

<?php 
  declare(strict_types=1);
  ini_set('display_errors', 'TRUE');
  error_reporting(-1);

  if( isset($connection) ):
    // NO PROBLEM
  else:
    $connection = 'MY CONNECTION NOT SET';  
  endif;  

  // DEFAULTS
    $prijava_id     = 'NOT FOUND';
    $comp_name      = 'NOT FOUND';
    $comp_licence   = 'NOT FOUND';
    $prijava_id     = 'NOT FOUND';
    $cat_title      = 'NOT FOUND';
    $comp_class     = 'NOT FOUND';
    $comp_club      = 'NOT FOUND';
    $comp_country   = 'NOT FOUND';
    $prijava_count  = 'NOT FOUND';

  if( isset($prijava_category_id) ):  
    // NO PROBLEM
  else:
    $prijava_category_id = 'DEFAULT VALUE';   
  endif;

// PHP HEREDOC
  $query = <<< ____TMP
    SELECT 
      * 
    FROM 
      prijava 
    WHERE 
      `prijava_category_id` = $prijava_category_id;
____TMP;

  $select_all_prijava_query = fnMySQLi_query($connection, $query);
  if($select_all_prijava_query):
    while($row = mysqli_fetch_assoc($select_all_prijava_query))
    {
      $prijava_id           = $row['prijava_id'];
      $prijava_category_id  = $row['prijava_category_id'];
      $comp_name            = $row['comp_name'];
      $comp_licence         = $row['comp_licence'];
      $comp_class           = $row['comp_class'];
      $comp_cat             = $row['comp_cat'];
      $comp_club            = $row['comp_club'];
      $comp_country         = $row['comp_country'];
      $prijava_count        = $row['prijava_count'];
    }  
  endif;  
  echo "<tr>";   
  echo "<td> $prijava_id </td>";
  echo "<td> $comp_name </td>";
  echo "<td> $comp_licence </td>";

// PHP HEREDOC
  $query = <<< ____TMP
    SELECT 
      * 
    FROM 
      categories 
    WHERE 
      `cat_id` = $prijava_category_id;
____TMP;
  $select_categories_id = fnMySQLi_query($connection,$query);

  if($select_categories_id):
    while($row = mysqli_fetch_assoc($select_categories_id))
    {
    $cat_id    = $row['cat_id'];
    $cat_title = $row['cat_title'];
    }
  endif;  
  echo "<td>$cat_title</td>";
  echo "<td>$comp_class</td>";
  echo "<td>$comp_club</td>";
  echo "<td>$comp_country</td>";
  echo "</tr>";
echo '</table>';

echo '<table id="reg-num">';   
// PHP HEREDOC
  $query = <<< ____TMP
    SELECT 
      * 
    FROM 
      prijava 
    WHERE 
      `prijava_count` = $prijava_count;
____TMP;
    $send_prijava_query = fnMySQLi_query($connection, $query);

    $count_prijava = 'NOT FOUND count_prijava';
    if($send_prijava_query):
      $row = mysqli_fetch_array($send_prijava_query);
      $prijava_id     = $row['prijava_id']; 
      $count_prijava  = mysqli_num_rows($send_prijava_query);
    endif;  
    echo "<tr>";
      echo "<th>Ukupno / Total</th>"; 
      echo "<td>$count_prijava</td>"; 
    echo "</tr>";
echo '</table>';


//=======================================
function fnMySQLi_query($connection, $query, $HIDE=FALSE)
{
  $connection = 'mysqli_connect()';

  try
  {
    $result = mysqli_query($connection, $query); 

  } catch (Exception $e) {
    if($HIDE):
      // 
    else:  
      echo '<div style="background-color:orange"><hr>'
          . '<b> Failed gracefully </b><br>'
          . 'function _tryCatch($sql) <br>'
          . 'CAUGHT: $getMessage() ==> <br>' 
          . $e->getMessage() 
          . '<br>'
          . ' $query ==> '
          . '<pre>' .$query .'</pre>'
          .'<hr></div>';
    endif;      
  }

  return $result;
}
1 Like

Is the value for $prijava_category_id submitted by the user?

its simple registration form for some competition no users just submit button

Fatal error: Uncaught TypeError: mysqli_query() expects parameter 1 to be mysqli, string given in C:\xampp\htdocs\fai\tabela.php:268 Stack trace: #0 C:\xampp\htdocs\fai\tabela.php(268): mysqli_query(‘mysqli_connect(…’, ’ SELECT \r\n …‘) #1 C:\xampp\htdocs\fai\tabela.php(191): fnMySQLi_query(‘mysqli_connect(…’, ’ SELECT \r\n …’) #2 {main} thrown in C:\xampp\htdocs\fai\tabela.php on line 268

Many thanks for trying the script.

The original script did not show how the database was connected. Modify the following lines and include the database connection:

 if( isset($connection) ):
    // NO PROBLEM
  else:
    // FOLLOWING LINE TO BE MODIFIED TO CONNECT TO DATABASE
    $connection = 'CONNECT DATABASE - possibly include "connectDatabase.php"';
  endif;  

Hi,

I will give a pretty straight and simple solution.
$select_all_prijava_query = mysqli_query($connection, $query);
As we know mysqli_query() will return false on empty so just need to check it

if(mysqli_query($connection, $query) != false){
run your code...
}else{
echo "no results found";
}
1 Like

tnx for help it works

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.