douluoqiu4538 2014-05-31 14:26 采纳率: 100%
浏览 154
已采纳

使用PDO进行左连接

I am using the following PDO query:

  <?php
            $cadena =   $_SESSION[Region];

// We Will prepare SQL Query


       $STM = $dbh->prepare("SELECT  `id_mesero`, `nombre_mesero`,`alias_mesero`, `rest_mesero` FROM tbmeseros WHERE cadena_mesero='$cadena'");
// For Executing prepared statement we will use below function
    $STM->execute();
// we will fetch records like this and use foreach loop to show multiple Results
    $STMrecords = $STM->fetchAll();
    foreach($STMrecords as $row)
        {

The value from the 'rest_mesero' field is the index from the table 'tbrestaurantes'.

I would need to join some fields values from 'tbrestaurantes' to the PDO query, but I don't know how to do it using PDO.

Any help is welcome.

UPDATED QUESTION TEXT

This is my proposal for the query :

$dbh->prepare("SELECT * FROM tbmeseros LEFT JOIN tbrestaurantes ON tbmeseros.rest_mesero = tbrestaurantes.id_restaurante WHERE tbmeseros.cad_mesero = ?");

But is show an error:

Warning: PDOStatement::execute() [pdostatement.execute]: SQLSTATE[HY093]: Invalid parameter number: no parameters were bound in /.../AdminMeseros.php on line 80

Line 80 is

$STM->execute();

This is my updated query:

              <?php
            $cadena =   $_SESSION[Region];



       $STM =$dbh->prepare("SELECT * FROM tbmeseros LEFT JOIN tbrestaurantes ON tbmeseros.rest_mesero = tbrestaurantes.id_restaurante WHERE tbmeseros.cad_mesero = ?");
       $STM->bindParam(1, $cadena);
// For Executing prepared statement we will use below function
    $STM->execute(array($cadena));
// we will fetch records like this and use foreach loop to show multiple Results
    $STMrecords = $STM->fetchAll();
    foreach($STMrecords as $row)
        {

And here table's screenshots:

For tbmeseros:

enter image description here

For tbrestaurantes: enter image description hereenter image description here

The value of $cadena is 'HQ3'.

  • 写回答

2条回答 默认 最新

  • dongyao5843 2014-05-31 14:37
    关注

    When you put a parameter in the SQL, you need to supply the value for the parameter. There are two ways to do that:

    1) Call bindParam():

    $STM->bindParam(1, $cadana);
    

    2) Provide the values when calling execute():

    $STM->execute(array($cadana));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 yolov8边框坐标
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂