Jump to content

Recommended Posts

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 :P

Link to comment
https://forums.phpfreaks.com/topic/70431-giant-leap-into-mysql/
Share on other sites

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"

 

 

Link to comment
https://forums.phpfreaks.com/topic/70431-giant-leap-into-mysql/#findComment-353839
Share on other sites

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.

 

 

Link to comment
https://forums.phpfreaks.com/topic/70431-giant-leap-into-mysql/#findComment-354059
Share on other sites

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'];
     }
}

Link to comment
https://forums.phpfreaks.com/topic/70431-giant-leap-into-mysql/#findComment-354145
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.