dongsi0625 2016-03-31 09:29
浏览 49
已采纳

从数据库php爆炸/内爆

I'm want string out of the column data. But it failed.

enter image description here

    <?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>";
}

?>

enter image description here

But i want output =

bus 
car
bike
aabus
car
bike
aabus
car
bike
aabus

ddd
ee

And i not

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

Thank You.

  • 写回答

3条回答 默认 最新

  • dongneng5383 2016-03-31 09:52
    关注

    You should just do what you want to do.

    You want to connect to database then do it:

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

    I suggest you to use mysqli library instead of mysql (mysql is deprecated in new php versions and totally removed in php7)

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

    You want to query on log table, then do it:

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

    You want to fetch info from your result, then do it:

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

    You want to explode data, then do it:

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

    If you want to save database result in variables:

    If you are getting only one row of database, you can save them in variables directly:

    $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;
            }
        }
    }
    

    And if you have multiple rows of database you need to save them all in a total array:

    $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,
        );
    }
    

    And finally use this to see what structure your final array has:

    echo '<pre>';
    pront_r($array);
    echo '</pre>';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 maixpy训练模型,模型训练好了以后,开发板通电会报错,不知道是什么问题
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容