简体   繁体   中英

php mysql - search a row and concatenate values of every columns to variable string

Hello friends need some help I am trying to get the following result:

$Steps= "1010"; TEXT

the above format from a mysql table

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

can anyone help me with this I find nothing in internet

You can take column names from INFORMATION_SCHEMA and then loop them to concatenate each value column for the final string.

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 />';
}

You can use PHP implode :

implode — Join array elements with a string

So, just get every element of the result

SELECT * FROM table WHERE id = '$id'

Remove the ID item and implode the rest:

while($row = mysqli_fetch_array($datos)){
    unset($row["id"]);
    $result = implode($row);
    //do whatever with result
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM