简体   繁体   中英

Can't query to a relational database using PHP

I'm having a problem when calling a query from MySQL using PHP.

I have created the tables using SQL syntax on PHP my admin and it worked:

CREATE TABLE subjects(
id INT(11) NOT NULL AUTO_INCREMENT,
menu_name VARCHAR(30) NOT NULL,
position INT(3) NOT NULL,
visible TINYINT(1) NOT NULL,
PRIMARY KEY (id)
);

CREATE TABLE pages(
id INT(11) NOT NULL AUTO_INCREMENT,
subject_id INT(11) NOT NULL,
menu_name VARCHAR(30) NOT NULL,
position INT(3) NOT NULL,
visible TINYINT(1) NOT NULL,
content TEXT,
PRIMARY KEY (id),
INDEX (subject_id)
);

Then insert data on both tables with the following corresponding:

INSERT INTO subjects (menu_name, position, visible)
VALUES ('About', 1, 1);

INSERT INTO pages (subject_id, menu_name, position, visible, content)
VALUES (1, 'Our Mission', 1, 1, 'Our Mission has always been...');

Keep inserting data in both tables.

When I try to do a query to the second table it just forgets to call the second query for the pages inside the subjects of the menu. I've been told the variable $subject["id"] is not defined so I can't use variables on the query, but the tutorial I'm following is very accurate and I haven't change anything. Here is the code:

Here is de index.php file:

!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>

     <?php 
        $dbuser = 'root'; 
        $dbpass = 'root'; 
        $db = 'cms'; 
        $dbhost = 'localhost';
        $dbport = '3306';


        $link = mysqli_connect($dbhost,$dbuser,$dbpass,$db) or die("Error " . mysqli_error($link));

        $subject_set = "SELECT * FROM subjects WHERE visible = 1" or die("Error in the consult.." . mysqli_error($link));
        $result = mysqli_query($link, $subject_set); 

    ?>
        <ul>

        <?php
        while($subject = mysqli_fetch_assoc($result)){ ?>
            <li><?php echo $subject["menu_name"] ?>

                <?php
                    $query = "SELECT * ";
                    $query .= "FROM pages ";
                    $query .= "WHERE visible = 1 ";
                    $query .= "AND subject_id = {$subject["id"]} "; // VARIABLE ON MYSQL NOT DECLARED
                    $query .= "ORDER BY position ASC";

                    $page_set = mysqli_query($connection, $query);
                ?>
                <ul class="pages">
                    <?php
                    while($page = mysqli_fetch_assoc($page_set)){ ?>
                    <li><?php echo $page["menu_name"] ?></li>
                    <?php   
                    }
                    ?>
                </ul>

            </li>
        <?php   
        }
        ?>

        </ul>


<?php


        mysqli_free_result($result);

        mysqli_close($link);
?>
</body>
</html>
$string = "..." or die();

is utterly pointless. You're not executing your query. You're just defining a string which CONTAINS the query string. As such it's impossible for you to detect a database-level error at that point.

Your code should be:

$subject_set = "SELECT ...";
$result = mysqli_query(...) or die(mysqli_error($link));

You also fail to check for errors in your inner query. NEVER assume a query will succeed. Always assume it'll fail, check for that failure, and treat success as a pleasant surprise.

And in the grand-scheme of things, running nested queries as you are is very inefficient. You should be running a single JOIN ed query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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