douchu4048 2015-01-20 13:15
浏览 9

从数组转换为字符串,内爆不起作用

    <?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "comp4";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT id , First_Name, Last_Name FROM member WHERE username='tracy'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
    echo "id: " . $row["id"]. " - Name: " . $row["First_Name"]. " " . $row["Last_Name"]. "<br>";
}
} else {
echo "0 results";
}



$conn->close();
?>

I have this code in order to display some details about a member in my database but I want to make it into a string and a variable, I tried to use the php implode function but I recieved an error saying that some of the other variables were unexpected any tips or anything wrong that im doing?

  • 写回答

4条回答 默认 最新

  • dongwen7187 2015-01-20 13:25
    关注

    In your while loop, you should first concatenate all your column in one array, to be then collapsed in one single string as following :

    while($row = $result->fetch_assoc()) 
        {
            // temp variables
            $id = $row["id"];
            $first_name = $row["First_Name"];
            $last_name = $row["Last_Name"];
    
            $array = array($id, $first_name, $last_name); // creating an array
    
            $result = implode(" - ", $array); // Collapsing data using dash
    
            printf($result); // displaying data collapsed
        }
    

    from official php implode doc

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?