SQL result in PHP as String

0

I'm trying to put the result of an SQL query in a string, concatenating the results.

 $poblaciones = $wpdb->get_results('SELECT poblacion FROM wp_custom_poblacion
    WHERE postal = (SELECT cod_postal FROM WPCalidad.wp_custom_proveedores ORDER BY id DESC LIMIT 1);');

This will return a list with several cities, which I want to put in a string. I know it must be something simple, but I do not get it. Putting it this way it works:

$municipio=$poblaciones[0]->poblacion . $poblaciones[1]->poblacion ;

and with a for tb, but only if I put $ size = 5, for example.

for ($i=0;$i<$tamano;$i++){
$municipio=  $municipio .  $poblaciones[$i]->poblacion. "; " ;

}

How can I get the size of the array in $ size? I've tried count, mysql_num_rows ... but something I'll be doing wrong that does not work.

Thank you!

while($elem = $wpdb->get_results('SELECT poblacion FROM wp_custom_poblacion WHERE postal = (SELECT cod_postal FROM WPCalidad.wp_custom_proveedores ORDER BY id DESC LIMIT 1);')) {

  $municipio = $municipio. $elem . "; ";
}

That's not how it works for me.

Nothing, that there is no way. I've tried a thousand things and either fill in the blank string, or it puts me Array Array Array ...... So I have it now.

    global $wpdb;


    // Para proveedores
    $datosproveedor = $wpdb->get_results('SELECT  wp_custom_proveedores.id,
wp_custom_poblacion.provincia 
FROM wp_custom_poblacion
INNER JOIN wp_custom_proveedores on wp_custom_proveedores.cod_postal=wp_custom_poblacion.postal
WHERE wp_custom_poblacion.pais=(SELECT pais FROM WPCalidad.wp_custom_proveedores ORDER BY id DESC LIMIT 1) COLLATE utf8_unicode_ci
ORDER BY id DESC LIMIT 1;');

    $poblacionesproveedor = $wpdb->get_results('SELECT poblacion FROM wp_custom_poblacion
        WHERE postal = (SELECT cod_postal FROM WPCalidad.wp_custom_proveedores ORDER BY id DESC LIMIT 1);');

    $tamanoproveedor = 5;
   /* for ($i = 0; $i < $tamanoproveedor; $i++) {

        $municipioproveedor = $municipioproveedor . $poblacionesproveedor[$i]->poblacion . "; ";

    }*/

    foreach ($poblacionesproveedor as $poblacion){
  $municipioproveedor = $municipioproveedor . $poblacionesproveedor->$poblacion;
}

If all I want is that $ municipalityprovider contains the list of stocks ...: (

    
asked by orodruim 12.03.2018 в 11:27
source

1 answer

0

Try using a foreach loop

foreach ($poblaciones as $poblacion){
  print_r($poblaciones);
}

If you need to access the array index you can use $index -> $poblacion

    
answered by 12.03.2018 в 12:12