如何在SQL查询中使用mySQLi将$ result变量与表对象一起使用

时间:2021-01-26 15:43:36

I am trying to make this code work, but it only works until the second echo statement echo "Finished 2";.

我试图让这个代码工作,但它只能工作,直到第二个echo语句回显“完成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. How can I make SQL use the return table from $result in a new query?

根据我的理解,问题是sql不能将$ result识别为表,但$ result存储来自查询的返回表。如何让SQL在新查询中使用$ result的返回表?

如何在SQL查询中使用mySQLi将$ result变量与表对象一起使用

1 个解决方案

#1


1  

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 (i.e. column with other table name).

如果需要,您需要从$ result中获取行,然后从winery表中获取相应的列(即具有其他表名的列)。

BTW best option would be joining two tables.

BTW最佳选择是加入两张桌子。

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>

#1


1  

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 (i.e. column with other table name).

如果需要,您需要从$ result中获取行,然后从winery表中获取相应的列(即具有其他表名的列)。

BTW best option would be joining two tables.

BTW最佳选择是加入两张桌子。

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>