pocobueno1388 Posted June 28, 2006 Share Posted June 28, 2006 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 loopprint "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. Quote Link to comment https://forums.phpfreaks.com/topic/13158-having-problems-calling-specific-data-from-the-database/ Share on other sites More sharing options...
localhost Posted June 29, 2006 Share Posted June 29, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/13158-having-problems-calling-specific-data-from-the-database/#findComment-50624 Share on other sites More sharing options...
realjumper Posted June 29, 2006 Share Posted June 29, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/13158-having-problems-calling-specific-data-from-the-database/#findComment-50625 Share on other sites More sharing options...
pocobueno1388 Posted June 29, 2006 Author Share Posted June 29, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/13158-having-problems-calling-specific-data-from-the-database/#findComment-50637 Share on other sites More sharing options...
yong Posted June 29, 2006 Share Posted June 29, 2006 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 oneabout 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... Quote Link to comment https://forums.phpfreaks.com/topic/13158-having-problems-calling-specific-data-from-the-database/#findComment-50644 Share on other sites More sharing options...
pocobueno1388 Posted June 29, 2006 Author Share Posted June 29, 2006 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? Quote Link to comment https://forums.phpfreaks.com/topic/13158-having-problems-calling-specific-data-from-the-database/#findComment-50658 Share on other sites More sharing options...
Eugene Posted June 29, 2006 Share Posted June 29, 2006 the part password=$password needs to have single quotes. password = '$password' and the same for username. Quote Link to comment https://forums.phpfreaks.com/topic/13158-having-problems-calling-specific-data-from-the-database/#findComment-50660 Share on other sites More sharing options...
redarrow Posted June 29, 2006 Share Posted June 29, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/13158-having-problems-calling-specific-data-from-the-database/#findComment-50673 Share on other sites More sharing options...
pocobueno1388 Posted June 29, 2006 Author Share Posted June 29, 2006 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 Link to comment https://forums.phpfreaks.com/topic/13158-having-problems-calling-specific-data-from-the-database/#findComment-50686 Share on other sites More sharing options...
redarrow Posted June 29, 2006 Share Posted June 29, 2006 [!--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] Quote Link to comment https://forums.phpfreaks.com/topic/13158-having-problems-calling-specific-data-from-the-database/#findComment-50715 Share on other sites More sharing options...
.josh Posted June 29, 2006 Share Posted June 29, 2006 [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' ? Quote Link to comment https://forums.phpfreaks.com/topic/13158-having-problems-calling-specific-data-from-the-database/#findComment-50729 Share on other sites More sharing options...
pocobueno1388 Posted June 29, 2006 Author Share Posted June 29, 2006 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]<?phpinclude '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] Quote Link to comment https://forums.phpfreaks.com/topic/13158-having-problems-calling-specific-data-from-the-database/#findComment-50953 Share on other sites More sharing options...
.josh Posted June 29, 2006 Share Posted June 29, 2006 you need to start your script off with session_start(); in order to access the session variables:[code]<?phpsession_start();...[/code]please note that it is at the beginning of anything else. Quote Link to comment https://forums.phpfreaks.com/topic/13158-having-problems-calling-specific-data-from-the-database/#findComment-51010 Share on other sites More sharing options...
pocobueno1388 Posted July 1, 2006 Author Share Posted July 1, 2006 I have everything working correctly now. Thanks for everyone who posted advice =D Quote Link to comment https://forums.phpfreaks.com/topic/13158-having-problems-calling-specific-data-from-the-database/#findComment-51478 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.