1

I am working on a project with php and Mysql where I need to get values from Mysql database and store them into array.

Code:

$con=new mysqli("localhost","root","","databse");
$sql="select name from data";
$result= array();
$result=$con->query($sql);

How to store the data in php array?

Vijayaragavendran
  • 726
  • 1
  • 10
  • 21

1 Answers1

3

Here is an example of how to do it!

 //MySQLi information

    $db_host     = "localhost";
    $db_username = "username";
    $db_password = "password";

    //connect to mysqli database (Host/Username/Password)
    $connection = mysqli_connect($db_host, $db_username, $db_password) or die("Error " . mysqli_error());

    //select MySQLi dabatase table
    $db = mysqli_select_db($connection, "table") or die("Error " . mysqli_error());

   $sql = mysqli_query($connection, "SELECT * FROM data");

   while($row = mysqli_fetch_array($sql)) {
   $names[] = $row['name'];
}

Edit: Print the results out by using

print_r($names); 

or

foreach($names as $name) {
echo "$name";
}

Good luck!

Mr Pro Pop
  • 666
  • 5
  • 19