[英]fetch 2 columns from table in 2 array php mysql
I have a table with 6 columns 我有一张六列的桌子
Sort like: 排序方式:
Database -> kids 数据库->孩子
|CANDY | COLOR | DRINK | PET | SONG | TOY |
---------------------------------------------------
|cookie | blue | juice | dog | if | ball |
|cake | red | coke | cat | ask | doll |
I want to store 我要储存
Array
called Candy[];
所有的糖果都放在一个名为Candy[];
Array
Candy[];
color[];
color[];
上的所有颜色color[];
drink[];
所有饮料drink[];
etc.... 等等.... I managed to create the arrays with the columns names with a FORLOOP
with these lines, which works fine: 我设法用这些行的FORLOOP
创建了具有列名的数组,这很好用:
$fieldName = mysqli_fetch_field_direct($result, $i)->name; $ fieldName = mysqli_fetch_field_direct($ result,$ i)-> name; ${$fieldName} = array(); $ {$ fieldName} = array();
But when the code gets to the WHILE part, that is also inside the loop, to add the columns items inside the arrays[], it adds only the first element and returns to the FORLOOP, goes and adds one element to the next array[] and jumps to the next array[].... 但是,当代码到达WHILE部分(也位于循环内部)以在arrays []中添加列项目时,它仅添加第一个元素并返回到FORLOOP,然后将一个元素添加到下一个array [ ]并跳到下一个array [] ....
while($row = mysqli_fetch_array($result)){
//inserts itens in the "$array" with columns name
${$fieldName}[] = $row[$i];
}
If I try to "echo"
the arrays[]
and don´t put a "BREAK"
at the end of the WHILE
, It returns an error 如果我尝试"echo"
arrays[]
,而不在WHILE
的末尾添加"BREAK"
,则返回错误
"Undefined offset: 0 in line..." “未定义的偏移量:0行...”
And when I put the " BREAK
", it works for: 当我输入“ BREAK
”时,它适用于:
echo candy[0]; = cookie
but doesn't works for: 但不适用于:
echo candy[1]; = Undefined offset: 1 in line...
Here is the whole code: 这是完整的代码:
$sql="SELECT candy, color, drink, pet, song, toy FROM kids";
$result = mysqli_query($con,$sql);
$colNumber = mysqli_num_fields($result);
for($i=0;$i<=$colNumber -1;$i++){
$fieldName = mysqli_fetch_field_direct($result, $i)->name;
echo . $fieldName . "<br /><br />";
//Creates an Array with Coll Name from DB
//with dynamic-variable ${$fieldname}
${$fieldName} = array();
while($row = mysqli_fetch_array($result, MYSQLI_NUM)){
//inserts the received itens into the array ${$fieldName}
${$fieldName}[] = $row[$i];
printf ("%s (%s)\n", $row[$i], $row[1]);
}
echo ${$fieldName}[0];
echo candy[0];
echo ${$fieldName}[1];
echo candy[1];
echo "<hr />";
}
The WHILE code works when it´s not inside a FORLOOP
and if I make a query()
like: 当WHILE代码不在FORLOOP
并且如果我执行query()
FORLOOP
:
SELECT candy FROM kids.
But then, like that, I´d need like 600 lines of repeated code to get what I want and copy/paste again and again for each new coll on the DB table. 但是,那样,我需要大约600行重复的代码来获取所需的内容,并一次又一次地复制/粘贴数据库表中的每个新列。
Any ideas? 有任何想法吗?
I need it to put the arrays inside HTML <SELECT><OPTION></SELECT>
, then use mt_rand()
to shuffle and get different " profiles
". 我需要它将数组放入HTML <SELECT><OPTION></SELECT>
,然后使用mt_rand()
随机播放并获取不同的“ profiles
”。 This won´t be used with kid stuff, that was just an example. 这不会与儿童用品一起使用,这只是一个例子。 It will be a virtual crime creator that will shuffle the variables to create different crime hypothesis for law school students work on. 它将是一个虚拟的犯罪创造者,它将对变量进行混洗,以为法学院学生从事的工作创建不同的犯罪假设。
I already spent 3 days reading documentation on http://php.net/manual/pt_BR/mysqli-result.fetch-array.php 我已经花了三天时间阅读http://php.net/manual/pt_BR/mysqli-result.fetch-array.php上的文档
and "googling" it but couldn't find any answer. 并“搜索”它,但找不到任何答案。
Found a way to do it. 找到了一种方法。 I translated the code to English so others can have it. 我将代码翻译成英文,以便其他人可以使用。 If there is any variable name wrong, fix it using the comments and enjoy! 如果变量名有误,请使用注释进行修复并享受!
$sql = "SELECT parquet, defendant, action, modusOperandi, victim, crime FROM crime_elements";
//runs '$sql' query to get database data
$result = mysqli_query($con,$sql);
$collNumber = mysqli_num_fields($result);
echo "ColL Number :" . $collNumber;
// Creates $data Array[]
$data = array();
while($row = mysqli_fetch_array($result)){
//inserts received data into "$data" and makes available by array[]index
$data[] = $row;
}
for($i=0;$i<=$collNumber -1;$i++){
// Gets the column name from the query()
$fieldName = mysqli_fetch_field_direct($result, $i)->name;
echo "</br><b>Coll Name:</b> " . $fieldName . "<br /><br />";
${$fieldName} = array_column($data, $i);
print_r(${$fieldName});
echo "<br /><b>Specified item choice[]:</b> ". ${$fieldName}[2];
echo "<hr />";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.