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.
Link to comment
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
Link to comment
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]
Link to comment
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.
Link to comment
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...
Link to comment
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]
Link to comment
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]
Link to comment
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' ?
Link to comment
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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.