LemonInflux Posted September 24, 2007 Share Posted September 24, 2007 OK, if any of you have helped me before, you'll know I tend to avoid MySQL at all costs. But, I've decided to try it out. So, here's my task. For practice more than anything, I want to create a MySQL driven forum. Some may say this is a bit ambitious, but I did actually manage to create one completely made of PHP files (no MySQL at all). So, I'm capable of creating, deleting tables, etc, and I could probably work out the writing-to-database side of things, but I have no idea how to read from a database. Last time I tried, it returned the first result, and the rest weren't. In short, how do you read every result from the table? How do you query it in a way like, 'display the user with this id'? Also, creating an online list requires a boolean in the database, I presume? How would I set this up? I suppose it would be write a 1 to a field, then upon logout write a 0, but I guess sessions would have to be used so if the user just closed the window, he wouldn't just be left logged in. Thanks in advance, Tom. PS. Sorry for the confusing layout Quote Link to comment https://forums.phpfreaks.com/topic/70431-giant-leap-into-mysql/ Share on other sites More sharing options...
obay Posted September 24, 2007 Share Posted September 24, 2007 how do you read every result from the table? $result = mysql_query("SELECT * FROM table_name") while ($row = mysql_fetch_array($result)) { echo $row[column1] . $row[column2] . $row[column3].. etc.. etc.. } How do you query it in a way like, 'display the user with this id'? SELECT * FROM user WHERE id = "5" Quote Link to comment https://forums.phpfreaks.com/topic/70431-giant-leap-into-mysql/#findComment-353839 Share on other sites More sharing options...
LemonInflux Posted September 24, 2007 Author Share Posted September 24, 2007 Ok, thanks. If I want to echo the second row, what do I do? :\ Quote Link to comment https://forums.phpfreaks.com/topic/70431-giant-leap-into-mysql/#findComment-354051 Share on other sites More sharing options...
freakus_maximus Posted September 24, 2007 Share Posted September 24, 2007 Ok, thanks. If I want to echo the second row, what do I do? :\ Nothing. The WHILE will echo each row you have queried for. So, if you have 1 row, you get 1 echo of that row, if you have 20 rows, you will get 20. The code he is showing you is basic, you will need to add some line breaks or display the results in a table or dive, but that should be similar to what you where doing with your flat file forum. Quote Link to comment https://forums.phpfreaks.com/topic/70431-giant-leap-into-mysql/#findComment-354059 Share on other sites More sharing options...
LemonInflux Posted September 24, 2007 Author Share Posted September 24, 2007 So would I have to use foreach( or something for extra rows? Quote Link to comment https://forums.phpfreaks.com/topic/70431-giant-leap-into-mysql/#findComment-354063 Share on other sites More sharing options...
freakus_maximus Posted September 24, 2007 Share Posted September 24, 2007 So would I have to use foreach( or something for extra rows? Nope. The WHILE is going to do whatever you have defined for each row. Quote Link to comment https://forums.phpfreaks.com/topic/70431-giant-leap-into-mysql/#findComment-354071 Share on other sites More sharing options...
LemonInflux Posted September 24, 2007 Author Share Posted September 24, 2007 Eh? So what if I wanted to say 'for each line, if the username matches the $_POST value, echo their userid'? Quote Link to comment https://forums.phpfreaks.com/topic/70431-giant-leap-into-mysql/#findComment-354112 Share on other sites More sharing options...
trq Posted September 24, 2007 Share Posted September 24, 2007 You probably need to start with some tutorials, its pretty straight forward stuff. Theres a good link in my signiture. Quote Link to comment https://forums.phpfreaks.com/topic/70431-giant-leap-into-mysql/#findComment-354135 Share on other sites More sharing options...
BlueSkyIS Posted September 24, 2007 Share Posted September 24, 2007 Eh? So what if I wanted to say 'for each line, if the username matches the $_POST value, echo their userid'? $result = mysql_query("SELECT * FROM table_name") while ($row = mysql_fetch_array($result)) { if ($row['username'] == $_POST['username']) { echo $row['userid']; } } Quote Link to comment https://forums.phpfreaks.com/topic/70431-giant-leap-into-mysql/#findComment-354145 Share on other sites More sharing options...
LemonInflux Posted September 24, 2007 Author Share Posted September 24, 2007 Ah, I see, thankyou. Quote Link to comment https://forums.phpfreaks.com/topic/70431-giant-leap-into-mysql/#findComment-354162 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.