How get all values in a column using PHP?

前端 未结 6 1260
悲&欢浪女
悲&欢浪女 2020-11-28 09:10

I\'ve been searching for this everywhere, but still can\'t find a solution: How do I get all the values from a mySQL column and store them in an array?

For eg: Tab

6条回答
  •  难免孤独
    2020-11-28 09:49

    I would use a mysqli connection to connect to the database. Here is an example:

    $connection = new mysqli("127.0.0.1", "username", "password", "database_name", 3306);
    

    The next step is to select the information. In your case I would do:

    $query = $connection->query("SELECT `names` FROM `Customers`;");
    

    And finally we make an array from all these names by typing:

    $array = Array();
    while($result = $query->fetch_assoc()){
        $array[] = $result['names'];
    }
    
    print_r($array);
    

    So what I've done in this code: I selected all names from the table using a mysql query. Next I use a while loop to check if the $query has a next value. If so the while loop continues and adds that value to the array '$array'. Else the loop stops. And finally I print the array using the 'print_r' method so you can see it all works. I hope this was helpful.

提交回复
热议问题