Array foreach and MYSQL query with php

0

I have this example of foreach with array, and what I need to know is how can I enter mysql query to this array?

  <?php 
    $cabecera=array("nombre","telefono","referencia");
    $luis=array("Luis García","645395715","Trabajo");
    $paco=array("Paco Salvatierra","625781225","Amigo");
    $sofia=array("Sofía López","664887221","cliente");
    $pilar=array("Pilar Martinez","674458115","familia");
    $agenda=array($cabecera,$luis,$paco,$sofia,$pilar);
    ?>

First write the secondary arrays, which will form the rows of the table, and then insert them as elements of the main array. Then we show them nesting two foreach loops, using the necessary html code, we show them in a table:

    <table border="1" width="60%" cellspacing="0">
    <?php  
    foreach ($agenda as $fila){
            echo "<tr>";
            foreach ($fila as $celda){
                    echo "<td> $celda </td>";
                    }
            echo "</tr>";
            }
    ?>

Introducing the query:

include("conexiones.php");
//la variable $link esta en conexiones es la conexion a la base de datos



$cabecera = array($link,'SELECT fecha FROM clase'); 

$datos=array($link, 'select * from alumno '); 

$agenda=array($cabecera,$datos);
?>
<table border="1" width="60%" cellspacing="0">
<?php  
foreach ($agenda as $fila){
        echo "<tr>";
        foreach ($fila as $celda){
                echo "<td> ".$celda." </td>";
                }
        echo "</tr>";
        }

I get an error:

  

Catchable fatal error: Object of class mysqli could not be converted   to string in C: \ xampp \ htdocs \ notebooks \ assas_areas.php on line   178

    
asked by Milton W. Orellana 23.02.2017 в 02:18
source

1 answer

1

Assuming that your connection, which you will have called $link in the conexiones.php file you are including, works fine . You can execute the following procedures:

A. See the table To send a query to the database you must use the method query , of the object $link of mysqli , for example:

/*
 * Viendo más de cerca tu código, creo que no es necesario lo de $cabeceras
 * $cabecera = $link->query("SELECT fecha FROM clase"); 
*/

$datos = $link->query($link, "select nombre, telefono, referencia from alumno"); 

B. Get results To obtain the results of this query, there are several ways to do it, one is invoking the method fetch_assoc() that allows you to get a result row as an associative array.

    $filas_cabecera = $cabecera->fetch_assoc();
    $alumnos = $datos->fetch_assoc();
/*
 * Si haces print_r($alumnos) obtendrás algo parecido a esto, si tu consulta funciona bien:
*Array
*(
*    [0] => Array
*        (
*            [nombre] => Juan
*            [telefono] => 7874736
*            [referencia] => Trabajo
*        ),
*    [1] => Array
*        (
*            [nombre] => Pedro
*            [telefono] => 87759403
*            [referencia] => Amigo
*        )
*)
*/

C. Read and print results:

    $strHTML= "<table>";
            $strHTML.= "<tr>";

//Tus encabezados
            $strHTML.= "<th>Nombre</th><th>Teléfono</th><th>Referencia</th>";
            $strHTML.= "</tr>";
//Tus datos
            $strHTML.= "<tr>";
//Verás que al usar $celda deberás poner entre [] el nombre de la columna de tu tabla, como ha sido devuelto en el array asociativo de más arriba

        foreach ($alumnos as $celda){
                $strHTML.= "<td> $celda['nombre']</td>";
                $strHTML.= "<td> $celda['telefono']</td>";
                $strHTML.= "<td> $celda['referencia']</td>";
                }
            $strHTML.= "</tr>";
        }
    $strHTML.= "</table>";
    //Imprimimos la variable que hemos ido concatenando
    echo $strHTML;


    /* liberar resultados después de leerlos*/
        $datos->free();
    /* cerrar la conexión */
        $link->close();

More info
answered by 23.02.2017 в 03:04