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?

0

1 Answer 1

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!

Sign up to request clarification or add additional context in comments.

9 Comments

It showed me an error: Undefined variable: row in C:\xampp\htdocs\ajax_admin_image\ar.php on line 10
Make sure that you have the database information correct. Anyways, from my answer, I edited | select * from date | to | select * from data | This might be why! Make it "data"
I already changed that field. From date to data.
I succesfully sorted it. Switching $result and in the place of $row solves that problem..
Updated my code, copy everything back and change the MySQL information! Fixed the problem! Yup figured it 2 mins ago!
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.