php mysql - 搜索一行并将每列的值连接到变量字符串
php mysql - search a row and concatenate values of every columns to variable string
你好朋友需要一些帮助我正在尝试获得以下结果:
$Steps= "1010"; TEXT
以上格式来自mysqltable
id | step1| step2| step3| step4|
0001 | 1 | 0 | 1 | 0 |
0002 | 0 | 0 | 0 | 0 |
php:
$Nperm = 0;
while($row = mysqli_fetch_array($datos)){
$result .= "$row[$Nperm]";
$Nperm++;
}
return $result
谁能帮我解决这个问题我在网上找不到任何东西
您可以从 INFORMATION_SCHEMA
中获取列名,然后循环它们以连接每个值列以获得最终字符串。
define('DB_HOST', 'localhost');
define('DB_NAME', 'your_database');
define('DB_USER', 'your_user');
define('DB_PASSWORD', 'your_password');
$link = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$sql = "
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'table_name' AND table_schema = 'your_db' AND COLUMN_NAME != 'id'
ORDER BY ORDINAL_POSITION";
$result = mysqli_query($link, $sql);
$columns = array();
$temp = array();
foreach($result as $row) { //Take all column names
$columns[] = $row['COLUMN_NAME'];
}
while($row = mysqli_fetch_array($datos)) {
$s = '';
for($i = 0; $i < count($columns); $i++) {
$s .= $row[$columns[$i]];
}
$temp[] = $s; //Put all strings in an array
echo $s.'<br />';
}
您可以使用 PHP implode:
implode — Join array elements with a string
所以,只获取结果的每个元素
SELECT * FROM table WHERE id = '$id'
删除 ID 项,implode
其余项:
while($row = mysqli_fetch_array($datos)){
unset($row["id"]);
$result = implode($row);
//do whatever with result
}
你好朋友需要一些帮助我正在尝试获得以下结果:
$Steps= "1010"; TEXT
以上格式来自mysqltable
id | step1| step2| step3| step4|
0001 | 1 | 0 | 1 | 0 |
0002 | 0 | 0 | 0 | 0 |
php:
$Nperm = 0;
while($row = mysqli_fetch_array($datos)){
$result .= "$row[$Nperm]";
$Nperm++;
}
return $result
谁能帮我解决这个问题我在网上找不到任何东西
您可以从 INFORMATION_SCHEMA
中获取列名,然后循环它们以连接每个值列以获得最终字符串。
define('DB_HOST', 'localhost');
define('DB_NAME', 'your_database');
define('DB_USER', 'your_user');
define('DB_PASSWORD', 'your_password');
$link = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$sql = "
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'table_name' AND table_schema = 'your_db' AND COLUMN_NAME != 'id'
ORDER BY ORDINAL_POSITION";
$result = mysqli_query($link, $sql);
$columns = array();
$temp = array();
foreach($result as $row) { //Take all column names
$columns[] = $row['COLUMN_NAME'];
}
while($row = mysqli_fetch_array($datos)) {
$s = '';
for($i = 0; $i < count($columns); $i++) {
$s .= $row[$columns[$i]];
}
$temp[] = $s; //Put all strings in an array
echo $s.'<br />';
}
您可以使用 PHP implode:
implode — Join array elements with a string
所以,只获取结果的每个元素
SELECT * FROM table WHERE id = '$id'
删除 ID 项,implode
其余项:
while($row = mysqli_fetch_array($datos)){
unset($row["id"]);
$result = implode($row);
//do whatever with result
}