简体   繁体   English

如何使用 mySQLi 在 SQL 查询中使用带有表对象的 $result 变量

[英]How to use a $result variable with table object in SQL query using mySQLi

I am trying to make this code work, but it only works until the second echo statement echo "Finished 2";我试图让这段代码工作,但它只工作到第二个 echo 语句echo "Finished 2"; . .

<?php
if (count($_GET) > 0){
$sql = "SELECT * FROM winery WHERE winery_name='".$_GET['winery_name']."'";
echo "Finished 1";
$result = $db->query($sql);
echo "Finished 2";
$sql = "SELECT * FROM".$result."WHERE wine_type='".$_GET['wine_type']."'";
echo "Finished 3";
$result = $db->query($sql);
echo "Finished 4";
$sql = "SELECT * FROM".$result.", wine_variety WHERE wine_id=wine_variety.wine_id";
echo "Finished 5";
$result = $db->query($sql);
echo "Finished 6";
$sql = "SELECT * FROM".$result."WHERE variety_id='".$_GET['grape_variety']."'";
echo "Finished 7";
$result = $db->query($sql);
echo "Finished all queries";

}
?>

The problem from my understanding is that sql doesn't recognize $result as a table, but $result stores the return table from my query.根据我的理解,问题是 sql 无法将$result识别为表,但$result存储了我查询的返回表。 How can I make SQL use the return table from $result in a new query?如何让 SQL 在新查询中使用$result的返回表?

在此处输入图片说明

I think from your winery table you are fetching other table name???我想从你的酒厂表你正在获取其他表名???

If so you need to fetch row from the $result and then get appropriate column from winery table (ie column with other table name).如果是这样,您需要从 $result 中获取行,然后从酒厂表中获取适当的列(即具有其他表名的列)。

BTW best option would be joining two tables.顺便说一句,最好的选择是加入两张桌子。

One more point where I think you are making mistake is我认为你犯错的另一点是

$sql = "SELECT * FROM".$result."WHERE wine_type='".$_GET['wine_type']."'";

should be应该

$sql = "SELECT * FROM ".$result." WHERE wine_type='".$_GET['wine_type']."'";

space between FROM & double quote and between double quote and WHERE FROM 和双引号之间以及双引号和 WHERE 之间的空格

To get winery_id from winary_name you can write your HTML form like要从 winary_name 获取 winery_id,您可以编写 HTML 表单,例如

<select name="winary_id">
    <option value="Winary ID HERE">Winary Name Here</option> // you can generate your dynamic options like this which will return id instead of name
</select>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM