0

I'm trying to fill one array with the data obtained by one sql select query execution but I can't do it, if someone please could help me.

I'm still very new to programming on php so sorry for that, probably this is really bad coding but I don't know how to fill the array with the foreach loop.

The data returned by the sql database server is:

1 La Defense - Chateau de Vincennes
2 Porte Dauphine - Nation
3 Pont de Levallois Becon - Gallieni
3bis Gambetta - Porte des Lilas
4 Porte de Clignancourt - Mairie de Montrouge
5 Bobigny Pablo Picasso - Place d’Italie
6 Charles de Gaulle Etoile - Nation
7 Louis Leblanc - Pre Saint Gervais
8 Balard - Pointe du Lac
9 Pont de Sèvres - Mairie de Montreuil
10 Boulogne Pont de Saint Cloud - Gare d’Austerlit...
11 Chatelet - Mairie des Lilas
12 Front Populaire - Mairie d’Issy
13 Gare Saint Lazare - Olympiades

My php function is:

function select($tipoTte) {
       $connection = connectDB();

       $sql = mysqli_prepare($connection, "SELECT DISTINCT linea FROM lugar_transporte WHERE tipoTte = ?");
       mysqli_stmt_bind_param($sql, "s", $tipoTte);

       $query = $sql->execute();

       if(!$query)
          die();

       $result = $sql->store_result();

       $realresult = $sql->bind_result($linea);

       $rawdata = array();

       $sql->fetch();

       for($i = 0; $i < 14; $i++) {
          $rawdata[$i] = utf8_encode($linea);
       }

       disconnectDB($connection);
       return $rawdata;
}

How can I fill the rawdata array with all the rows from the query? I mean I want to rawdata have the data like this:

rawdata["Linea 1 La Défense - Chateau de Vincennes", "Linea 2 Porte Dauphine - Nation ", ...] 

EDIT: OK, with the new version of the code I got:

rawdata["Linea 1 La Défense - Chateau de Vincennes", "Linea 1 La Défense - Chateau de Vincennes", "Linea 1 La Défense - Chateau de Vincennes", ...]

How can I do to iterate throw the rows of the query result? I don't know if I'm explaining myself correctly...

5
  • Try .... $rawdata['linea'][] = $valor; Commented Apr 12, 2018 at 14:38
  • $rawdata[] = 'Linea ' . $valor; Commented Apr 12, 2018 at 14:39
  • rawdata['linea'] = $valor; this is a syntax error missing $, also you may wish to use $rawdata['linea'][] = $valor; as others note Commented Apr 12, 2018 at 14:41
  • Thanks for your comments, now I have the array filled but with the same value always, (like this): rawdata["Linea 1 La Défense - Chateau de Vincennes", "Linea 1 La Défense - Chateau de Vincennes", "Linea 1 La Défense - Chateau de Vincennes", ...] how can I change the value in every iteration? Commented Apr 12, 2018 at 15:40
  • Despite my answer, try: var_dump( $realresult); and also: var_dump( $result ); Commented Apr 12, 2018 at 16:22

2 Answers 2

1

Check this edit, fixed it and tested it. It´s working with php7 and mysql 5.6.

<?php

$mysqli = new mysqli("localhost", "username", "password", "dbname");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}
$linea = "0";
$query = "SELECT DISTINCT linea FROM lugar_transporte WHERE tipoTte = ?";
$stmt1 = mysqli_prepare($mysqli, $query);
mysqli_stmt_bind_param($stmt1, "s", $linea);
$result = $mysqli->query($query);
var_dump($result);
while($row = $result->fetch_array())    {
    $rows[] = $row;
}
$rawdata = array();
$i = 0;
foreach($rows as $row)    {
    $rawdata[$i] = utf8_encode($row[$i]);
    echo $rawdata["$i"];
    $i++;
}

/* free result set */
$result->close();

/* close connection */
$mysqli->close();
?>
Sign up to request clarification or add additional context in comments.

6 Comments

This don't work because of "tipoTte = $tipoTte" in the SELECT query, if I put 'metro' instead of $tipoTte works, but only show the first row "1 La Defense - Chateau de Vicennes"
I've tried with mysqli_stmt_bind_param() function before and it didn't work, if I try var_dump($result) it show me bool(false) so it fails I don't know why man. EDIT: I've tried var_dump($rows[i$]); and it showed me what I want, it's close man, I'm gonna try with echo
I just fixed it and tested it, try again please. It works on my server. You can place it in your function and replace $linea by your method variable ($tipoTte).
It works, but if I use the mysqli_stmt_bind_param() function I cant use the fetch_array() function, it doesn't work, I suppose I have to use mysqli_fetch_array() but I'm trying to know how...
I don't know why but if I use mysqli_stmt_bind_param() then fetch_array() didn't work propertly...I think I'd could be because of my php version, I don't know. Finally I get what I want I write the solution here a couple days ago, thank you for your help man!
|
1

Ok, so finally I do it, I have to work with incompatibilities between php versions and methods but here is the final version, I got the array as I wanted, thank you so much for all of you who helped me, specially Pedro Branco, if I could hug you I'd do it man.

So here's the final version if someone needs it or have the same problem:

function select($tipoTte) {
            $connection = connectDB();

            var_dump($result);*/

            $stmt = $connection->prepare("SELECT DISTINCT linea FROM lugar_transporte WHERE tipoTte = ?");
            $stmt->bind_param('s', $tipoTte);

            $stmt->execute();

            $result = $stmt->get_result();

            while($row = $result->fetch_assoc())    {
                    $rows[] = $row;
                    }
            $rawdata = array();
            $i = 0;

                    foreach($rows as $row)    {
                        $rawdata[$i] = $rows[$i];
                        $i++;
                }
                    var_dump($rawdata);

            $result->close();

                disconnectDB($connection);
                return $rawdata;
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.