如何从MySQL获取和组合数据,然后在PHP中插入数组

时间:2023-01-28 19:00:14

This question is quite specific for my needs, hence I can't find the best way to do this.

这个问题对我的需求非常具体,因此我找不到最好的方法来做到这一点。

What I would like to do is fetch name and surname from the table people and combine both into an array to end up with such results:

我想要做的是从表人中获取名称和姓氏,并将它们组合成一个数组,最终得到这样的结果:

"Bob Jones","Tony Wright",.. etc.

“鲍勃琼斯”,“托尼赖特”,......等

I'm using PDO for this. Here is what I have:

我正在使用PDO。这是我有的:

$attrs = array(PDO::ATTR_PERSISTENT => true);

// connect to PDO
$pdo = new PDO("mysql:host=localhost;dbname=new", "root", "root", $attrs);

// the following tells PDO we want it to throw Exceptions for every error.
// this is far more useful than the default mode of throwing php errors
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$conn = $pdo->prepare("SELECT name, surname FROM people");

$conn->execute();
$results = $conn->fetchAll();

foreach($results as $row){

    $fullName = $row['name'] . "," . $row['surname'];

    print_r($fullName);
} 

I have tried a few things, I'm just stuck with this code at minute. Any help, or suggestions is much appreciated.

我尝试了一些事情,我只是在一分钟内坚持使用这段代码。任何帮助或建议都非常感谢。

4 个解决方案

#1


1  

$arr = array();
foreach($results as $row) {
   $arr[] = "{$row['name']},{$row['surname']}";
}
echo implode($arr);

#2


1  

You can also resolve this problem in SQL, so foreach would not be needed any more.

您还可以在SQL中解决此问题,因此不再需要foreach。

Just replace

SELECT name, surname FROM people

with

SELECT CONCAT_WS(' ', name, surname) FROM people

#3


0  

Once you’ve fetched the rows from the database, you need to loop over the result set and assign the combined name to a new array:

从数据库中获取行后,需要循环结果集并将组合名称分配给新数组:

<?php

// result set from database
$results = $conn->fetchAll();

// create an empty array to store our names
$names = array();

// loop over result set and add entry to $names array
foreach ($results as $result) {
    $names[] = $row['name'] . ' ' . $row['surname'];
}

print_r($names);

#4


0  

$fullname =array();
foreach($results as $row){
        $fullName = $row['name'] . " " . $row['surname'];
}
print_r($fullname);

#1


1  

$arr = array();
foreach($results as $row) {
   $arr[] = "{$row['name']},{$row['surname']}";
}
echo implode($arr);

#2


1  

You can also resolve this problem in SQL, so foreach would not be needed any more.

您还可以在SQL中解决此问题,因此不再需要foreach。

Just replace

SELECT name, surname FROM people

with

SELECT CONCAT_WS(' ', name, surname) FROM people

#3


0  

Once you’ve fetched the rows from the database, you need to loop over the result set and assign the combined name to a new array:

从数据库中获取行后,需要循环结果集并将组合名称分配给新数组:

<?php

// result set from database
$results = $conn->fetchAll();

// create an empty array to store our names
$names = array();

// loop over result set and add entry to $names array
foreach ($results as $result) {
    $names[] = $row['name'] . ' ' . $row['surname'];
}

print_r($names);

#4


0  

$fullname =array();
foreach($results as $row){
        $fullName = $row['name'] . " " . $row['surname'];
}
print_r($fullname);