2

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

NoobEditor
  • 15,563
  • 19
  • 81
  • 112
Azad chouhan
  • 167
  • 2
  • 19

2 Answers2

2

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!! :)

NoobEditor
  • 15,563
  • 19
  • 81
  • 112
  • @Azadchouhan : yeah..that i saw....only possible reason i could think of is that, your `if` condition is failing, may be thats why `$username` is not defined....do one thing, in that same `if`, add `else` condition and add => `else{die("no val");}`, run and then post your output here...i feel you'll get `else` output!! – NoobEditor Mar 04 '14 at 07:24
  • Parse error: syntax error, unexpected 'if' (T_IF) in C:\wamp\www\ProjectDream\Controller\Home\Homeinit.php on line 7 – Azad chouhan Mar 04 '14 at 07:28
  • hahaha lolzz yeah I add else part sir but can you tell me why i am getting this error. See I am learning php at home alone.. and only stackoverflow is my teacher right now :) – Azad chouhan Mar 04 '14 at 07:33
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/48931/discussion-between-noobeditor-and-azad-chouhan) – NoobEditor Mar 04 '14 at 07:34
  • @Azadchouhan : come on chat using above link – NoobEditor Mar 04 '14 at 08:16
  • @Azadchouhan : come on chat – NoobEditor Mar 04 '14 at 11:50
2

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.

Nidhin Joseph
  • 1,461
  • 3
  • 15
  • 32
  • but what if i have more than one variable i have to use session for every variable – Azad chouhan Mar 04 '14 at 07:21
  • 1
    Yes sure. You can keep all the basic things like user name, user id,email id as SESSION. So that you don't have to query for it every time. – Nidhin Joseph Mar 04 '14 at 07:22
  • Do you dnt think sir it is a bad practice ???? Store every single value in session???? – Azad chouhan Mar 04 '14 at 07:24
  • @Azadchouhan : `session` values are preferred only for maintaining session for a particular `user` or a `thread`, so yes, its kind of bad practice storing all values in `session` when u can store it in `variables`!! :) – NoobEditor Mar 04 '14 at 07:28
  • If you are using something very regularly at different places, it is better to keep it as session instead of querying it every time. And you have to destroy the session when it is no longer needed. PHP is proving methods for deleting the whole session and also a single session variable. – Nidhin Joseph Mar 04 '14 at 07:30
  • 1
    @NoobEditor you are almost right. But this link is giving a good answer for our confusion regarding the same. The SESSION can also be used to improve efficiency. But for sure, that doesn't means storing everything in session. http://stackoverflow.com/questions/17554990/session-variables-how-much-data-is-too-much – Nidhin Joseph Mar 04 '14 at 07:44
  • @NidhinJoseph : i have always found `session-data` very head-banging plus space consuming....but your link is useful, SO has awesome repositories!! :) – NoobEditor Mar 04 '14 at 07:51