Print empty query

Asked

Viewed 19 times

-1

Personal how do I display echo if the consultation has no results?

<?php
define('DB_HOST'        , "10.0.0.10");
define('DB_USER'        , "sa");
define('DB_PASSWORD'    , "@cm#db2018");
define('DB_NAME'        , "syspdv");
define('DB_DRIVER'      , "sqlsrv");


@$pesquisar = $_POST['busca'];

$codigo = str_pad($pesquisar,14, "0", STR_PAD_LEFT);

require_once "conexao.php";
 
try{
 
$Conexao    = Conexao::getConnection();
$query      = $Conexao->query("SELECT PRODUTO.PRODES, PRODUTO.PROPRC1 ,PRODUTO.PROPRC2,PRODUTO.PROPRC3 , CAST(ESTOQUE.ESTATU AS NUMERIC) AS ESTOQUE FROM PRODUTO inner join ESTOQUE ON PRODUTO.PROCOD = ESTOQUE.PROCOD LEFT JOIN PRODUTOAUX ON PRODUTOAUX.PROCOD=PRODUTO.PROCOD WHERE PRODUTOAUX.PROCODAUX = '$codigo' OR PRODUTO.PROCOD = '$codigo'

");
$produtos   = $query->fetchAll();
 
}catch(Exception $e){
 
echo $e->getMessage();
exit;
 
}?>
  • I would put some data via query directly just to have some data to work with and be able to display via echo or in html (to view the final result, when actual data)

  • search by rowCount

  • that code there works only that if it has not resulted I can not place type 'product not found''

1 answer

0


Use the line counter of your query result and store in a variable:

$count_row = $stmt->rowCount();

Then you can use the result of the variable that contains the amount of records of your select.

Example:

if($count_row==0)
{
   echo "Não tem registros para exibir";
} else
{
    // Exibe sua rotina aqui
}

You can learn more about the rowCount() in the official PHP documentation.

Pdostatement :: rowCount() returns the number of lines affected by last instruction DELETE, INSERT or UPDATE executed by the object Corresponding pdostatement.

  • I tried more also not the message

Browser other questions tagged

You are not signed in. Login or sign up in order to post.