Jump to content

Archived

This topic is now archived and is closed to further replies.

pocobueno1388

Having Problems Calling Specific Data From The Database.

Recommended Posts

I am making a site that requires you to have a membership. So the users must have there own account, which requires a username and password. I think you guys understand what I am saying.

I have the entire membership system set up and working, but I am having problems displaying information from the database. I want to display that specific person's information when they are logged in. Say there was a page that they could go to when they were logged in and it said something like:

Name:
Age:
Height:
Birthday:

I want it to display that persons data that I have saved in the database. But if someone else was logged into a different account, it would display their information.

When they login it registers their loginname and password as a session...but I am not sure how to use that in calling the information from the database. I have tried something like this:

[code]
$loop = mysql_query("SELECT * FROM players WHERE loginname=$loginname and password=$password")
or die ('cannot select people');

while ($row = mysql_fetch_array($loop)){

$username = $row['username'];
$money = $row['money'];



} //end loop

print "Username: $username";
print "<p>";
print "Money: $money";
[/code]

But that doesn't get what I want. Could someone please push me in the right direction? Thanks.

Share this post


Link to post
Share on other sites
hmm, should be working, although i dont believe the password = $password bit needs to be there...

also you shouldnt need a while statement there if im correct, try something like this:

[code]
$loop = mysql_query("SELECT * FROM players WHERE loginname=$loginname")
or die('cannot select people');

$row = mysql_fetch_array($loop))

$username = $row['username'];
$money = $row['money'];


print "Username: $username";
print "<p>";
print "Money: $money";
[/code]

UNTESTED

Share this post


Link to post
Share on other sites
How about...

[code]

// Retrieve all the data from the table
$result = mysql_query("SELECT * FROM players WHERE loginname=$loginname and password=$password")
or die(mysql_error());

// store the record of the table into $row
$row = mysql_fetch_array( $result );

echo "$row[username]";
echo "<br>";
echo "$row[money]";

[/code]

Share this post


Link to post
Share on other sites
localhost - Yours just gave me a blank screen.

realjumper - I think you are getting somewhere. I got an error like this:

[code]
You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'and password=052588' at line 1
[/code]

It grabbed the password from the database...?

I tried logging into a different account with different database information and it still grabbed the same password. It is just grabbing the information from the first row in the database for some reason.

Share this post


Link to post
Share on other sites
i only say my thinking....

Colin1388's method like "loginname=$loginname and password=$password" is not safe....

i think the username is not repeat...when someone register their information you must dispost the repeat

one

about SQL syntax i often use PHPMYADMIN is a good tool you can write SQL then exec that in PHPMYADMIN

the use information will show for you ....

my english is very poor i wish you can understand what i say...

Share this post


Link to post
Share on other sites
Yong - I do use PHPMYADMIN. I am not quite sure what you are trying to say though. How is it not safe? And what other way do you suggest doing it?

Share this post


Link to post
Share on other sites
the part password=$password needs to have single quotes. password = '$password' and the same for username.

Share this post


Link to post
Share on other sites
php way
[code]
<?php

$loop = "SELECT * FROM players WHERE loginname='$loginname' and password='$password' ";

$result=mysql_query($loop);

while ($row = mysql_fetch_assoc($result)){

$username = $row['username'];
$money = $row['money'];


echo $username;
echo"<br>";
echo $money;
}
?>

[/code]

php and html way

[code]
<?php

$loop ="SELECT * FROM players WHERE loginname='$loginname' and password='$password' ";

$result=mysql_query($loop);

while ($row = mysql_fetch_assoc($result)){

$username = $row['username'];
$money = $row['money'];

?>

<html>
<body>

<?php echo $username?>
<br>
<?php echo $money?>

<?php }>
[/code]

Share this post


Link to post
Share on other sites
redarrow - None of those ways worked...The first one displayed everything but the information from the database, and the second way just gave me a blank screen.

Share this post


Link to post
Share on other sites
[!--quoteo(post=389116:date=Jun 29 2006, 05:11 AM:name=Colin1388)--][div class=\'quotetop\']QUOTE(Colin1388 @ Jun 29 2006, 05:11 AM) [snapback]389116[/snapback][/div][div class=\'quotemain\'][!--quotec--]
redarrow - None of those ways worked...The first one displayed everything but the information from the database, and the second way just gave me a blank screen.
[/quote]
use my first example and do this.

under select statement.

echo $loop;

post your findings please cheers.
[code]
<?php

$db=mysql_connect("localhost","xxxxnamexxx","xxxpasswordxxxx");
mysql_select_db($db)or die("database not working");

$loop = "SELECT * FROM players WHERE loginname='$loginname' and password='$password' ";
echo $loop;

$result=mysql_query($loop);

while ($row = mysql_fetch_assoc($result)){

$username = $row['username'];
$money = $row['money'];


echo $username;
echo"<br>";
echo $money;
}
?>
[/code]

Share this post


Link to post
Share on other sites
[code]
$loop = "SELECT * FROM players WHERE loginname='$loginname' and password='$password' ";
[/code]
where are you setting $loginname and $password? if you had a login form that calls this script, you need to at the very least do this:

[code]
$loginname = $_POST['loginname'];
$password = $_POST['password'];
$loop = "SELECT * FROM players WHERE loginname='$loginname' and password='$password' ";
[/code]

also, you have this:
[code]
$username = $row['username'];
$money = $row['money'];
[/code]
are you sure you have a column name in your table called 'username' and 'money' ?

Share this post


Link to post
Share on other sites
Nothing is working...I am starting to wonder if I have my sessions setup wrong or something. Here is the code I am using now, I defined what loginname and password was.

[code]
<?php

include 'config.php';
include 'header.php';

$loginname = $_SESSION['loginname'];
$password = $_SESSION['password'];

$loop = "SELECT * FROM players WHERE loginname='$loginname' and password='$password' ";

$result=mysql_query($loop);

while ($row = mysql_fetch_assoc($result)){

$username = $row['username'];
$money = $row['money'];


echo $username;
echo"<br>";
echo $money;
}

?>[/code]

Share this post


Link to post
Share on other sites
you need to start your script off with session_start(); in order to access the session variables:

[code]
<?php
session_start();
.
.
.
[/code]
please note that it is at the beginning of anything else.

Share this post


Link to post
Share on other sites
I have everything working correctly now. Thanks for everyone who posted advice =D

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.