-1

i've got this code and i would like to get it as an INT to be able to have it used as a counter to set the new id to introduce new data at 'plats' table. Anyone could help me please?

<?php
  include("conection.php");
  $consulta="SELECT count(id_plat) FROM `plats`";//consulta no plats actuals
  $resultat=mysqli_query($connect,$consulta);
  $row = mysql_fetch_assoc($resultat);
  echo $row;
  mysqli_close($connect);
 ?>
  • `var_dump($row)` to see what exactly is returned in `$row`, probably easiest way to see what exaclty should you echo. Also forget about MYSQL functions. They are obsolite and removed since php 7.0. Use [MYSQLI](https://www.php.net/manual/en/book.mysqli.php) functions instead – Eugene Anisiutkin Mar 16 '20 at 08:39
  • Try `mysqli_fetch_assoc()` (with an `i`) – Nigel Ren Mar 16 '20 at 08:53

1 Answers1

0
<?php
  include("conection.php");
  $consulta="SELECT count(id_plat) FROM `plats`";//consulta no plats actuals
  $resultat=mysqli_query($connect,$consulta);
  $row = mysqli_fetch_row($resultat);
  $mycounter = (int)$row[0];
 ?>

Fetch the results with number-based index and assign the 0th index to a variable.

MiK
  • 918
  • 4
  • 16
  • Hello MiK! Thanks for your answer. Unfortunatelly returns me this error: Fatal error: Uncaught Error: Call to undefined function mysql_fetch_row() in "directory\count.php:5" Stack trace: #0 {main} thrown in directory\count.php on line 5 – Jordi Ponts Mar 16 '20 at 08:36
  • 1
    That's because you didn't copy my code, you have a bug in your original code with a missing i, it's mysqli_fetch, not mysql_fetch – MiK Mar 16 '20 at 09:01
  • Just checked it, i'm sorry – Jordi Ponts Mar 16 '20 at 09:19