View unanswered posts | View active topics It is currently Sun Aug 07, 2022 8:41 pm



Reply to topic  [ 5 posts ] 
 DIsplaying variables from the database 
Author Message
Junior Member

Joined: Fri May 14, 2004 5:52 pm
Posts: 11
Post 
Hey guys,

Could any1 gimme an example of how to display a variable after pullin it out of the database.??

Thanx,

Desperately,
Mishmia :rolleyes:


Fri May 14, 2004 5:55 pm
Report this post
Profile
Junior Member

Joined: Thu Apr 29, 2004 4:49 pm
Posts: 5
Post 
I'm going to assume MySQL, but the theory/code is similar for any database.

Code:

<?php

$link = mysql_connect("server", "username", "passwd"); //server prolly=localhost, then user/pass..
$db = mysql_select_db("dbname", $link);

//example table structure is...
//id (primary/unique), first_name, last_name

$result = mysql_query("SELECT * FROM table WHERE id=1", $link);

/*Now there are several different approaches here, so you might want to give more detail, but with this extremely
simple table structure, I'm just going to grab the 1 row as an array*/

$row = mysql_fetch_array($result, MYSQL_BOTH); //mysql_both grabs the array as associative (column names) and enumerated (0, 1, 2)

echo $row['id]
.$row['first_name']
.$row[2]; //the both means you can use either column name or 0 based index (last_name is the 3rd column, so 2)

mysql_close($link);
?>



I didn't add in any error handling, but that's a pretty simple way to do it with some explanation...if ya need more detail/help, just let us know...


Sat May 15, 2004 9:59 am
Report this post
Profile WWW
Junior Member

Joined: Fri May 14, 2004 5:52 pm
Posts: 11
Post 
Thanks!

When a user logs in, I want the webpage to say " Hello User". I m unable to do this..Here is my code...I dunno whats the error...

Login Page Code:

<?
session_start();
include 'dbinfo.php';
// Conver to simple variables
$username = $_POST['username'];
$password = $_POST['password'];

if((!$username) || (!$password)){
echo "Please enter ALL of the information!
";
include 'index.php';
exit();
}

$password = md5($password);

$sql = mysql_query("SELECT * FROM users WHERE username='$username' AND password='$password'");
$login_check = mysql_num_rows($sql);

if($login_check > 0){
while($row = mysql_fetch_array($sql)){
foreach( $row AS $key => $val ){
$$key = stripslashes( $val );
}
// Register some session variables!
session_register('username');
$_SESSION['username'] = $username;
session_register('email');
$_SESSION['email'] = $email;

mysql_query("UPDATE users SET last_login=now() WHERE userid='$userid'");
header("Location: login_success.php");
}
} else {
echo "<font color=red>You could not be logged in! The username and passsssword do not match!</font>

Please try again!
";
include 'index.php';
}
?>

Login Success Page : ( When the user successfully logs in the system...)

<?php
require 'config.php';
session_start();
echo "Welcome ". $_SESSION['username'] ."!

";
?>

Why am i just getting just the "Welcome !" message (

Thanks again


Sat May 15, 2004 10:05 am
Report this post
Profile
Junior Member

Joined: Thu Apr 29, 2004 4:49 pm
Posts: 5
Post 
That looks like a session issue...I have'nt messed with sessions in awhile, but you only need to use session_register OR $_SESSION (so prolly just use $_SESSION).

Maybe run a phpinfo() and see if the session variables are active on the success page/script...it has the session/post traces near the bottom.


Sat May 15, 2004 10:14 am
Report this post
Profile WWW
Junior Member

Joined: Fri May 14, 2004 5:52 pm
Posts: 11
Post 
Hey cristofv

Thank u very much :)

I tried around with the code u sent first and now i m popping up the values from the database. Thanks.

I still havent figured out this session thingie yet :rolleyes: Guess wud look at it later :D

Thanks man, I appreciate it
C u around


Sat May 15, 2004 5:51 pm
Report this post
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 5 posts ] 

Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
© Copyright 2003-2008 www.php-editors.com. The ultimate PHP Editor and PHP IDE site.