Extract exploded results from a column

Asked

Viewed 262 times

1

I entered several data inside the same column with the implode, now I want to extract the results of the query with the explodes so as to keep the data separate so that later I can work them.

I have the code this way, but it’s not returning anything in the arrays:

$conn = new mysqli($servername, $username, $password, $dbname);
$conn->set_charset('utf8');

$sql = "SELECT arrachar FROM centrodb.marcacaoInfancia";

$result = mysqli_query($conn, $sql);

    $result1 = explode(' ', $result);

    echo $result1[0];
    echo $result1[1];
    echo $result1[2];
    echo $result1[3];
    echo $result1[4];
    echo $result1[5];
    echo $result1[6];
    echo $result1[7];
    echo $result1[8];
    echo $result1[9];
    echo $result1[10];
    echo $result1[11];

2 answers

3


You have to "treat" that $result to determine how you want to handle the data.

A widely used method is the fetch_assoc() which returns an associative array of the table.

So let’s make a loop with the fetch_assoc() and read each row of the table:

while ($linha= mysqli_fetch_assoc($result)) {
    $result1 = explode(' ', $linha["arrachar"]);
    var_dump($result1);
}

If you only have a line or query with limit 1 can ignore the while.

$linha= mysqli_fetch_assoc($result);
$result1 = explode(' ', $linha["arrachar");
var_dump($result1);

Another example using the fetch_array (array common, numeric)

$linha= mysqli_fetch_array($result, MYSQLI_NUM);
$result1 = explode(' ', $linha[0]);
var_dump($result1);
  • As exemplified returns all the statements that exist in the table, but always followed, example:array(1) { [0]=> string(92) "2018-04-26,Peq_Almoço,14,Almoço,12,Almoço_(Dieta),2,Lanche,14,Jantar,10,Jantar_(Dieta),10" } array(1) { [0]=> string(91) "2018-04-27,Peq_Almoço,15,Almoço,12,Almoço_(Dieta),3,Lanche,15,Jantar,12,Jantar_(Dieta),2" }. I wanted to have this result separate, to be able to mount a table for the end user to consult

  • 1

    You have to make it explode with a comma

0

Try something like that:

$conn = new mysqli($servername, $username, $password, $dbname);
$conn->set_charset('utf8');

$sql = "SELECT arrachar FROM centrodb.marcacaoInfancia";

$result = mysqli_query($conn, $sql);
$data = '';

while($row = $result->fetch_assoc()) {
    $data .= $row["arrachar"].',';
}
$result1 = explode(',', $data);

echo $result1[0];
echo $result1[1];
echo $result1[2];
echo $result1[3];
echo $result1[4];
echo $result1[5];
echo $result1[6];
echo $result1[7];
echo $result1[8];
echo $result1[9];
echo $result1[10];
echo $result1[11];
  • Thus only returns the first row of the table

  • I did the following: the variable $data is concatenating all the results of the query and added another separator that in the case was the comma, so that later could be used in the explode. Try printing the result inside while to see if it returns something different.

  • I didn’t vote no. Inside the while shows the times that there is echo inside the while, but always the first row of the repeated table

Browser other questions tagged

You are not signed in. Login or sign up in order to post.