我正在使用以下PDO查询:

  <?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)
        {


“ rest_mesero”字段中的值是表“ tbrestaurantes”中的索引。

我需要将“ tbrestaurantes”中的某些字段值连接到PDO查询,但是我不知道如何使用PDO。

欢迎任何帮助。

更新的问题文本

这是我对查询的建议:

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


但是显示错误:

警告:PDOStatement :: execute()[pdostatement.execute]:SQLSTATE [HY093]:无效的参数编号:在第80行的/.../AdminMeseros.php中未绑定任何参数

80行是

$STM->execute();


这是我更新的查询:

              <?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)
        {


这是表格的屏幕截图:

对于tbmeseros:



对于tbrestaurantes:



$ cadena的值为“ HQ3”。

最佳答案

在SQL中放置参数时,需要提供参数的值。有两种方法可以做到这一点:

1)呼叫bindParam()

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


2)调用execute()时提供值:

$STM->execute(array($cadana));

10-04 18:58