无法使用内部联接和自定义获取 post 函数从所有三个 table 获取所有 post
Unable to get all posts from all three table using inner join and custom get post function
我无法从这三个 table“帖子、类别、用户”中获取所有帖子。当我尝试加载页面 index.php:
时出现以下错误
Here are the page structure and code : index.php
<?php
include_once('resourses/init.php');
$posts = getPosts();
?>
<html>
<head>
<title>First Page</title>
</head>
<body>
<?php
foreach($posts as $post){
?>
<a href=""><?php echo $post['title'];?></a>
<?php
}
?>
</body>
</html>
Second php file is : init.php
<?php
//error_reporting(0);
include_once('config.php');
include_once('func.php');
?>
Third file of init.php -> config.php
<?php
$config['db_host'] = 'localhost';
$config['db_user'] = 'root';
$config['db_pass'] = 'usbw';
$config['db_name'] = 'cblog';
foreach($config as $key => $value){
define(strtoupper($key), $value);
}
$db = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
if(!$db){
echo "Unable to connect due to: ".mysqli_connect_error($db);
}
?>
And last file of init.php -> func.php
<?php
function getPosts(){
$query ="
SELECT posts.id, posts.cat_id, posts.user_id, posts.title, posts.contents, posts.date_posted, categories.name, users.name FROM posts
INNER JOIN categories ON posts.id=categories.id
INNER JOIN users ON posts.user_id=users.id
ORDER BY posts.id DESC";
$runQuery = mysqli_query($db, $query);
while($row = mysqli_fetch_assoc($runQuery)){
$posts[] = $row;
}
}
?>
When i browse the index.php page it show some errors, plz see
screenshot mentioned below:
如错误所述,init.php 中的 $db 参数在 func.php 脚本中无法识别。有没有理由把它们分开,如果不把它们放在一个文件中,那是有道理的。
然后在 func.php 中数组 $posts[] 没有被初始化。执行 $posts = array();
然后 array_push($row);
这应该可以修复错误。
我无法从这三个 table“帖子、类别、用户”中获取所有帖子。当我尝试加载页面 index.php:
时出现以下错误Here are the page structure and code : index.php
<?php
include_once('resourses/init.php');
$posts = getPosts();
?>
<html>
<head>
<title>First Page</title>
</head>
<body>
<?php
foreach($posts as $post){
?>
<a href=""><?php echo $post['title'];?></a>
<?php
}
?>
</body>
</html>
Second php file is : init.php
<?php
//error_reporting(0);
include_once('config.php');
include_once('func.php');
?>
Third file of init.php -> config.php
<?php
$config['db_host'] = 'localhost';
$config['db_user'] = 'root';
$config['db_pass'] = 'usbw';
$config['db_name'] = 'cblog';
foreach($config as $key => $value){
define(strtoupper($key), $value);
}
$db = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
if(!$db){
echo "Unable to connect due to: ".mysqli_connect_error($db);
}
?>
And last file of init.php -> func.php
<?php
function getPosts(){
$query ="
SELECT posts.id, posts.cat_id, posts.user_id, posts.title, posts.contents, posts.date_posted, categories.name, users.name FROM posts
INNER JOIN categories ON posts.id=categories.id
INNER JOIN users ON posts.user_id=users.id
ORDER BY posts.id DESC";
$runQuery = mysqli_query($db, $query);
while($row = mysqli_fetch_assoc($runQuery)){
$posts[] = $row;
}
}
?>
When i browse the index.php page it show some errors, plz see screenshot mentioned below:
如错误所述,init.php 中的 $db 参数在 func.php 脚本中无法识别。有没有理由把它们分开,如果不把它们放在一个文件中,那是有道理的。
然后在 func.php 中数组 $posts[] 没有被初始化。执行 $posts = array(); 然后 array_push($row);
这应该可以修复错误。