我有一个名为$ row ['mykey']的变量,它是从表一上的查询中生成的,我已经回应了 它到我的页面,它确实显示我正在寻找的价值。现在我需要使用这个varible来设置我的新查询 在表2中,仅在与表2中的contractor_lock匹配时才显示结果。 $ stmt1一个可以正常显示整个表,但我想使用$ stmt2但没有得到数据结果。谢谢你的帮助!
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt1 = $conn->prepare("SELECT client_id, companyname, contact, contractor_lock FROM table_two");
$stmt2 = $conn->prepare("SELECT client_id, companyname, contact, contractor_lock FROM table two WHERE contractor_lock = $row['mykey']");
$stmt->execute();
$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) {
echo $v;
}
}
catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
$conn = null;
}
答案 0 :(得分:0)
您必须将$row['mykey']
连接到stmt2
,而不是直接包含它,因为它可能被解释为字符串。
应该是这样的
$stmt2 = $conn->prepare("SELECT client_id, companyname, contact, contractor_lock FROM table two WHERE contractor_lock = ?");
$stmt2->execute($row['mykey']);