0

Im new to web development. I need the array like following

["Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"]

I tried. But I failed.I connected the database. How can I make it?

my array-code

$count=0;
$sql="SELECT name FROM planet";
$sql_run=mysqli_query($con,$sql); 

while($row = mysqli_fetch_assoc($sql_run))
{
    $result[] = "'".implode("\'",$row)."'".",";
    echo $result[$count++];
}

By using above code, i couldn't get the result what I expected.

4
  • What result did you get? Commented Jan 9, 2016 at 19:25
  • I don't at all understand why you write "'".implode("\'",$row)."'".",". Anyway, to get what you want, you must do $result[] = '"' . $row['name'] . '"' inside of the while(). Commented Jan 9, 2016 at 19:28
  • I got like this, all are repeated. 'Mercury\'Mercury','Venus\'Venus','Earth\'Earth','Mars\'Mars','Jupiter\'Jupiter','Saturn\'Saturn','Uranus\'Uranus','Neptune\'Neptune', but I need like this 'Mercury', 'Venus', 'Earth','Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune' Commented Jan 9, 2016 at 19:30
  • thank you so much. its working. Really thanks. Problem is Im new to php@cFreed Commented Jan 9, 2016 at 19:43

1 Answer 1

1

Each fetch returns one planet in associative array. To get array of rows, you have to do:

$list = array();
while($row = mysqli_fetch_assoc($sql_run)) {
     array_push($list, $row['name']);
}
var_dump($list);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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