Jump to content

Having Problems Calling Specific Data From The Database.


pocobueno1388

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.
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
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]
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.
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...
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]
[!--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]
[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' ?
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]

Archived

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

×
×
  • Create New...

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.