我想在单列中以逗号分隔存储多个数据,并在不同行的php的帮助下获取数据 就像在列视图中一样,它看起来像这样 - 牙刷-1234,玻璃-1234, 在那里1234是产品ID 我希望在php中获取这些数据,每行包含所有细节的分隔值。 喜欢
<?php $sql="select product_name form products where product_id=1234" ?>
因此可以通过 - 和单列来获取数据并将其分开。
答案 0 :(得分:0)
您希望将逗号分隔的数据显示为单独的行。你可以试试这个。
<?
//Here is the complete example of your code by using MYSQLi Object Oriented:
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "select product_id,product_name form products where product_id=1234";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
$yourData = array();
while($row = $result->fetch_assoc()) {
$yourData[$row['product_id']] = explode(",",$row['product_name']);
}
if(count($yourData) > 0){
foreach ($yourData as $key => $value) {
foreach ($value as $productname) {
echo "Product ID >> ".$key. " -- Product Name >> ".$productname;
}
}
}
}
else
{
echo "0 results";
}
$conn->close();
?>
旁注:如果您在应用中使用mysql_*
,建议您使用mysqli_*
或PDO
因为mysql_*
已被弃用并且在PHP 7中不可用。