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 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决