doumu8217 2012-11-26 23:40
浏览 38
已采纳

我该如何在函数中使用PDO

1: PDO use prepared statement which the meaning of prepared is something that we use it in many functions i think, and we prepared it for many functions.then i just want to know should i use diffrent sql query in all of my functions or should i define many query at first point in my init.php file(which include functions and connection to db)and then in each function pass them to the function to use these queries?just an example for what i described:

$query1 = "Insert into names(id,name) values (:id,:names)"
$query2 = "insert into games(id,name) values (:id,:names)"

and then for functions somtihing like this

function job($query){
//do the job
}

i ask this question because i read in some place i have to use this prepared statment in more than one place and if its true how should i use some unique query in a function?

2: can i use this :id :names in two prepared statement?or i have to set unique names?

$query1 = "Insert into names(id,name) values (:id,:names)"
$query2 = "insert into games(id,name) values (:id,:names)"

wont they conflict with each other?

  • 写回答

2条回答 默认 最新

  • douluan5738 2012-11-26 23:47
    关注

    I certainly wouldn't define all of my queries in one place for them to be used later -- I would put the queries where they are going to be used; otherwise it could get very confusing, especially if you name them query1, query2, etc.

    If you have the same query that is prepared separately, then you can use the same tokens without any problem:

    $stmt1 = $db->prepare($query1);
    $stmt2 = $db->prepare($query2);
    

    You can bind completely different values to :id and :name for both stmt1 and stmt2.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助