Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
516 views
in Technique[技术] by (71.8m points)

How to create and fill php array from Mysql database

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..|.......|

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 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

RTM

Example #1 Fetch all remaining rows in a result set

<?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:
");
$result = $sth->fetchAll();
print_r($result);
?> 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...