dongxie3701 2017-02-02 14:35
浏览 229

PHP错误oci_bind_by_name():用于绑定的无效变量

I have a PHP file which will extract data from an RSS feed and insert it into a database. Usually I wrap each field with a custom function to replace quotes but no longer need it (because of a change in requirements). For some reason when I use the logic below I get an error "error oci_bind_by_name(): Invalid variable used for bind" but if I wrap each field in the custom function like this:

$guid = customfunction($item->guid); 

it works, why?

Thank you

foreach($rss->channel->item as $item) {




   print '<a href="'.$item->link.'">'.$item->title.'</a><br />';

    $guid = $item->guid;
    $title = $item->title;
    $link = $item->link;
    $pubDate = $item->pubDate;
    $description = $item->description;
    $content = $item->content;

    $stid = oci_parse($spConn,"INSERT INTO table123
                (sku, title, link, pubDate, field1, field2)
                VALUES(:guid_bv, :title_bv, :link_bv, :pubDate_bv, :description_bv, :content_bv)");


                oci_bind_by_name($stid, ":guid_bv", $guid);
                oci_bind_by_name($stid, ":title_bv", $title);
                oci_bind_by_name($stid, ":link_bv", $link);
                oci_bind_by_name($stid, ":pubDate_bv", $pubDate);
                oci_bind_by_name($stid, ":description_bv", $description);
                oci_bind_by_name($stid, ":content_bv", $content);

                oci_execute($stid);
  • 写回答

1条回答 默认 最新

  • drnbotxo449747 2017-02-06 15:45
    关注

    By default, oci_bind_by_name() requires only 3 parameters, and treats all variables that you bind as characters. It works fine for most cases however, but if you are try to add a float, binary data or integers it might scream at you and complain about invalid variable used. To fix this either you need to either give oci_bind_by_name another set of parameters, ex:

      if(is_numeric($v2)){ 
        oci_bind_by_name($stmth, $bvar, $v2,  8, OCI_B_INT); 
      }else{ 
        $v2 = (string) $v2; 
        oci_bind_by_name($stmth, $bvar, $v2, -1, SQLT_CHR); 
      } 
    

    or simply wrap your variable in strval() like this

    oci_bind_by_name($stid, ":description_bv", strval($description));
    oci_bind_by_name($stid, ":content_bv", strval($content));
    

    While I strongly advise creating/using a custom class that will handle all variable binds for you, using strval() with your variables should eliminate all "Invalid variable used for bind" errors when passing integers, doubles or other formats.

    评论

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型