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条)

报告相同问题?

悬赏问题

  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?