简体   繁体   中英

How to fetch Record In html using more than one php class

Hello everyone I am trying to access the data from mysql to html page. For this I create these classes

HomeManager.php

<?php
 session_start();
class HomeManager
{
    function fetchUser($arr)
    {   $userid=$_SESSION['uid'];
        require_once(FRONT_ROOT_PATH.'DatabaseManager.php');
        $query ="Select * from tbusers where userid='".$userid."'";
        $db= new DatabaseManager();
        $result=$db->executeQuery($query);
        return $result;
    }
}

Homeinit.php

<?php
include(LIB_PATH."Home/HomeManager.php");
    if(isset($SESSION['uid']))
    {
        $obj=new HomeManager();
        $user=$obj->fetchUser($_POST)
        if(Count($user)>0)
        {
            $username=$user['username'];
        }

    }

?>

And I am using this class on this html page

Home.php

     <?php
include('../../Include/config.inc.php');
include(LIB_PATH."Home/Homeinit.php");

?>
    <html>
    <head>
    </head>
    <body>
    <div>
    <input type="name" value=".$username."
    </div>
    </body>
    </html>

I am at learning stage of PHP. Experts Please Help me how can I Fetch Record from mysql database and show it on my html page

PHP is server side and HTML is markup, so to print anything on browser which comes via server side, you have to make that server-side value visible to your browser, which is otherwise blind to server side values.

Using PHP , you will have to echo the result of PHP in HTML to make it visible to browser,like :

<input type="name" value="<?php echo $username ?> " > 
<!-- notice the opening   ^^ and closing tags  ^^   of php -->

Also,

$user=$obj->fetchUser($_POST)

You have a semi-colon missing too on this line....add ;

This should do!! :)

You can do it in different ways.

You can use the php code in between HTML also.

So you can echo the required html string using PHP so that it will properly render at the client side.

 <?php echo "<input type='name' value='".$username."'/>" ?>

You can also use AJAX to populate the value field dynamically.

Note : In your case, it will be better to keep the variable $username as a session variable so that it will be accessible in all your files.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM