I can find lots of tutorials showing you how to load an array into a database field but can't seem to figure out how to pull each entry in a field into an array as seperate items. Seems simple enough just can't get it to work, any help?
我可以找到很多教程,向您展示如何將數組加載到數據庫字段中,但似乎無法弄清楚如何將字段中的每個條目作為單獨的項目拉入數組。看起來很簡單,只是無法讓它工作,任何幫助?
5 个解决方案
#1
If using the modern PDO library, use the PDOStatement->fetchAll() function with the fetch_style parameter set to PDO::FETCH_COLUMN.
如果使用現代PDO庫,請使用PDOStatement-> fetchAll()函數,並將fetch_style參數設置為PDO :: FETCH_COLUMN。
Based on a sample from that page:
基於該頁面的示例:
$sth = $dbh->prepare("SELECT field FROM dbtable");
$sth->execute();
$array = $sth->fetchAll(PDO::FETCH_COLUMN);
If using the old MySQL API (not recommended, example omits error checking)
如果使用舊的MySQL API(不推薦,示例省略錯誤檢查)
$array = array();
$result = mysql_query("SELECT field FROM dbtable");
while ($row = mysql_fetch_row($result)) {
$array[] = $row[0];
}
mysql_free_result($result);
#2
$big_2_dimensional_array_of_data;
foreach ($big_array_of_data as $row) {
$query = "INSERT INTO table_name (field1, field2, ...) VALUES($row[0], $row[1], ...)
mysql_query($query);
}
Something like that I think
我想是這樣的
#3
after reading his question a few times, i guess what he wants to do is something like this:
在讀了幾次問題后,我想他想做的是這樣的:
$query = "SELECT field1, field2, ... fieldn FROM table;";
$r = mysql_query($query,$conn);
$row = mysql_fetch_assoc($r);
i'm still not quite sure what it is he exactly wants...
我還不太清楚他究竟想要的是什么......
#4
My interpretation of this question is that the questioner has inserted a number of rows into a table, and isn't sure how to handle getting them out other than one at a time. (It's possible that the question might also be referring to data serialized and then stuck into a single field... but I hope not!)
我對這個問題的解釋是,提問者已經在表格中插入了許多行,並且不確定如何處理它們而不是一次一個。 (問題可能也可能是指序列化的數據,然后插入單個字段......但我希望不會!)
So, here's how to get multiple rows:
那么,這是如何獲得多行:
$query = "SELECT field1, field2, ... fieldn FROM table;";
$r = mysql_query($query,$conn);
$data = array();
while($row = mysql_fetch_assoc($r)) {
$data[] = $row;
}
You'd now have all the rows returned by your query in $data, so something like this would work to access it: $data[2]['field1']
你現在已經在$ data中查詢了你的查詢返回的所有行,所以這樣的東西可以訪問它:$ data [2] ['field1']
#5
The examples below assume your SELECT statement is stored in $select and your connection is stored in $db. A two-dimensional array of the results is stored in $rows afterward.
下面的示例假設您的SELECT語句存儲在$ select中,並且您的連接存儲在$ db中。結果的二維數組隨后存儲在$ rows中。
If you're using mysql (for mysqli procedures, just replace mysql_ with mysqli_):
如果你正在使用mysql(對於mysqli程序,只需用mysqli_替換mysql_):
$result = mysql_query($select, $db);
$rows = [];
while ($row = mysql_fetch_assoc($result) {
$rows[] = $row;
}
Using mysqli classes:
使用mysqli類:
$result = $db->query($select);
$rows = [];
while ($row = $result->fetch_assoc()) {
$rows[] = $row;
}
$result->close()
Using PDO:
$stmt = $db->query($select);
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);