[英]Can't query to a relational database using PHP
I'm having a problem when calling a query from MySQL using PHP. 使用PHP从MySQL调用查询时出现问题。
I have created the tables using SQL syntax on PHP my admin and it worked: 我已经在管理员php上使用SQL语法创建了表,并且可以正常工作:
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. 有人告诉我变量
$subject["id"]
没有定义,所以我不能在查询中使用变量,但是我遵循的教程非常准确,我没有做任何更改。 Here is the code: 这是代码:
Here is de index.php file: 这是de index.php文件:
!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. 您应该运行单个
JOIN
ed查询。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.