Error in php query

1

I have the following code that should print me the amount of products registered in a table

  <?php
    require_once "js/conexion.php";
    $sql = 'select count(idp) as total from producto';
    $result = $mysqli->query($sql);
    if(!$result){
        die($mysqli->error);
    }else {
      $out = mysqli_fetch_array($result);
      echo $out['total'];
    }
    $mysqli->close();
   ?>

At the moment of loading the page, you send me the following message

  

Commands out of sync; you can not run this command now

Try to run it in this other way

  <?php
  require_once "js/conexion.php";
  $sql = 'select count(idp) as total from producto';
  $result = $mysqli->query($sql);
  if(!$result){
    echo "Error ";
    echo mysql_error();
    die;
  }else {
    $out = mysqli_fetch_array($result);
    echo $out['total'];
  }
  $mysqli->close();
   ?>

But send me this error

  

Fatal error: Uncaught Error: Call to undefined function mysql_error ()   in C: \ xampp \ htdocs \ point-sale \ Admin.php: 237 Stack trace: # 0 {main}   thrown in C: \ xampp \ htdocs \ point-sale \ Admin.php on line 237

    
asked by Hugo Costilla 01.11.2018 в 06:42
source

2 answers

1

I think it is not necessary to put conditions on your query and I would use the: Foreach $ out as $ row  echo $ row ['idp'];

    
answered by 01.11.2018 в 07:05
1

(PHP) The first code you are using is possibly failing because you are using mysqli_fetch_array ($ result) and in the next line you are calling it as an associative array echo $ out ['total']; . Try calling it by a numeric index.

THAT IS, IT SHOULD BE LIKE THAT ...

    $out = mysqli_fetch_array($result);
     echo $out[0];

Here is the MYSQLI_FETCH_ARRAY documentation

    
answered by 02.11.2018 в 20:40