Basic MySQL Functions in PHP and How I Use Them

Alright, first of all, I'm not quite sure if I explained and integrated the functions correctly. So, if you think I got them wrong, please do tell me and I'll try to make some changes on the post. Anyways, you're probably wondering why I made this post. Basically, there are still some people who are new to programming in PHP so I guess it would be a good help if there are articles to guide them through. I'll assume you guys know what MySQL is.

Okay, so I'm going to be explaining the basic and widely-used functions of MySQL in PHP:

  • mysql_connect - This is the function that is used when connecting to the server. This is usually the first function that you call before doing any other mysql functions. This function takes 5 parameters. We only need to know the first three since those are usually the significant ones. The first parameter is the server name which takes a string data type, second parameter is the user name which also takes a string data type, and lastly, the password which also takes the same data type.
  • mysql_select_db - What this function does is that it selects the database name you specify in the server. The parameter that it takes is the database name you wish to select. The database name parameter takes a string data type.
  • mysql_query - This function does the querying. It's like typing a query on the mysql command line tool. The parameter here that is significant is the query itself, of course. The query should be a string data type.
  • mysql_fetch_array - This function retrieves the data after doing a "SELECT" query using mysql_query. The first parameter is the result of a mysql_query. This parameter takes a resource data type. The second parameter is the result type which is optional. The default value is MYSQL_BOTH which means you can either use names or the number of the column you wish to retrieve. The value can also be MYSQL_ASSOC or MYSQL_NUM. It's quite self-explanatory actually. Just bear in mind that MYSQL_NUM is usually faster.
  • mysql_error - This function returns the error message in the last mysql operation. Basically, it displays the error message in string data type if the last mysql query executed failed.

1 comments:

  Anonymous

November 6, 2008 at 4:33 AM

i remember the days..

..when PHP & MySQL coding is somewhat fun..

but gone are those days..

so, all i can do is reminisce..

nice post (the only one i can ever relate to, others blow my mind..tsk)

:D

PS: my word verification is 'renti'.. hmmm..