drzablspw01655860 2015-07-03 16:05
浏览 43
已采纳

用php从数据库获取信息

I'm making API to simple forum ,, Now trying to get the information from the Database and show it

on the control page : 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>

The Result ===> 'problem'


The 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);

?>

i expected it will show the information

i think the function tinyf_forums_get() is causing that

  • 写回答

1条回答 默认 最新

  • doukong6031 2015-07-03 16:12
    关注

    Your code is broken:

    You define an array, then never use it:

    $forums = array(); 
    
        $users[count($forums)] = mysqli_fetch_object($qresult);
         ^^^^^---undefined, never returned, never used otherwise, therefore useless.
    
    return $forums;
           ^^^^^^---returning permanently empty array
    

    and since $forums is an empty array:

    php > $x = array();
    php > var_dump($x == null);
    bool(true)
    

    You probably want

    if (count($forums) == 0)
    

    instead.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭