Mysqli select multiple queries

nviens

I'm trying to select one query by using the user_id to get the categories specific to the user and then from there use the results from the first query to select data from a second table in a second query. The data from the second query I would like to display in a drop down menu. Is there any easy way to do this? I'm new to using mysqli so please bear with me.

I have the following function but it's not displaying anything in the dropdown menu, it is showing the drop down menu though.

function get_classes($mysqli) {
     if(isset($_SESSION['user_id'], $_SESSION['username'], $_SESSION['login_string'])) {
    $user_id = $_SESSION['user_id'];
    if ($stmt = $mysqli->prepare("SELECT category_id 
                            FROM questions WHERE user_id = ?")) {
        $stmt->bind_param('i', $user_id);
        $stmt->execute();
        $stmt->store_result();

        if($stmt->num_rows>0) {
            $stmt->bind_result($category_id);
            $stmt->fetch();
            if($result = $mysqli->prepare("SELECT category_id, category_name
                            FROM category WHERE category_id = ?")) {
                $result->bind_param('s', $category_id);
                $result->execute();
                $result->store_result();
            }
        }
        echo "<select id='classes' name='classes'>";
        while ($row = $result->fetch_assoc()) {
            unset($user_id, $category_name);
            $category_id = $row['category_id'];
            $category_name = $row['category_name'];
            echo '<option value="'.$category_id.'">'.$category_name.'</option>';
        }
        echo "</select>";
    }

}
}

I'm calling the function from another page with the following code:

<?php
            get_classes($mysqli);

            ?>
Kevin

Alternatively, you could join them instead, then the normal fetching. Example:

function get_classes($mysqli) {
    if(!isset($_SESSION['user_id'], $_SESSION['username'], $_SESSION['login_string'])) {
        return false;
    }

    $user_id = $_SESSION['user_id'];

    $sql = '
        SELECT
        category.category_id,
        category.category_name

        FROM category
        JOIN questions
        ON questions.category_id = category.category_id

        WHERE questions.user_id = ?
    ';
    $stmt = $mysqli->prepare($sql);
    $stmt->bind_param('i', $user_id);
    $stmt->execute();
    $result = $stmt->bind_result($category_id, $category_name);

    // printing
    echo '<select name="classes" name="classes">';
    while($row = $stmt->fetch()) {
        echo '<option value="'.$category_id.'">'.$category_name.'</option>';
    }
    echo '</select>';
}

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Error when trying to run multiple mysqli queries

분류에서Dev

conditional statements to run mysqli queries

분류에서Dev

mysqli select 오류

분류에서Dev

MySQLi, SELECT FROM, PHP

분류에서Dev

mysqli queries not returning results inside function

분류에서Dev

Multiple Queries Parse Javascript

분류에서Dev

Multiple queries in WHERE clause

분류에서Dev

mysqli to pdo simple select 문

분류에서Dev

Upload multiple images to Database MYSQLI

분류에서Dev

Mysqli - Are parameterized queries enough for preventing XSS second order attacks?

분류에서Dev

SQL Queries from multiple tables

분류에서Dev

PHP MySQLi 준비된 문-SELECT

분류에서Dev

MySqli query select all fields are not null

분류에서Dev

NHibernate - Eager load graphs of objects with multiple queries

분류에서Dev

Is it ever "ok" to use multiple queries instead of one?

분류에서Dev

Add to Dictionary Query Results of Multiple Linq Queries

분류에서Dev

Multiple Queries in one Report (Visual Studio 2005)

분류에서Dev

Multiple sql queries vs List of data

분류에서Dev

Combine results of multiple queries in Oracle Sql

분류에서Dev

준비된 문 mysqli SELECT * FROM users

분류에서Dev

mysqli_connect 및 mysql_select_db 사용

분류에서Dev

I get error when i use mysqli select

분류에서Dev

PHP : "select *"가 포함 된 Mysqli 준비 문

분류에서Dev

Creating multiple while loops for one array in php and mysqli

분류에서Dev

MySQL SELECT queries of same limits getting slower and slower

분류에서Dev

Hibernate firing a large number of select queries while saving associated objects

분류에서Dev

MySQL Merge Two SELECT Queries With CASE On One Table

분류에서Dev

Node.js - Building JSON asynchronously with multiple queries

분류에서Dev

Return data to from multiple queries in a single structure in Oracle

Related 관련 기사

뜨겁다태그

보관