explode/implode 来自数据库 php

explode/implode from database php

我想要列数据中的字符串。 但是失败了。

    <?php
    $conn = mysql_connect("localhost", "nantawat", "12345678") or die(mysql_error());
    $select_db = mysql_select_db("my_db", $conn) or die(mysql_error());
    $select_tbl = mysql_query("SELECT * FROM log", $conn);

    while ($fetch = mysql_fetch_object($select_tbl)) {
        $r = $fetch->data;

    $i = explode(",", $r);

    if (!isset($i[1])) {
        for ($j = 0; $j <= 200; $j++) {
            $i[$j] = null;
        }
    }

    $name = $i[0];
    $mama = $i[1];

    $no = $i[2];
    $a = $i[3];
    $b = $i[4];

    echo $name . "</br>";
    echo $mama . $no . $a . $b . "</br>";

}

while ($data = mysql_fetch_object($select_tbl)) {

    echo $data->data . "<br>";
}

?>

但我想要输出=

bus 
car
bike
aabus
car
bike
aabus
car
bike
aabus

ddd
ee

我不是

Notice: Undefined offset: 3 in C:\xampp\htdocs\logs\New folder (2)\explode.php on line 21

Notice: Undefined offset: 4 in C:\xampp\htdocs\logs\New folder (2)\explode.php on line 22

谢谢。

首先,在单个单元格中存储逗号分隔值是不明智的,而且您正在使用已弃用的 mysql_ 函数。我认为您的解决方案可以通过使用 foreach 而不是 isset 部分找到:

while ($fetch = mysql_fetch_object($select_tbl)) {
    $r = $fetch->data;
    $i = explode(",", $r);
    foreach ($i as $q){
       echo $q . '<br/>';
    }
}

如果您仍想访问变量 $name、$mama、$no 和 $ab,您可以专门针对这些变量使用 isset。

while ($fetch = mysql_fetch_object($select_tbl)) {
    $r = $fetch->data;
    $i = explode(",", $r);
    if (isset($i[0])){
        $name = $i[0];
        echo $name . '<br>'; //only echo if it exists
    }
    if (isset($i[1])){
        $mama= $i[1];
        echo $mama. '<br>'; //only echo if it exists
   }
   //try it yourself for $no and $ab
 }

你应该做你想做的事。

您想连接到数据库然后执行它:

$conn = mysql_connect("localhost", "nantawat", "12345678") or die(mysql_error());

我建议您使用 mysqli 库而不是 mysql(mysql 在新的 php 版本中已弃用,并在 php7 中完全删除)

$conn = mysqli_connect("localhost", "nantawat", "12345678", "my_db") or die(mysql_error());

您想在 log table 上查询,然后执行:

$select_tbl = mysqli_query($conn, "SELECT * FROM log");

您想从结果中获取信息,然后执行此操作:

while ($row = mysqli_fetch_array($select_tbl)) {
    echo $row['id_user'];
    echo $row['id_doc'];
    echo $row['date'];
    echo $row['data'];
}

你想爆数据,那就做:

while ($row = mysqli_fetch_array($select_tbl)) {
    echo $row['id_user'];
    echo $row['id_doc'];
    echo $row['date'];
    $data = explode(',', $row['data']);
    foreach ($data as $d) {
        if ($d !== '') { // because before first comma or after last can be empty
            echo $d . PHP_EOL;
        }
    }
}

如果要将数据库结果保存在变量中:

如果你只获取一行数据库,你可以直接将它们保存在变量中:

$id_user = '';
$id_doc = '';
$date = '';
$data = array();
id ($row = mysqli_fetch_array($select_tbl)) {
    $id_user = $row['id_user'];
    $id_doc = $row['id_doc'];
    $date = $row['date'];
    $tempData = explode(',', $row['data']);
    foreach ($tempData as $d) {
        if ($d !== '') {
            $data[] = $d;
        }
    }
}

如果你有多行数据库,你需要将它们全部保存在一个总数组中:

$array = array();
id ($row = mysqli_fetch_array($select_tbl)) {
    $id_user = $row['id_user'];
    $id_doc = $row['id_doc'];
    $date = $row['date'];
    $data = array();
    $tempData = explode(',', $row['data']);
    foreach ($tempData as $d) {
        if ($d !== '') {
            $data[] = $d;
        }
    }
    $array[] = array(
        'id_user' => $id_user,
        'id_doc' => $id_doc,
        'date' => $date,
        'data' => data,
    );
}

最后用它来查看最终数组的结构:

echo '<pre>';
pront_r($array);
echo '</pre>';

尝试:

while ($row = mysqli_fetch_array($select_tbl)) {
    extract($row);
    /* Using extract method can get the array key value as variable  
    Below variables are available 
    $id_user;
    $id_doc;
    $date;
    $data; */
}