简体   繁体   English

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

[英]PHP error oci_bind_by_name(): Invalid variable used for bind

I have a PHP file which will extract data from an RSS feed and insert it into a database. 我有一个PHP文件,它将从RSS提要中提取数据并将其插入数据库。 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: 出于某种原因,当我使用下面的逻辑时,我得到一个错误“error oci_bind_by_name():用于绑定的无效变量”但是如果我在自定义函数中包装每个字段,如下所示:

$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);

By default, oci_bind_by_name() requires only 3 parameters, and treats all variables that you bind as characters. 默认情况下,oci_bind_by_name()仅需要3个参数,并将您绑定的所有变量视为字符。 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: 要解决此问题,您需要为oci_bind_by_name提供另一组参数,例如:

  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 或者简单地将您的变量包装在strval()中

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. 虽然我强烈建议创建/使用将为您处理所有变量绑定的自定义类,但使用带变量的strval()应该在传递整数,双精度或其他格式时消除所有“用于绑定的无效变量”错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM