我正在为简单的论坛制作API, 现在尝试从数据库中获取信息并显示它
控制页面上的: showForums.php
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>TheForums</title>
</head>
<body>
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
require_once('fourmsAPI.php');
/*
function tinyf_forums_get($extra ='')
{
global $tf_handle;
$query = sprintf("SELECT * FROM `forums` %s",$extra );
$qresult = mysqli_query($tf_handle, $query);
if (!$qresult)
return NULL;
$recount = mysqli_num_rows($qresult);
if ($recount == 0)
return NULL ;
$forums = array();
for($i = 0 ; $i < $recount ; $i++)
$users[count($forums)] = mysqli_fetch_object($qresult);
//mysql_free_result($qresult);
return $forums;
}
*/
$forums = tinyf_forums_get();
if($forums == NULL)
{
die('problem');
}
$fcount = count($forums);
if($fcount == 0)
{
die('No Forums ');
}
?>
<ul type = "square">
<?php
for($i = 0 ; $i < $ucount ; $i++)
{
$forum = $forums[$i];
echo "<li><a href = \"forums.php?id=$forum->id\"> $forum->title <a/> <br/> $forum->desc <br/> </li>"; //$array ->
}
?>
</ul>
</body>
</html>
结果===&gt; &#39;问题&#39;
Apifile:
fourmsAPI.php
<?php
//Forums APIs
function tinyf_forums_get($extra ='')
{
global $tf_handle;
$query = sprintf("SELECT * FROM `forums` %s",$extra );
$qresult = mysqli_query($tf_handle, $query);
if (!$qresult)
return NULL;
$recount = mysqli_num_rows($qresult);
if ($recount == 0)
return NULL ;
$forums = array();
for($i = 0 ; $i < $recount ; $i++)
$users[count($forums)] = mysqli_fetch_object($qresult);
//mysql_free_result($qresult);
return $forums;
}
function tinyf_forums_get_by_id($fid)
{
$id = (int)$fid;
if($fid == 0 )
return NULL ;
$result = tinyf_forums_get('WHERE id ='.$id);
if($result == NULL)
return NULL;
$forum = $result[0];
return $forum;
}
//get result is array()
function tinyf_forums_get_by_name($name)
{
global $tf_handle;
$n_name = mysqli_real_escape_string($tf_handle, strip_tags($name));
$result = tinyf_users_get("WHERE `name` = '$n_name'");
if ($result != NULL){
$user = $result[0];
}
else{
$user = NULL;
}
return $user ;
}
function tinyf_forums_get_by_email($email)
{
global $tf_handle;
$n_email = mysqli_real_escape_string($tf_handle, strip_tags($email));
$result = tinyf_users_get("WHERE `email` = '$n_email' ");
if ($result != NULL)
{
$user = $result[0];
}
else{
$user = NULL ;
}
return $user ;
}
function tinyf_forums_add($title,$desc)
{
global $tf_handle;
if ((empty($title)) || (empty($desc)))
return false;
$n_title = mysqli_real_escape_string($tf_handle, strip_tags($title));
$n_desc = mysqli_real_escape_string($tf_handle, strip_tags($desc));
$query = sprintf("INSERT INTO `forums` VALUE(NULL,'%s','%s')",$n_title,$n_desc);
$qresult = mysqli_query($tf_handle, $query);
if(!$qresult)
return false;
return true;
}
function tinyf_forums_delete($fid)
{
global $tf_handle;
$id = (int)$fid;
if($id == 0 )
return false ;
tinyf_forums_delete_all_posts($fid);
$query = sprintf ("DELETE FROM `forums` WHERE `id`= %d",$id);
$qresult = mysqli_query($tf_handle, $query);
if(!$qresult)
return false;
return true;
}
function tinyf_forums_update($fid,$title = NULL,$desc = NULL)
{
global $tf_handle;
$id = (int)$uid;
if($id == 0 )
return false ;
$forum = tinyf_forums_get_by_id($id);
if(!$forum)
return false;
if ((empty($title)) && (empty($desc)))
return false;
$fields = array() ;
$query = 'UPDATE `forums` SET ' ;
if(!empty($title))
{
$n_title = mysqli_real_escape_string($tf_handle, strip_tags($title));
$fields[count($fields)] = "`title` = '$n_title'";
}
if(!empty($desc))
{
$n_name = mysqli_real_escape_string($tf_handle,strip_tags($name));
$fields[count($fields)] = "`desc` = '$n_desc'";
}
for($i = 0; $i < $fcount ; $i++)
{
$query .= $fields[$i];
if($i != ($fcount - 1)) // i = 0 that the first element in the array .. 2 will be - 1 last 3shan hwa by3ed el array mn wa7ed :D
$query .=' , ';
}
$query .= ' WHERE `id` = '.$id;
$qresult = mysqli_query($tf_handle, $query);
if(!$qresult)
return false;
else
return true;
}
function tinyf_forums_delete_all_posts($fid)
{
global $tf_handle;
$id = (int)$fid;
if($id == 0){
return false;
}
$forums = tinyf_forums_get_by_id($id);
if(!$forum){
return false;
}
$topicsq = sprintf('SELECT * FROM `posts` WHERE `fid` = %d',$id) ;
$tresult = mysqli_query($tf_handle,$topicsq);
if(!$tresult){
return false;
}
$tcount = mysqli_num_rows($result);
for($i = 0; $i<$tcount ; $i++){
$topic = mysqli_fetch_object($tresult);
mysqli_query($tf_handle,'DELETE FROM `posts` WHERE `pid` = '.$topic ->id);
mysqli_query($tf_handle,'DELETE FROM `posts` WHERE `id` = '.$topic ->id);
}
mysqli_free_result($tresult);
return true ;
}
include ('db.php') ;
error_reporting(E_ALL);
ini_set('display_errors', 1);
?>
我预计会显示信息
我认为函数tinyf_forums_get()正在导致
答案 0 :(得分:1)
您的代码已损坏:
您定义一个数组,然后永远不要使用它:
$forums = array();
$users[count($forums)] = mysqli_fetch_object($qresult);
^^^^^---undefined, never returned, never used otherwise, therefore useless.
return $forums;
^^^^^^---returning permanently empty array
并且$forums
是一个空数组:
php > $x = array();
php > var_dump($x == null);
bool(true)
你可能想要
if (count($forums) == 0)
代替。