jaimeweb Posted March 27, 2007 Share Posted March 27, 2007 Hi...Right this may be hard to explain becuase i am not any good with mysql...but i have a website and i have a mysql database of which people sign up to my website and then i can view all the users... Now i want to add something on my website which says "newest member: *their username*" So it takes the username from the mysql table of the newest entry...is this possilbe and how?!!?!?! Please explain in detail.....thank you so much in advance Quote Link to comment https://forums.phpfreaks.com/topic/44558-solved-newest-member-sign-up-from-database/ Share on other sites More sharing options...
jaimeweb Posted March 28, 2007 Author Share Posted March 28, 2007 anyone? Quote Link to comment https://forums.phpfreaks.com/topic/44558-solved-newest-member-sign-up-from-database/#findComment-216824 Share on other sites More sharing options...
Shirik Posted March 28, 2007 Share Posted March 28, 2007 Show the structure of the table? Do you have an primary key/auto_increment column? If so, couldn't you just do something like SELECT username FROM members ORDER BY id LIMIT 1; Also, SELECT LAST_INSERT_ID(); works but I don't think it will work for your purposes, I'm afraid. Alternatively, add a column to store the date/time of join, then order on that instead of id (same query as above). Quote Link to comment https://forums.phpfreaks.com/topic/44558-solved-newest-member-sign-up-from-database/#findComment-216851 Share on other sites More sharing options...
jaimeweb Posted March 28, 2007 Author Share Posted March 28, 2007 Hi Shirik thanks for your comment. Right as for this bit: SELECT username FROM members ORDER BY id LIMIT 1; I would have no idea of how to insert that on my PHP webpage...so would you be able to write it so i could just copy it in...(obviosuly changing the parts i need to) And yes i do have a auto_increment column......i think...anyway..ive added a screenshop of my database in phpmyadmin...Please get back to me thanks so much again [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/44558-solved-newest-member-sign-up-from-database/#findComment-217043 Share on other sites More sharing options...
jaimeweb Posted March 29, 2007 Author Share Posted March 29, 2007 Please help! Quote Link to comment https://forums.phpfreaks.com/topic/44558-solved-newest-member-sign-up-from-database/#findComment-217510 Share on other sites More sharing options...
genics Posted March 29, 2007 Share Posted March 29, 2007 Something like this? <?php $latestUser = "SELECT id, uid FROM table_name ORDER BY id DESC LIMIT 1"; $result = mysql_query($latestUser ) or die(mysql_error()); while(list($id, $uid) = mysql_fetch_array($result, MYSQL_NUM)) { ?> <p>Newest Member: <?php echo $uid; ?></p> <?php } ?> Quote Link to comment https://forums.phpfreaks.com/topic/44558-solved-newest-member-sign-up-from-database/#findComment-217520 Share on other sites More sharing options...
genics Posted March 29, 2007 Share Posted March 29, 2007 Replace table_name With your actual table name. Quote Link to comment https://forums.phpfreaks.com/topic/44558-solved-newest-member-sign-up-from-database/#findComment-217522 Share on other sites More sharing options...
jaimeweb Posted March 29, 2007 Author Share Posted March 29, 2007 Hi genics thanks for the reply...im gettin this message when i go on my webpage...ive typed the table name in... Warning: mysql_query() [function.mysql-query]: Access denied for user: 'nobody@localhost' (Using password: NO) in /data01/owlsfan/public_html/try.php on line 3 Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /data01/owlsfan/public_html/try.php on line 3 Access denied for user: 'nobody@localhost' (Using password: NO) Quote Link to comment https://forums.phpfreaks.com/topic/44558-solved-newest-member-sign-up-from-database/#findComment-217525 Share on other sites More sharing options...
jaimeweb Posted March 29, 2007 Author Share Posted March 29, 2007 Another thing...when you say Table_name...what do you mean...the actaul table name? because that is just "users"...but then how does the code know to pick it from which database becuase the database is called "owlsfan_simp" which consists of one table which is "users" Thanks Quote Link to comment https://forums.phpfreaks.com/topic/44558-solved-newest-member-sign-up-from-database/#findComment-217531 Share on other sites More sharing options...
Waldir Posted March 29, 2007 Share Posted March 29, 2007 you need to connect to the database /* Make connection to database */ $connection = mysql_connect(DB_SERVER, DB_USER, DB_PASS) or die('Couldnt Connect to the database'); mysql_select_db(DB_NAME, $connection) or die(mysql_error()); replace: DB_SERVER, DB_USER, DB_PASS, DB_NAME with the corresponding information Quote Link to comment https://forums.phpfreaks.com/topic/44558-solved-newest-member-sign-up-from-database/#findComment-217546 Share on other sites More sharing options...
genics Posted March 29, 2007 Share Posted March 29, 2007 Yep, as waldir pointed out - you need to connect to the database in order for my script to work. Your strcuture will be: database -> table ---> user So you need to first connect to your database with the following script: $host = "server"; $user = "username"; $pass = "password"; $database = "database_name"; $connect = mysql_connect($host, $user, $pass) or die('Connection Failed'); mysql_select_db($database) or die(mysql_error()); Then you need to select the table and relevant fields from the database: <?php $latestUser = "SELECT id, uid FROM table_name ORDER BY id DESC LIMIT 1"; And then finally you need to display those selected fields: $result = mysql_query($latestUser ) or die(mysql_error()); while(list($id, $uid) = mysql_fetch_array($result, MYSQL_NUM)) { ?> <p>Newest Member: <?php echo $uid; ?></p> <?php } ?> So to put it all together: <?php $host = "server"; $user = "username"; $pass = "password"; $database = "database_name"; $connect = mysql_connect($host, $user, $pass) or die('Connection Failed'); mysql_select_db($database) or die(mysql_error()); $latestUser = "SELECT id, uid FROM table_name ORDER BY id DESC LIMIT 1"; $result = mysql_query($latestUser ) or die(mysql_error()); while(list($id, $uid) = mysql_fetch_array($result, MYSQL_NUM)) { ?> <p>Newest Member: <?php echo $uid; ?></p> <?php } ?> So for you to get my script to work, you need to edit all the bits: - server - username - password - database_name - table_name Quote Link to comment https://forums.phpfreaks.com/topic/44558-solved-newest-member-sign-up-from-database/#findComment-217576 Share on other sites More sharing options...
genics Posted March 29, 2007 Share Posted March 29, 2007 Another thing...when you say Table_name...what do you mean...the actaul table name? because that is just "users"...but then how does the code know to pick it from which database becuase the database is called "owlsfan_simp" which consists of one table which is "users" Thanks The table name is the name of the table you posted a screenshot of. If that's called users then just replace table_name with users Quote Link to comment https://forums.phpfreaks.com/topic/44558-solved-newest-member-sign-up-from-database/#findComment-217577 Share on other sites More sharing options...
jaimeweb Posted March 29, 2007 Author Share Posted March 29, 2007 OMG, thanks so much..it works ...thanks very much for your help! Quote Link to comment https://forums.phpfreaks.com/topic/44558-solved-newest-member-sign-up-from-database/#findComment-217615 Share on other sites More sharing options...
genics Posted March 30, 2007 Share Posted March 30, 2007 No problem mate. Remember to mark this as solved. Quote Link to comment https://forums.phpfreaks.com/topic/44558-solved-newest-member-sign-up-from-database/#findComment-217889 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.