php在同时进行多个查询

时间:2017-11-24 12:46:29

标签: php wordpress mysqli while-loop

我遇到了一个问题,我似乎无法找到解决方案。

我正在使用wordpress,我正在编写一个插件,可以在管理仪表板上显示当前用户正在“工作”的项目。为此我需要2个查询但由于某种原因,在查询第二个查询时网站停止工作。

我有3张桌子

wp_posts(post_name和ID)

wp_cpm_user_role(project_id和user_id)

wp_users(ID和login_login)

其中:

wp_posts.ID = wp_cpm_user_role.project_id

并且:

wp_cpm_user_role.user_id 0 wp_users.ID

到目前为止我的代码

Function assigned_projects(){
//database information
$current_user = wp_get_current_user();
    $servername = "localhost";
    $username = "root";
    $password = "Password1";
    $dbname = "wordpress";

    //Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);

    if ($mysqli->connect_errno)
    {
            printf("Connect failed: %s\n", $mysqli->connect_error);
            exit();
    }

//First query
    $sql = "SELECT wp_posts.post_name, wp_posts.ID 
FROM wordpress.wp_posts 
INNER JOIN wp_postmeta ON wp_posts.ID = wp_postmeta.post_id 
WHERE post_type = 'cpm_project' AND meta_key = '_project_active' AND meta_value = 'yes'";
$result = $conn->query($sql);

//get login name on current user
    $user = $current_user->user_login;

    //prepare table for presentation
            echo '<table class="HOTtable">';
            echo '<tr>';
            echo '<td class="tdcenter">Project name<hr></td>';
            echo '<td class="tdright">Quick link<hr></td>';
            echo '</tr>';

    if ($result->num_rows > 0)
    {
            //output data of each row
            while($row = $result->fetch_assoc())
            {
                    $name = $row["post_name"];
                    $ID = $row["ID"];

        //create a direct link to the project url
                    $url = "https://localhost/wp-admin/admin.php?page=cpm_projects&tab=project&action=overview&pid=" . $ID;

        //second query to match users with projects
                    $sql1 = "SELECT wp_cpm_user_role.user_id, wp_cpm_user_role.project_id, wp_users.user_login FROM wordpress.wp_cpm_user_role INNER JOIN wp_users ON wp_cpm_user_role.user_id = wp_users.ID WHERE wp_cpm_user_role.project_id =" . $ID;
                    $result1 = $conn->query($sql1);

                    while($row1 = $result1->fetch_assoc())
                    {
                            if ($user == $row1["user_login"] AND $ID == $row1["project_id"])
                            {
                                    echo '<tr>';
                                    echo '<td class="tdcenter">' . $name . '<hr></td>';
                                    echo '<td class="tdright"><a href=' . $url . '>I am the link</a><hr></td>';
                                    echo '</tr>';
                            }
                    }
            }
    }
    echo '</table>';
    $conn->close;
}

1 个答案:

答案 0 :(得分:-1)

我尝试删除我的代码并从原始帖子中复制代码,现在可以正常工作......

编码令人困惑。但是,谢谢你抽出时间:)