0

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

0

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

yes really thanks that's happened because i copy some functions from the api for users . another api file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.