hi, i need to put the result of my query into an array, then return it. here it is my code:

function getData($country) {

include("vars.php");
$conn=mysqli_connect($host, $user, $password) or die("No se pudo establecer comunicacion");
$db=mysqli_select_db($conn, $basedatos) or die("Error en la base de datos");

$xcountry=$country;

switch ($country) {
case "01":
$sql1="SELECT nommun from $tabla9 where coddep='$xcountry'";
$rs2=mysqli_query($conn, $sql1) or die(mysqli_error($conn));
$filas2=mysqli_num_rows($rs2);		
$campos=mysqli_num_fields($rs2);
$nomcampos=mysql_field_name($rs2);

settype($retval,"array");

    for($i=0;$i<$filas2;$i++){
            for($j=0;$j<$campos;$j++){
                    $retval[$i]$nomcampos[$j] = mysqli_result($result,$i,$nomcampos,$j);
            }//end inner loop
    }//end outer loop
//return $retval;

so, in the first approach of this code, was working using this simple line:

return array('','AH01','AH02','AH03','AH04','AH05','AH06','AH07');

the result was send it ok, obvioulsy the goal is to get the data from a table, put it in the array and then send this array, i think this line in particular is the problem:

      $retval[$i]$nomcampos[$j] = mysqli_result($result,$i,$nomcampos,$j);

but at this time, I don't get more ideas, so if you have suggestions will be welcome

regards

    so guys almost there!, I had this new point of view:

    function doIt($country) {
    
    include("vars.php");
    $conn=mysqli_connect($host, $user, $password) or die("No se pudo establecer comunicacion");
    $db=mysqli_select_db($conn, $basedatos) or die("Error en la base de datos");
    
    $xcountry=$country;
    
    switch ($country) {
    case "01":
    $sql1="SELECT nommun from $tabla9 where coddep='$xcountry'";
    $rs2=mysqli_query($conn, $sql1) or die(mysqli_error($conn));
    $filas2=mysqli_num_rows($rs2);		
    
    settype($retval,"array");
    
        for($i=0;$i<$filas2;$i++){
    		while($xrsa = mysqli_fetch_assoc($rs2)){
    			$retval[$i]=$xrsa['nommun'];
    			}
        }
    
    
    return $retval;
    
    //return array('','AH01','AH02','AH03','AH04','AH05','AH06','AH07');
    break;
    
    

    so, at this time i got the first record -turin- but there are more, my new question is:

    what should I tweak in my code to get all the records?

    regards

      Get rid of the for loop, that the while is inside of, then get rid of the $i in the brackets so they are empty...

                  while($xrsa = mysqli_fetch_assoc($rs2)){ 
                      $retval[]=$xrsa['nommun']; 
                      } 

        First problem I see is here:

                for($i=0;$i<$filas2;$i++){ 
                    while($xrsa = mysqli_fetch_assoc($rs2)){ 
                        $retval[$i]=$xrsa['nommun']; 
                        } 
                } 

        That inner while() loop is going to loop through all records in the result set during the first iteration of the for() loop. In other words, $i is going to be 0 until that while() loop consumes all records in the result set leaving none for the rest of the iterations of the for() loop.

        You need to get rid of one of those loops, presumably the for() loop since it doesn't matter how many rows MySQL claims were in the result set - you only want to continue looping as long as you're able to keep retrieving rows.

        EDIT: Woops, looks like I took too long to reply.

          thanks a lot guys, it works, this is the result of the function - of course it works-:

          function doIt($country) {
          
          include("vars.php");
          $conn=mysqli_connect($host, $user, $password) or die("No se pudo establecer comunicacion");
          $db=mysqli_select_db($conn, $basedatos) or die("Error en la base de datos");
          
          $xcountry=$country;
          
          switch ($country) {
          case "01":
          $sql1="SELECT nommun from $tabla9 where coddep='$xcountry'";
          $rs2=mysqli_query($conn, $sql1) or die(mysqli_error($conn));
          $filas2=mysqli_num_rows($rs2);		
          
          settype($retval,"array");
          
          		while($xrsa = mysqli_fetch_assoc($rs2)){
          			$retval[]=$xrsa['nommun'];
          			}
          
          
          return $retval;
          
          break;
          

          best regards
          God bless you

            Write a Reply...