0

Not all data is being saved in the array through mysqli_fetch_array() function when I try to echo each index. I want to store all table names inside of an array variable called $tablas So I'm trying to gather all the table names using the mysqli_fetch_array(). I have 4 tables stored in the datable so I was using a print_r() but it was only showing for the index of 0, then I tried with a var_dump() but did the same. So I wanted to find out if table names are actually being saved into the $table array so then I tried with using echo instead for each index in the array as shown below but only is working for index 0 others seem not to exist. What is causing this?

<?php
    $conexion = mysqli_connect("localhost","root","","capacitacion");
    $consulta = "SHOW TABLES FROM capacitacion";
    $resultado = mysqli_query($conexion, $consulta);

    if($resultado){
        $tablas = mysqli_fetch_array($resultado);
        echo $tablas[0];
        echo $tablas[1];
        echo $tablas[2];
    }
    else{
        echo 'error';
    }
?>
5
  • 4
    mysqli_fetch_array() only returns a single row at a time. Commented Feb 17, 2020 at 21:39
  • Take a look at fetch_all, that might do what you want to do. Commented Feb 17, 2020 at 21:48
  • I did it with the fetch_all and now the error show is Array to string conversion. how to I fix it? Commented Feb 17, 2020 at 22:04
  • use var_dump($tablas) to debug the structure of your array, you will see it is compose of many items (array themselves), so you probably have to loop over tablas if you want to handle this case. Commented Feb 17, 2020 at 22:21
  • Note: The object-oriented interface to mysqli is significantly less verbose, making code easier to read and audit, and is not easily confused with the obsolete mysql_query interface where missing a single i can cause trouble. Example: $db = new mysqli(…) and $db->prepare("…") The procedural interface is an artifact from the PHP 4 era and should not be used in new code. Commented Feb 17, 2020 at 22:24

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.