如何从Mysql数据库创建和填充php数组 [英] How to create and fill php array from Mysql database
本文介绍了如何从Mysql数据库创建和填充php数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有 MYSQL 表:
I have MYSQL table:
|column1|column2|column3|
|id1 |human1 |data |
|id2 |human1 |wejkls |
|id3 |human2 |sklkls |
|id4 |human1 |sdasds |
|id5 |human2 |l;lkls |
|id6 |human3 |kkklkl |
|.......|.......|.......|
|idN |human..|.......|
所以,我需要创建 php 数组:
So, I need to create php array:
Array {
[1] => human1
[2] => human2
[3] => human3
[N] => humanN
}
如何获取人名并填充数组?
And how can I get names of persons and fill them array?
添加:现在我有了这个代码:
Added: Now I have this code:
$connection = new mysqli("localhost", "admin", "password", "db");
$query = "SELECT DISTINCT `teacher` FROM school_year ";
$result = mysqli_query($connection, $query);
while ($a[] = mysqli_fetch_array($result, MYSQL_NUM)) {}
print_r($a);
print_r 结果:
print_r result:
Array ( [0] => Array ( [0] => Ahmed A.A.) [1] => Array ( [0] => Scott P.P. ) [2] => Array ( [0] => ....)...
如何获取名称作为数组中的值?我不需要打印名称,我需要它们用于其他功能.
How to get names as values in array? I don't need to print names, I need them for other function.
推荐答案
示例 #1 获取结果集中所有剩余的行
<?php
$sth = $dbh->prepare("SELECT name, colour FROM fruit");
$sth->execute();
/* Fetch all of the remaining rows in the result set */
print("Fetch all of the remaining rows in the result set:\n");
$result = $sth->fetchAll();
print_r($result);
?>
这篇关于如何从Mysql数据库创建和填充php数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文