
-----------------------------------
Blade
Wed Jan 26, 2005 4:02 pm

Read/Write to mysql database, and a little phpmyadmin
-----------------------------------
I originally posted this in php help as a response to a question, but since it was so detailed and tutorial-like i decided to edit a bit to generalize it and post it in here. the scenerio was he needed to create a database, and wanted to display news on the site. he wanted it to display the date, a picture with the date, the title, and the body.

a mysql server has databases, in the databases are tables. tables have set "fields" which you can think of as colomns.. each row will have different information.. ie:

table news:


id | topic  |  message
------------------------
1  | test   | testing the news
2  | test2  | second news test

just to make it easier the best way to do mysql stuff is to get a program called phpmyadmin, then edit the config file with your hostaddress (for your database), your username and password. phpmyadmin allows you fully control your database so it'd be best to put some type of security on it if you decide to use it. also the reasoning for using this is to make databases (as long as you have the proper permission on your mysql server), make tables etc.. phpmyadmin is good to have because if you're only reading and writing to specific tables in your scripts you only need to make the table once (same with database), then you can use it to edit tables and such instead of writing scripts to do anything you want to do with it.

now. when you go into phpmyadmin you can create a new database if you need to, but some places give you your database so you are restricted to editing that one.. in this iam going to refer to your news table, as news. so you're going to want to create a table called news. you are going to need 5 fields in it from what you said you wanted it to display. the first one i always use in all my tables, it being the identifier, because you can have 2 of the same titles in your database or something similar. so we'll call that field id .. gonna be set as int and i usually set the size to 11 ... i also set id as the primary field and sometimes i set it to autoincrement... you may want to do this.. as for the other fields you will fill in the names according to what you want to be in them.. you said you wanted an image with the day on it.. so you may want to label that as iday .. and have a hyperlink created by php to determine which image to link it to.. you'll want that to be varchar and maybe a length of 50.. 50 being the amount of characters (maximum being 255) the field is able to hold.. if it is more then mysql will return an error.. do the same with the rest of the fields... iam going to refer to your fields as: id, idate, date, title, body... now for the body you're going to want to set the type of that to text ... where there is no limit for the amount of characters it can hold .. (some other programs like phpmyadmin call it blob) .. so for that you will set no size...

now for the php part..
first off to connect


note: if you are unfamilar with any of these function, you can search for them at  