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条)

报告相同问题?

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装