【发布时间】:2016-03-31 11:00:21
【问题描述】:
我在 DB 中有 2 个表,一个是 User_info,另一个是 Product_info。我想要做的是,如果给我User_id,那么我将在表User_info 中检查与User_id 对应的Product_id。然后我将使用我检索到的这个Product_id 从表Product_info 中找到Product_name。
**Table-User_info**
User_id Product_id
2 P1
3 P4
2 P3
45 P7
**Table-Product_info**
Product_name Product_id
A P1
B P4
C P3
D P7
例如,如果给我User_id 是2,那么我想显示A 和C
以下是我写的代码:
$mysqli=new mysqli("127.0.0.1","root","","bargad");//Connecting to the database
if($mysqli->connect_error)
{
echo "Couldnt connect to server";
}
else
{
$query="SELECT Product_id from User_info WHERE User_id=?";
$statement=$mysqli->prepare($query);
//binding parameters
$statement->bind_param('s',$u_id);//u_id is given
$statement->execute();
$statement->bind_result($pdt_id);
while($statement->fetch())
{
$query2="SELECT Product_name from Product_info WHERE Product_id=?";
$statement_2=$mysqli->prepare($query2);
$statement_2->bind_param('s',$pdt_id);
$statement_2->execute();
$statement_2->bind_result($pdt_name);
while($statement_2->fetch())
{
echo $Product_name;
}
}
}
> The error i got is -Fatal error: Uncaught Error: Call to a member
> function bind_param() on boolean and Stack trace: #0 {main} thrown
【问题讨论】:
-
您应该查看存储过程。您基本上可以创建一个接受参数并执行您需要的任何逻辑的 mysql 函数。您可以从一个存储过程返回多个结果集。
-
@Matthew 你能举个小例子吗?
-
检查连接表w3schools sql join