使用PHP PDO与MySQL的非对象错误 [英] Non-Object Errors using PHP PDO with MySQL

查看:166
本文介绍了使用PHP PDO与MySQL的非对象错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在这几天抓了我的头,我还没有地方。
我试图通过PHP PDO从MySQL数据库中提取一小组值;我知道PDO工作原理,因为我正在使用它其他地方,我已经基于我的代码围绕以前工作的代码。

  function custom_sub_cat_list $ db_details,$ cat_id){// ln21 
$ subcat = NULL;
try {
$ h = new PDO(mysql:host =。$ db_details ['host']。;; dbase =。$ db_details ['db'],$ db_details ['user '],$ db_details ['pass']);
$ h-> setAttribute(PDO :: ATTR_EMULATE_PREPARES,false);
} catch(PDOException $ ex){
return false;
}
try {
$ q = $ h-> prepare(SELECT category FROM:tbl WHERE parentid =:catid;);
$ q-> bindValue(:tbl,$ db_details ['tbl'],PDO :: PARAM_STR);
$ q-> bindValue(':catid',$ cat_id,PDO :: PARAM_STR);
$ q-> execute();
while($ row = $ _query-> fetch()){
$ subcat ['id'] [] = $ row ['categoryid'];
$ subcat ['name'] [] = $ row ['category'];
};
return $ subcat;
} catch(PDOException $ ex){
return false;
}
} // ln49

  $ cat_id = 123;在bindValue上的非对象上的函数bindValue 
$ db_details = array(
host=> $ sql_host,
db=> $ sql_db,
user=> $ sql_user,
pass=> $ sql_password,
tbl=> $ sql_tbl ['categories']
);
custom_sub_cat_list($ db_details,$ cat_id)



我确定这是显而易见的



b $ b

谢谢非常多!对所有帮助的人,我学到了几个位:-)有一些愚蠢的错误,我忽略了,我只怪责任看看它两天坚实。

  function custom_sub_cat_list($ db_details,$ cat_id){
$ subcat = NULL;
try {
$ h = new PDO(mysql:host =。$ db_details ['host']。; dbname =。$ db_details ['db']。; charset = utf8 ,$ db_details ['user'],$ db_details ['pass']);
$ h-> setAttribute(PDO :: ATTR_EMULATE_PREPARES,false);
$ h-> setAttribute(PDO :: ATTR_ERRMODE,PDO :: ERRMODE_EXCEPTION);
$ q = $ h-> prepare(SELECT category,categoryid FROM。$ db_details ['table']。WHERE parentid =:cid;);
$ q-> bindParam(':cid',$ cat_id,PDO :: PARAM_INT);
$ q-> execute();
while($ row = $ q-> fetch()){
$ subcat ['id'] [] = $ row ['categoryid'];
$ subcat ['name'] [] = $ row ['category'];
};
$ h = NULL;
return $ subcat;
} catch(PDOException $ ex){
print_r($ ex-> getMessage());
print_r($ ex-> getTrace());
// return false;
}
}


解决方案

strong>你不需要一双新鲜的眼睛



你不是画家,而是程序员(据说)。

,而不是看你的代码,你必须运行。并启用错误报告。



哦,只是发现了它

当然,你不应该gag错误信息!

 } catch(PDOException $ ex){
return false;
}

现代版本的@操作符。

在您的代码中移除所有 try..catch 块,并在之后开始使用它们



因此,为了解决这个问题以及未来的许多其他问题


  1. 删除代码中的所有try..catch块。

  2. 启用PDO的错误报告

  3. 不要使用占位符作为标识符,而是按照链接到
  4. 的标签中描述的格式化
  5. 关闭 display_errors 设置如果你不想显示错误(抑制错误消息的唯一原因,我可以想到)。

此外,您不应在每个函数调用中打开单独的连接。

在脚本开头创建一个连接,然后在函数中使用if

  global $ h; 


I've been scratching my head over this for a few days now and I've still got nowhere. I'm trying to pull a small set of values from a MySQL database via PHP PDO; I know PDO works as I am using it else where and I have based my code around te previously working code.

function custom_sub_cat_list($db_details, $cat_id) { //ln21
$subcat = NULL;
try {
    $h = new PDO("mysql:host=".$db_details['host'].";dbase=".$db_details['db'],$db_details['user'],$db_details['pass']);
    $h->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
} catch(PDOException $ex) {
    return false;
}
try {
    $q = $h->prepare("SELECT category FROM :tbl WHERE parentid = :catid;");
    $q->bindValue(":tbl", $db_details['tbl'], PDO::PARAM_STR);
    $q->bindValue(':catid', $cat_id, PDO::PARAM_STR);
    $q->execute();
    while($row = $_query->fetch()) {
        $subcat['id'][] = $row['categoryid'];
        $subcat['name'][] = $row['category'];
    };
    return $subcat;
} catch(PDOException $ex) {
    return false;
}
}//ln49

I am getting "Call to a member function bindValue() on a non-object" on the bindValue's and being called up like below.

$cat_id     = 123;
$db_details = array(
    "host"  => $sql_host,
    "db"    => $sql_db,
    "user"  => $sql_user,
    "pass"  => $sql_password,
    "tbl"   => $sql_tbl['categories']
);
custom_sub_cat_list ($db_details, $cat_id)

I'm sure it's something glaringly obvious but I can't see the problem and would like a fresh pair of eyes.

WORKING VERSION BELOW

Thank You Very Very Much! to everyone who helped, I've learnt a few bits too :-) There was some silly mistakes in there that I had overlooked, I just blame looking at it for two days solid.

function custom_sub_cat_list($db_details, $cat_id) {
$subcat = NULL;
try {
    $h = new PDO("mysql:host=".$db_details['host'].";dbname=".$db_details['db'].";charset=utf8",$db_details['user'],$db_details['pass']);
    $h->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
    $h->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $q = $h->prepare("SELECT category, categoryid FROM ".$db_details['table']." WHERE parentid = :cid;");
    $q->bindParam(':cid', $cat_id, PDO::PARAM_INT);
    $q->execute();
    while($row = $q->fetch()) {
        $subcat['id'][] = $row['categoryid'];
        $subcat['name'][] = $row['category'];
    };
    $h = NULL;
    return $subcat;
} catch(PDOException $ex) {
    print_r($ex->getMessage());
    print_r($ex->getTrace());
    //return false;
}
}

解决方案

You don't need a fresh pair of eyes

You are not painter but a programmer (supposedly).
So, instead of watching your code you have to run it. And enable error reporting.

Oh, just spotted it
And of course, you shouldn't gag error messages!

} catch(PDOException $ex) {
    return false;
}

a modern version of @ operator.
Please get rid of ALL try..catch blocks in your code and start using them only after learning what are they for.

So, in order to solve this very problem as well as many other problems in the future

  1. Get rid of all try..catch blocks in your code.
  2. Enable error reporting for PDO as described in tag wiki I linked to in the comments.
  3. Do not use placeholders for the identifiers but format them as described in the tag wiki I linked to
  4. Turn off display_errors setting if you don't want errors to be displayed (the only reason for suppressing error messages I can think of).

Also, you shouldn't open separate connection in every function call.
Create one connection in the beginning of your script and then use if in the function, using

global $h;

这篇关于使用PHP PDO与MySQL的非对象错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆