多个mysql表加入php?

时间:2015-10-25 19:30:58

标签: php mysql

老实说,我不知道如何做我想做的事情。我在php和Mysql中都没有过多的经验,但我正在尝试并且可以使用一些帮助,最好使用工作示例代码。

问题:我有3张桌子

  1. 成员
  2. customfields
  3. customvals
  4. 成员包含:

    membername | Id
    

    customfields包含:

    rank | name
    

    自定义包含

    fieldid | userid | fieldvalue
    

    表格列匹配

    customvals.userid=members.id
    customvals.fieldid=members.rank
    

    我需要做的是匹配数据,以便在调用page.php?user = membername时显示在页面上

    Table1.membername:<br>
    Table2.name[0] - Table3.fieldvalue[0]<br>
    Table2.name[1] - Table3.fieldvalue[1]<br>
    etc...
    

    (显然只显示所述会员名的信息)

    代码越多,对我来说就越有帮助。请不要发布内部联接声明。如果您能解释解决方案的工作方式和原因,那么对我来说也是最有帮助的

    到目前为止,我的代码是:

    $profileinfocall = "SELECT Table1.`membername`, Table2.`name`, Table3.`fieldvalue`
    FROM members AS Table1
    LEFT JOIN customvals AS Table3 ON Table1.`id` = Table3.`userid`
    LEFT JOIN customfields AS Table2 ON Table3.`fieldid` = Table2.`rank`
    WHERE Table1.`membername` = $username;";
    
    $membercall = "SELECT * FROM members WHERE membername=$username";
    
    $profileinfo = mysql_query($profileinfocall, $membercall);
    
    while($row = mysql_fetch_array($profileinfo)) {
    echo $row['membername'];
    }
    

    显然,当我收到以下错误时,这并不起作用:

    警告:mysql_query()期望参数2是资源,第534行给出字符串。

    警告:mysql_fetch_array()期望参数1为资源,在第535行中给出null

2 个答案:

答案 0 :(得分:1)

这个SQL查询应该这样做:

SELECT a.membername, a.Id, b.fieldid, b.userid, b.fieldvalue, c.rank, c.name
FROM members AS a
LEFT JOIN customvals AS b ON a.id = b.userid
LEFT JOIN customfields AS c ON b.rank = c.fieldid
WHERE a.Id = #MEMBERIDHERE#;

答案 1 :(得分:1)

虽然这是一个非常广泛的问题,并且您没有提供任何PHP代码,但您可能希望将其分解为各个部分:

建立与数据库的连接(使用mysqli)并发送查询:

$c = mysqli_connect("localhost","user","password","db");
if (mysqli_connect_errno())
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
else {
    $result = mysqli_query($c,"SELECT * FROM members");
    while($row = mysqli_fetch_assoc($result)) {
        echo "{$row['membername']}";
    }
}
mysqli_close($c);

将你的桌子绑在一起:

最好在进入MySQL语法时以清晰的结构(包括换行符)开始。一种方法是使用某种查询框架:

SELECT tablealias.column, table2alias.field3
FROM table AS tablealias
LEFT|RIGHT|INNER JOIN table2 AS table2alias ON table.id=table2.id
WHERE (this and that = true or false, LIKE and so on...)

将其分解为您的具体问题:

SELECT Table1.`membername`, Table2.`name`, Table3.`fieldvalue`
FROM members AS Table1
LEFT JOIN customvals AS Table3 ON Table1.`id` = Table3.`userid`
LEFT JOIN customfields AS Table2 ON Table3.`fieldid` = Table2.`rank`
WHERE Table1.`Id` = 'UserID to be searched for' 

改进&amp;安全措施:

但是,除了满足眼睛之外,还有更多的东西。如果你刚刚开始,你也可以直接进入准备好的mysqli - 陈述。鉴于获取您的成员的查询,唯一的变化部分是ID。这可以用于准备好的语句,它比我们的第一个查询更安全(虽然不是那么快)。请考虑以下代码:

$sql =  "SELECT Table1.`membername`, Table2.`name`, Table3.`fieldvalue`
    FROM members AS Table1
    LEFT JOIN customvals AS Table3 ON Table1.`id` = Table3.`userid`
    LEFT JOIN customfields AS Table2 ON Table3.`fieldid` = Table2.`rank`
    WHERE (Table1.`Id` = ?)";
$c = mysqli_connect("localhost","user","password","db");
$stmt = $c->stmt_init();
if ($stmt->prepare($sql)) {
    $stmt->bind_params("i", $userid);
    $stmt->execute();
    while ($stmt->fetch()) {
        //do stuff with the data
    }
    $stmt->close();
}
$mysqli->close();