dongsuoying9059 2012-03-21 16:54
浏览 82
已采纳

无法使用PHP从SQLite检索数据

This is my first try to use SQLite, I am trying to retrieve data from SQLite but am not getting success neither its throwing any exceptions.

my respective code is as follows:

$sql_query = "SELECT * FROM `item` WHERE combo LIKE '" . $value . "' LIMIT 1;";
        try {
            /*** connect to SQLite database ***/
            $dbh = new PDO("sqlite:src/mydb.s3db");
            foreach ($dbh->query($sql_query) as $row) {
                $name = $row['name'];
                echo "name".$name; 
                $style = $row['style'];
                echo $style;
                $appr = $row['appr'];
                echo $appr;
                $style = $row['style'];
                echo $style;
            }

        } catch(PDOException $e) {
            //echo $e -> getMessage();
            echo "problem with DB";
            echo "<br>";
        }

Kindly guide me through this. Thank you.


//phpnifo();

PDO

PDO support enabled | PDO drivers mysql, sqlite, sqlite2

pdo_sqlite

PDO Driver for SQLite 3.x enabled | PECL Module version (bundled) 1.0.1 $Id: pdo_sqlite.c 293036 2010-01-03 09:23:27Z sebastian $ SQLite Library 3.3.7

  • 写回答

1条回答 默认 最新

  • douyuepi6485 2012-03-21 17:18
    关注

    a) Tell PDO that you want it to use exceptions for reporting errors

    $dbh = new PDO("sqlite::memory:");
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    

    b) instead of putting the payload paraemters into the sql statement use prepared parametrized statements

    try {
        $stmt = $dbh->prepare( 'SELECT * FROM `item` WHERE combo LIKE ? LIMIT 1' );
        $stmt->execute( array($value) );
        foreach( $stmt as $row ) { ...
    

    c) If you still don't get any output try code that unconditionally prints something. E.g. a SELECT Count(*) will always return at least one record (if no error occurs).

    $stmt = $dbh->prepare( 'SELECT Count(*) as cnt FROM `item` WHERE combo LIKE ?' );
    $stmt->execute( array($value) );
    foreach( $stmt as $row ) {
        echo 'Count: ', $row['cnt'], "
    ";
    }
    $stmt = null;
    

    edit: self-contained example

    <?php
    ini_set('display_errors', true);
    error_reporting(E_ALL);
    
    echo "start
    ";
    
    try {
        $dbh = new PDO("sqlite::memory:");
        $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
        setup($dbh);
    
    
        $stmt = $dbh->prepare('SELECT * FROM item WHERE combo LIKE ? LIMIT 1');
        $stmt->execute( array('comboB') );
        foreach( $stmt as $row ) {
            echo
                $row['name'], " ",
                $row['style'], " ",
                $row['appr'], "
    "
            ;
        }
    }
    catch(Exception $ex) {
        var_dump($ex);
    }
    echo "done.
    ";
    
    function setup($dbh) {
        $dbh->exec('
            CREATE TABLE item (
                combo TEXT,
                name TEXT,
                style TEXT,
                appr TEXT
            )
        ');
        $stmt = $dbh->prepare('INSERT INTO item (combo,name,style,appr) VALUES (?,?,?,?)');
        $stmt->execute( array('comboA','nameA','styleA','apprA') );
        $stmt->execute( array('comboB','nameB','styleB','apprB') );
        $stmt->execute( array('comboC','nameC','styleC','apprC') );
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。