想要使用 php pdo 从表中删除一行


Want to delete a row from a table using php pdo

我想使用 php pdo 从表中删除一行。我正在使用以下代码,

 $dsn = 'mysql:host=127.0.0.1;dbname=as1';
         $user = 'root';
         $password = '';
    try {
      // Connect and create the PDO object
     $pdo = new PDO($dsn, $user, $password);
     $pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
    }
    catch(PDOException $e) {
     echo 'Database connection failed - ';
     echo $e->getMessage();
     exit;
     }
    $sql1="DELETE FROM photo WHERE id=?";
        $q1=array($result);
                                try {
        $stmt1 = $pdo->prepare($sql1);
        $stmt1->execute($q1);
        $stmt1->setFetchMode(PDO::FETCH_BOTH);
     $result1= $stmt1->fetchColumn();
    }
    catch (PDOException $e) {
        die("Failed to run query: " . $e->getMessage());
        } 

但是我在表中的数据没有删除...它显示无法运行查询。

您没有为 ? 提供值

$stmt1->execute($q);   // Where is $q defined?

应该是这样的

 $q=array(1);
 $stmt1->execute($q);