Bounty Posted July 3, 2010 Share Posted July 3, 2010 Ok i made this simple register / login script so how can i list all Usernames from database table like this: 1.user1 2.user2 3.user3 ...... ? Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted July 3, 2010 Share Posted July 3, 2010 Something like this is probably what you're looking for. $query = "SELECT `username` FROM `table` WHERE `username` IS NOT NULL; $result = mysql_query($query); echo "<table>"; count = 1; while( $array = mysql_fetch_assoc($result) { echo "<tr><td>{$count}</td><td>{$array['username']}</td></tr>"; $count++; } echo "</table>"; [code=php:0] Quote Link to comment Share on other sites More sharing options...
Bounty Posted July 3, 2010 Author Share Posted July 3, 2010 I think you missed an '$' at count = 1; Btw with this script i get this error Parse error: syntax error, unexpected '{' in C:\xampp\htdocs\register\memberspage.php on line 15 Line 15 is this: while( $array = mysql_fetch_assoc($result) { :// Thanks for the help btw Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted July 3, 2010 Share Posted July 3, 2010 D'oh! That's what I get for typing it directly in to the reply box . . . No syntax highlighting == I missed more than just that. Here it is edited, and without syntax errors. $query = "SELECT `username` FROM `table` WHERE `username` IS NOT NULL"; $result = mysql_query($query); echo "<table>"; $count = 1; while( $array = mysql_fetch_assoc($result) ) { echo "<tr><td>{$count}</td><td>{$array['username']}</td></tr>"; $count++; } echo "</table>"; Quote Link to comment Share on other sites More sharing options...
Bounty Posted July 3, 2010 Author Share Posted July 3, 2010 Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\register\memberspage.php on line 14 Sorry for troubling you so much...:/ Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted July 3, 2010 Share Posted July 3, 2010 Can you post the code you're using now, that generated the error? Quote Link to comment Share on other sites More sharing options...
Bounty Posted July 3, 2010 Author Share Posted July 3, 2010 <html> <head> <title>Members</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <?php $tablee = "users"; $username = $_POST['username']; $result = mysql_query ("SELECT username FROM $tablee WHERE username IS NOT NULL"); echo '<table>'; $count = 1; while ($array = mysql_fetch_assoc($result)) { echo ("<tr><td>$count</td><td>$array[username]</td></tr>"); $count++; } echo '</table>'; ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted July 4, 2010 Share Posted July 4, 2010 OK. Try this, but this time, copy and paste it. Don't go and change things until we see if it works or not. <html> <head> <title>Members</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <?php $tablee = "users"; $username = $_POST['username']; $result = mysql_query ("SELECT username FROM $tablee WHERE username IS NOT NULL"); echo '<table>'; $count = 1; while ($array = mysql_fetch_assoc($result)) { echo ("<tr><td>$count</td><td>$array[username]</td></tr>"); $count++; } echo '</table>'; ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
Bounty Posted July 4, 2010 Author Share Posted July 4, 2010 Warning: mysql_query() [function.mysql-query]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\xampp\htdocs\register\memberspage.php on line 10 Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in C:\xampp\htdocs\register\memberspage.php on line 10 Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\register\memberspage.php on line 14 Line 10: $result = mysql_query ("SELECT username FROM $tablee WHERE username IS NOT NULL"); Line 14: while ($array = mysql_fetch_assoc($result)) { haven't changed a thing...the last time i was trying something thats why it was changed i forgot to undo it Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted July 4, 2010 Share Posted July 4, 2010 There's your problem. You aren't connecting to the database before running the query. Look at: the mysql_connect() and mysql_select_db() functions. $server = // your server ip address, or localhost if that's the case $user = // database user name $pass = // database user's password, if applicable $db_name = // database name $dbc = mysql_connect($server, $user, $pass); mysql_select_db( $db_name, $dbc) Quote Link to comment Share on other sites More sharing options...
Bounty Posted July 4, 2010 Author Share Posted July 4, 2010 Parse error: syntax error, unexpected T_VARIABLE in C:\xampp\htdocs\register\memberspage.php on line 17 Line 17: $username = $_POST['username']; :// Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted July 4, 2010 Share Posted July 4, 2010 I'm not sure how, but I managed to paste your old code into the reply yesterday, instead of the code I edited. For that, I apologize. Neither of those code blocks have parse errors, though, and neither of them has $username = $_POST['username'] on line 14. In both versions, that is line 9, and the value of $username is not even used in the script. Here is the version of it tried to post yesterday. There are 5 lines of code before this script somewhere. Is this script include()d by another script? If it is, what is in that script? <html> <head> <title>Members</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <?php $tablee = "users"; $username = $_POST['username']; // Does this line have a purpose in this script? The value is never used. $query = "SELECT `username` FROM `$tablee` WHERE `username` IS NOT NULL"; $result = mysql_query($query) or die('Query cratered: ' . mysql_error() . '<br />Using query string: ' . $query); echo '<table>'; $count = 1; while ($array = mysql_fetch_assoc($result)) { echo ("<tr><td>$count</td><td>" . $array['username'] . "</td></tr>"); $count++; } echo '</table>'; ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
Bounty Posted July 4, 2010 Author Share Posted July 4, 2010 Have no idea how that line got in there...must be my mistake when pasting or something... Anyhow..do i need database connection for this? Wont work with or without the connection :/// This is the code i use now... <html> <head> <title>Members</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <?php $server = "localhost"; $user = "***"; $pass = "***"; $db_name = "mysql"; $dbc = mysql_connect($server, $user, $pass); mysql_select_db( $db_name, $dbc) $query = "SELECT `username` FROM `users` WHERE `username` IS NOT NULL"; $result = mysql_query($query) or die('Query cratered: ' . mysql_error() . '<br />Using query string: ' . $query); echo "<table>"; $count = 1; while( $array = mysql_fetch_assoc($result) ) { echo "<tr><td>{$count}</td><td>{$array['username']}</td></tr>"; $count++; } echo "</table>"; ?> </body> </html> It shows error in this line: $query = "SELECT `username` FROM `users` WHERE `username` IS NOT NULL"; Should i escape quotes or anything..? Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted July 4, 2010 Share Posted July 4, 2010 You left the semi-colon ; off the previous line. Quote Link to comment Share on other sites More sharing options...
Bounty Posted July 4, 2010 Author Share Posted July 4, 2010 It finally works... It shows: 1 2 Adam 3 Bounty First i created "Bounty" and then i created "Adam" so it should be Bounty then Adam..:// And why is 1. empty? Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted July 4, 2010 Share Posted July 4, 2010 Then there should only be 2 records in the `users` table in the database. How many are there? Also, you aren't using any ORDER BY statement in the query, so it may not retrieve the records in the order you'd expect it to. Quote Link to comment Share on other sites More sharing options...
Bounty Posted July 4, 2010 Author Share Posted July 4, 2010 So how can i order it? And is there a way to import data (usernames) in html directly? Edit: This is the table data structure its weird.. http://img149.imageshack.us/img149/5174/databh.jpg I'm lost... Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted July 4, 2010 Share Posted July 4, 2010 So how can i order it? Google "MySQL ORDER BY" And is there a way to import data (usernames) in html directly? What do you mean import username "directly"? I don't follow what you're asking. Edit: This is the table data structure its weird.. http://img149.imageshack.us/img149/5174/databh.jpg I'm lost... That isn't the table's structure, really. In phpMyAdmin, select the table, the click the "Structure" tab at the top to see the structure. Quote Link to comment Share on other sites More sharing options...
Bounty Posted July 4, 2010 Author Share Posted July 4, 2010 What do you mean import username "directly"? I don't follow what you're asking. Well could i make a variable that displays the list and when needed in html code just inserting the variable in the table.. That isn't the table's structure, really. In phpMyAdmin, select the table, the click the "Structure" tab at the top to see the structure. Well what is it? I think its data inside the table...if it is than it is weird... Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted July 4, 2010 Share Posted July 4, 2010 You apparently hit the "Propose Table Structure" link. That doesn't give you the current table structure. The "Structure" tab at the top does. As for your first question, I still don't understand what you're wanting to do. The username information should already be in the database for all actual users, no? Why would you need to re-insert it? Quote Link to comment Share on other sites More sharing options...
Bounty Posted July 4, 2010 Author Share Posted July 4, 2010 I ment something like this..could i make a string like "$userlist = command for listing" and use it in html anywhere in the code? I know this is not a table structure but what is it? It looks messed up... Quote Link to comment 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.