简体   繁体   English

如何将MySQLi返回对象用作新SQL查询的表

[英]How to use a MySQLi return object as a table for new SQL queries

I don't understand how we can use the results that is a table from a SELECT query for new queries. 我不明白我们如何使用SELECT查询中的表结果来进行新查询。 Let's say we join two tables using INNER JOIN and we get a mysqli return object holding the rows of the joined table as an array. 假设我们使用INNER JOIN连接两个表,我们得到一个mysqli return object ,将连接表的行保存为数组。 But how can I use this for new queries. 但是我如何将它用于新查询。 I tried doing it by using the result object $result as a mysql table but it doesn't work, neither does doing $result->fetch_all() make it into a table. 我尝试使用结果对象$result作为mysql表,但它不起作用,也没有做$result->fetch_all()使它成为一个表。

I have a form that takes in $_GET['winery_name'] and $_GET['wine_type'] that are both strings in winery and wine_type tables. 我有一个接收$_GET['winery_name']$_GET['wine_type'] ,它们都是winery和wine_type表中的字符串。 I need to go from their string value to their id values that are their primary keys and serve as foreign keys in wine table. 我需要从他们的字符串值转到他们的主键id值,并作为wine表中的外键。

Depending on the winery_name and wine_type, I have to filter out the wine table and post filtered results. 根据winery_name和wine_type,我必须过滤葡萄酒表并过滤过滤后的结果。 My approach so far doesn't work and I don't understand how to make it work using join tables. 到目前为止,我的方法不起作用,我不明白如何使用连接表使其工作。 Here's what I have so far: 这是我到目前为止所拥有的:

  <?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";

$row = $result->fetch_object();
$wineryid = $row -> winery_id;
echo "Winery id is ".$wineryid;


$sql = "SELECT * FROM wine where wine.winery_id =".$wineryid;
$result1 = $db->query($sql);
echo "Finished 4";


$sql = "SELECT * FROM wine_type where wine_type='".$_GET['wine_type']."'";
echo "Finished 5";
$result = $db->query($sql);
echo "Finished 6";

$row = $result->fetch_object();
$winetypeid = $row -> wine_type_id;
echo "Wine type id is ".$winetypeid;
//code stops working here
$sql = "SELECT * FROM ".$result1." WHERE wine_type =".$winetypeid;
echo "Finished 8";
$result = $db->query($sql);
echo "Finished 9";

while($row = $result1->fetch_object()){

    echo $row->wine_name;
}

}
?>

Here's the ER diagram: 这是ER图: ER图

Again, I am trying to go from winery_name(string) to winery_id(int) in winery and wine_type(string) to wine_type_id(int) in wine_type . 同样,我试图从winery_name(串)去winery_id(INT)的winery和wine_type(串)中wine_type_id(INT) wine_type Then using the int values to filter out wines from wine table. 然后使用int值过滤掉wine桌上的wine Also wine_type is an int value in in wine but string in wine_type . 此外wine_type是在一个int值wine ,但字符串中wine_type

If there is a way to make this statement work please let me know: 如果有办法让这个陈述有效,请告诉我:

$sql = "SELECT * FROM ".$result1." WHERE wine_type =".$winetypeid;

wine: 葡萄酒: 葡萄酒

winery 酒厂 酒厂

wine_type wine_type wine_type

You cannot do this on the web server. 您无法在Web服务器上执行此操作。 Once the Query results have been transferred from the database to the client Server, the database releases all references to the Query results. 将查询结果从数据库传输到客户端服务器后,数据库将释放对查询结果的所有引用。

This can be done with a nested select Statement: 这可以使用嵌套的select语句来完成:

SELECT a, b FROM (
    SELECT a, b FROM c JOIN d ON a =e
)

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

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