mysql assoc_数组的PHP函数
在一个新项目中,我试图通过将大多数操作存储为函数来减少重复代码,从而提高我的技能 以前我所有的代码都在每个PHP文件的头中(一种la Dreamweaver风格) 在这个新项目中,我有一个页面需要拉一个关联数组来显示博客 它目前正在失败。以下是我所拥有的: DB.phpmysql assoc_数组的PHP函数,php,mysql,Php,Mysql,在一个新项目中,我试图通过将大多数操作存储为函数来减少重复代码,从而提高我的技能 以前我所有的代码都在每个PHP文件的头中(一种la Dreamweaver风格) 在这个新项目中,我有一个页面需要拉一个关联数组来显示博客 它目前正在失败。以下是我所拥有的: DB.php <?php $servername = "localhost"; $username = "USERNAME"; $password = "*******"; $dbname = "dbname"; // Create c
<?php
$servername = "localhost";
$username = "USERNAME";
$password = "*******";
$dbname = "dbname";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
?>
function getAllBlogs()
{
$sql = "SELECT * FROM blog ORDER BY id DESC ";
$query = mysqli_query($conn, $sql);
$result = mysqli_fetch_assoc($query);
return $result;
}
<?php include("../Connections/db.php");?>
<?php include("functions.php");?>
<?php
$result = getAllBlogs();
foreach($result as $blogs) {
echo "/".$blogs['id']."_".seoUrl($blogs['title']);
}
?>
function getAllBlogs()
{
global $conn; // -> to get access to the variable, which is outside the method
$sql = "SELECT * FROM blog ORDER BY id DESC ";
$query = mysqli_query($conn, $sql);
$result = mysqli_fetch_assoc($query);
return $result;
}
blog.php
<?php
$servername = "localhost";
$username = "USERNAME";
$password = "*******";
$dbname = "dbname";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
?>
function getAllBlogs()
{
$sql = "SELECT * FROM blog ORDER BY id DESC ";
$query = mysqli_query($conn, $sql);
$result = mysqli_fetch_assoc($query);
return $result;
}
<?php include("../Connections/db.php");?>
<?php include("functions.php");?>
<?php
$result = getAllBlogs();
foreach($result as $blogs) {
echo "/".$blogs['id']."_".seoUrl($blogs['title']);
}
?>
function getAllBlogs()
{
global $conn; // -> to get access to the variable, which is outside the method
$sql = "SELECT * FROM blog ORDER BY id DESC ";
$query = mysqli_query($conn, $sql);
$result = mysqli_fetch_assoc($query);
return $result;
}
BLOG.php上的第33行是
foreach($result as $blogs) {
functions.php上的第17行和第18行是
$result1 = mysqli_query($conn, $sql);
$result = mysqli_fetch_assoc($result1);
方法getAllBlogs()没有用于返回值的变量
<?php
$result = getAllBlogs();
foreach($result as $blogs)
{
echo "/".$blogs['id']."_".seoUrl($blogs['title']);
}
?>
您忘记将其赋给$result变量
<?php
$result=getAllBlogs();// assign to $result variable
foreach($result as $blogs) {
echo "/".$blogs['id']."_".seoUrl($blogs['title']);
} ?>
首先在blog.php中包含functions.php文件。请使用下面的代码
<?php
require_once 'functions.php';
$result = getAllBlogs();
foreach($result as $blogs)
{
echo "/".$blogs['id']."_".seoUrl($blogs['title']);
}
?>
您收到了什么信息?请在此共享。在blog.php中包含functions.php并检查。编写include_once('functions.php');在blog.php之上,使用它可能是一个问题1。需要是$result=getAllBlogs()代码>2。当您觉得某个答案为您提供了解决方案,包括您所问的所有其他问题时,您只获取了一行便笺:。谢谢。我编辑了我的问题以包含更多信息和错误。谢谢。我编辑了我的问题以包含更多信息和错误。谢谢。我编辑了我的问题以包含更多信息和错误。