我找了一个答案但找不到,因为这里的问题似乎略有不同。
$vid = $_SESSION['ID_Vendor'];
echo "ID: $vid";
$q = 'SELECT business_name, vd.ID_Vendor, res.ID_RestaurantEstablishment
FROM restaurant res
INNER JOIN vendor_data vd
ON vd.ID_Vendor=res.ID_Vendor AND res.ID_Vendor="$vid" ORDER BY business_name ASC';
变量$ vid确实有一个值(在这种情况下等于2,但它可能不同),但是,当我专门设置时
WHERE res.ID_Vendor=2
我的查询返回正确的和预期的值列表,但是当我使用
时 WHERE res.ID_Vendor="$vid"
使用" $ vid",我的值的回显只是空的。
下面是代码的完整代码片段,用于回显输出。 谢谢你的帮助。
$vid = $_SESSION['ID_Vendor'];
echo "ID: $vid";
$q = 'SELECT business_name, vd.ID_Vendor, res.ID_RestaurantEstablishment
FROM restaurant res
INNER JOIN vendor_data vd
ON vd.ID_Vendor=sfe.ID_Vendor AND res.ID_Vendor="$vid" ORDER BY business_name ASC';
$r = mysqli_query($connection, $q);
while ($row = mysqli_fetch_array ($r, MYSQLI_NUM)) {
echo '>' . htmlspecialchars($row[0]) . ' ' . htmlspecialchars($row[1]) . ' ' . htmlspecialchars($row[2]) .'</option>';
}
答案 0 :(得分:2)
PHP无法识别撇号对中的变量''。将变量放在“”中。 EG:
showAllValidations()
答案 1 :(得分:0)
您的代码存在一些问题:
首先,这不起作用,将逐字回显$vid
。
echo "ID: $vid";
这将返回:ID: $vid
你需要用变量连接字符串,它应该是:
echo "ID: " . $vid;
这将返回:ID: 2
其次,您的查询无效:
$q = 'SELECT business_name, vd.ID_Vendor, res.ID_RestaurantEstablishment
FROM restaurant res
INNER JOIN vendor_data vd
ON vd.ID_Vendor=res.ID_Vendor AND res.ID_Vendor="$vid" ORDER BY business_name ASC';
您应该用双引号括起查询,而用单引号括起您的变量:
$q = "SELECT business_name, vd.ID_Vendor, res.ID_RestaurantEstablishment
FROM restaurant res
INNER JOIN vendor_data vd
ON vd.ID_Vendor=res.ID_Vendor AND res.ID_Vendor='$vid' ORDER BY business_name ASC";
答案 2 :(得分:0)
对于SQL,不像javascript或php,只允许单引号用于字符串或字符。
对于MySQL,如果res.id_vendor的列类型是数字,res.id_vendor ='1'与res.id_vendor = 1相同,所以最好总是用单引号包装变量,如 res.id_vendor = '$ VID'强>
最后,对编码约定的评论:建议不要在列命名中混合大写和小写字符,使用“_”作为单词分隔符,如
vd.id_vendor
res.id_restaurant_establishment
答案 3 :(得分:0)
由于我提出了这个想法,我想我应该说明使用准备好的陈述。
使用mysqli
将按如下方式进行(假设$connection
已成功初始化):
// The indentation here is purely a matter of personal preference
$query = 'SELECT business_name, vd.ID_Vendor, res.ID_RestaurantEstablishment
FROM restaurant res
INNER JOIN vendor_data vd
ON vd.ID_Vendor = res.ID_Vendor
WHERE res.ID_Vendor = ?
ORDER BY business_name ASC';
$stmt = $connection->prepare($query);
$stmt->bind_param('s', $vid); // 's' assumes $vid is string; use 'i' for int
$stmt->execute();
$res = $stmt->get_result();
while ($row = $res->fetch_array(MYSQLI_NUM))
{
echo '>' . htmlspecialchars($row[0]) . ' ' . htmlspecialchars($row[1]) . ' ' . htmlspecialchars($row[2]) .'</option>';
}
使用PDO
的成语是相同的。 PDO data source name (DSN)的格式在线记录。
$conn = new PDO($dsn, $username, $password); // define these vars elsewhere
$query = 'SELECT business_name, vd.ID_Vendor, res.ID_RestaurantEstablishment
FROM restaurant res
INNER JOIN vendor_data vd
ON vd.ID_Vendor = res.ID_Vendor
WHERE res.ID_Vendor = :vid
ORDER BY business_name ASC';
$stmt = $conn->prepare($query);
$stmt->execute(array(':vid' => $vid));
while ($row = $stmt->fetch(PDO::FETCH_NUM))
{
echo '>' . htmlspecialchars($row[0]) . ' ' . htmlspecialchars($row[1]) . ' ' . htmlspecialchars($row[2]) .'</option>';
}
在这两种情况下,我都将错误处理留作读者练习。