DISPLAYING DATA FROM A DATABASE

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
chris_s_22
Forum Commoner
Posts: 76
Joined: Wed Dec 31, 2008 2:05 pm

DISPLAYING DATA FROM A DATABASE

Post by chris_s_22 »

1 - do i use this code at the top of each of my page i wish to only alow access if there as been a session

Code: Select all

 
<?php
include_once 'Connect.php';
if (!is_authed()) 
{
     die ('You are not permitted to view this page, <a href="index.php">click here</a> to go back.');
}
?>
 
connect.php i use to conect to database this also does a included to functions.php

this is part of my functions.php

Code: Select all

 
function user_login($username, $password)
     // Now encrypt the data to be stored in the session
     $encrypted_id = md5($user['id']);
     $encrypted_name = md5($user['username']);
 
     // Store the data in the session
     $_SESSION['id'] = $id;
     $_SESSION['username'] = $username;
     $_SESSION['encrypted_id'] = $encrypted_id;
     $_SESSION['encrypted_name'] = $encrypted_name;
}
 
function is_authed()
{
     // Check if the encrypted username is the same
     // as the unencrypted one, if it is, it hasn't been changed
     if (isset($_SESSION['username']) && (md5($_SESSION['username']) == $_SESSION['encrypted_name']))
     {
          return true;
     }
     else
     {
          return false;
     }
}
 
lastly if i want do display info from the database where it matches the session being used
What code would if any would i use to compliment the code in the first question and what code would i enter into the table below
{code]
<html>
<head></head>
<body>
<table>
<tr>
<td>feild1 from database here</td>
<td>feild2 from database here</td>
<td>feild3 from database here</td>
</tr>
</table>
</body>
</html>
chris_s_22
Forum Commoner
Posts: 76
Joined: Wed Dec 31, 2008 2:05 pm

Re: DISPLAYING DATA FROM A DATABASE

Post by chris_s_22 »

after reading more tutorials im come to this

Code: Select all

 
<?php
include_once 'Connect.php';
if (!is_authed()) 
{
     die ('You are not permitted to view this page, <a href="index.php">click here</a> to go back.');
}
// i want this page to display the age of the session user i use * instead of age for the reason i will be displaying more than just there age
 
    $query = mysql_query("SELECT * FROM members WHERE id = '". $encrypted_id ."'");
    $result= mysql_query ($query) or die ('id and session data dont match.');
    if ($result)
    {   
    // deal with error
    }
    else
    {
 
    }
?>
 
age<br><?php echo $age; ?>
 
im getting the error message
id and session data dont match.
can any one point out what im doing wrong?
User avatar
N1gel
Forum Commoner
Posts: 95
Joined: Sun Apr 30, 2006 12:01 pm

Re: DISPLAYING DATA FROM A DATABASE

Post by N1gel »

Are you starting a session?

Might be helpful to read the manual on session_start

session_start
chris_s_22
Forum Commoner
Posts: 76
Joined: Wed Dec 31, 2008 2:05 pm

Re: DISPLAYING DATA FROM A DATABASE

Post by chris_s_22 »

in my connect.php
i use session_start(); at top of page

On any page that i want to have session details or connect to my database i include the connect.php. Or do i still have to use session_start(); on page?

my code im using is

Code: Select all

<?php
include_once 'Connect.php';

if (!is_authed()) 
{
     die ('You are not permitted to view this page, <a href="index.php">click here</a> to go back.');
}
$thequery = ("SELECT * FROM members WHERE id = '". $_SESSION['id'] ."'");
$query = mysql_query($thequery) or die ('id and session data dont match.');

?>
<html><head></head><body>
age<br><?php echo $age; ?>
</body>
</html>

Code: Select all

im no longer getting the error 'id and session data dont match'
but still not echoing the line <?php echo $age; ?>
chris_s_22
Forum Commoner
Posts: 76
Joined: Wed Dec 31, 2008 2:05 pm

Re: DISPLAYING DATA FROM A DATABASE

Post by chris_s_22 »

can anyone see what im doing wrong?
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: DISPLAYING DATA FROM A DATABASE

Post by papa »

You are not defining age.

Try something similar:

Code: Select all

 
<html><head></head><body>
age<br>
<?php
while ($row = mysql_fetch_assoc($query)) {
    echo $row["age"];
}
 ?></body>
</html>
http://us2.php.net/manual/en/function.m ... -assoc.php
chris_s_22
Forum Commoner
Posts: 76
Joined: Wed Dec 31, 2008 2:05 pm

Re: DISPLAYING DATA FROM A DATABASE

Post by chris_s_22 »

1) connect.php page contains session_start(); Do i still need to use this line in my script or because i included connect.php i dont have to?

The only things i want this page to do is
- only allow access if logged in
- get all info from database where it matches sessiona data
- display data on page from within the database

Can anyone help me find out which part of my script im not telling it to do the right way

after messing arround trying lots of things ive come up with the following code
However im just getting a blank page

Code: Select all

 
<?php
include_once 'Connect.php';
 
if (!is_authed()) 
{
     die ('You are not permitted to view this page, <a href="index.php">click here</a> to go back.');
}
    $thequery = ("SELECT * FROM members WHERE id = '". $_SESSION['id'] ."'");
    $query = mysql_query($thequery) or die ('id and session data dont match.');
    while ($row = mysql_fetch_assoc($query)) 
{          
$photo = $row["photo"];
 
}
echo "photo<br>";
echo "$photo"; 
?>
 
chris_s_22
Forum Commoner
Posts: 76
Joined: Wed Dec 31, 2008 2:05 pm

Re: DISPLAYING DATA FROM A DATABASE

Post by chris_s_22 »

if i
echo "<h5>$thequery</h5>";
i get the result that displays this on the page

SELECT * FROM members WHERE id = ''

if i
echo "<h5>$query</h5>";

i get
Resource id #17

does that help?
Post Reply