无法从db检索数据

时间:2016-09-08 07:36:56

标签: php

我正在尝试连接mysql db并获取一些数据。但是echo命令没有做任何事情。我又做错了什么?为什么我得到Undefined index:konu_baslik error

<?php

try{
    $db=new PDO("mysql:host=localhost;dbname=blog","root","");

}
catch (PDOException $ex){
$ex->getMessage();

}
$v=$db->prepare("select * from konular");

$v->execute(array());

$x=$v->fetchAll(PDO::FETCH_ASSOC);
foreach ($x as $m){
echo  $m["konu_baslik"];
}

?>

1 个答案:

答案 0 :(得分:2)

我希望这项工作与您合作:

<?php
        $servername = "localhost";
        $username   = "root";
        $password   = "";
        $database  = "blog";
            try {
                    $db =new PDO("mysql:host=$servername;dbname=$database", $username, $password);
                    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

                }
            catch(PDOException $e)
                {
                    echo "Connection failed: " . $e->getMessage();
                }

            $v=$db->prepare("SELECT * FROM konular");
            $v->execute();

            if($v)
            {
                while($FetchData = $v->FETCH(PDO::FETCH_ASSOC))
                {
                    echo $FetchData['konu_baslik'].'\n';
                }
            }

            else
            {
                echo "Something went wrong";
            }

?>