|
|
|
|
| |
Ever getting tired of typing the same crooked MySQL functions just for a tiny lookup in a table? I did, and then I made myself a couple of functions that I includes in every PHP-file I use in database-connection. Less work? See for yourself! |
|
New to MySQL and PHP? Take a look at this article by Julie Meloni before reading further:
Simple Connection to MySQL with PHP |
|
So when you are ready to make a e-commerce site or a portal, you will find you are using the mysql_connect, mysql_query and mysql_fetch_array a lot. And I tell you youll get tired of it. What I did was make one external file in which I made a few functions doing a look-up, inserting, changing or deleting records in the database. This way, I make more structured coding and I dont have to write so much as earlier. |
|
Another nice result is that it makes it much easier to debug the code. |
|
|
In every php3-file where I need some information from the database, I use a require-statement: |
|
# include the SQL-functions:
require SQLFunctions.inc.php3;
|
|
|
This file includes 6 functions: |
|
- connectIt() does the mysql_db_connect
- doSelect() does the selection in the database
- numSelect() counts the number of rows in the result
- doInsert() inserts information into the database
- doUpdate() updates information in the database
- doDelete() who would guess it deletes from the database
|
This article explains the first two functions. |
|
How it works |
|
By using the MySQL functions directly in PHP, youll need to do several things in order to carry out the functionality you need. As Julie explains, you need to connect to the database, then youll use the connection-id with other functions to insert, delete or select something in the database, to count the results and so on. |
|
In my simplified functions, I need to do the same thing. The first thing I need is to connect to the database. I made one function, where all I do is to connect to the database. |
|
The function connectIt: |
|
function connectIt() {
global $host, $user, $pasword, $db, $connect;
$connect = mysql_connect($host, $user, $pasword);
return $connect;
}
|
|
|
This function uses the variable $host, $user, $pasword and $db (which I have in a file called db.inc.php3 for easy changing between hosts). I guess the names of the variables are self-explanatory. Then it makes a new variable called $connect and assigns the result-number from the mysql_connect function. |
|
The return $connect; statement is used so the calling function receive the connection-id. |
|
Selecting information |
|
The doSelect() function is used to fetch information from a table. It fetches one row at the time, and return an array with the result. The mysql_fetch_array returns an associated array with a key-value combination. The key are the name of the column, and the value is the information in this column / row. |
|
function doSelect($table, $condition = " ", $selectThis = "*") {
#declaring globals
global $db;
# run the database connection.
$connect = connectIt ();
# do the query here
$sqlQuery = "SELECT " . $selectThis . " FROM " . $table . " " . $condition;
$queryID = mysql_db_query ($db, $sqlQuery, $connect);
# print $sqlQuery;
# print "<p>Query: " . $sqlQuery;
$resultArray = mysql_fetch_array($queryID);
return $resultArray;
}
|
|
|
|
The doSelect() function takes three variables: $table, $selectThis and $condition. The $selectThis and $condition is optional which means that if not set, they are assigned a default value. |
|
The third line of the function declares the globals here the variable $db (database). This value has been set in the external file db.inc.php3, as explained above. We need to know which database to connect to in the mysql_db_query function. |
|
The fifth line runs the connectIt() function, and assign the connection id to the variable $connect. We need to know this one too in the mysql_db_query function. |
|
Then, in the seventh line, we make the actual SQL-function that we use when connecting to the database. We assign the SQL-function to the variable $sqlQuery as a text-string (automatic in PHP). |
|
The string is divided in 5 parts a mix of SQL-specific words (SELECT and FROM), and the three variables used when calling the function. View the example below to further understand. |
|
On line nine, we make a new variable - $queryID and assigns the result of the function mysql_db_query(). This function takes three arguments the $db, the $sqlQuery and the $connect.
On line twelve, we fetch the result of the query and assigns it to $resultArray. The next line takes this variable and returns it to the calling variable. |
|
Comment: For the convenience of debugging, I have also included a line (number 10) which reads # print "<p>Query: " . $sqlQuery;
By un-commenting this line, the complete query as sent to the mysql_db_query function is printed in your browser. I use this when trying to figure our why stuff I want to happen, dont or vice versa. |
|
Example of use: |
|
You have a table customer, with the columns ID, Name, Address and phone (usually you would include more details, this is just as an example)
Ex 1.
You would like to find all customers with the name of Jonson:
|
|
$condition = WHERE Name = Jonson
$table = customer;
$info = doSelect($table, $condition);
# print the info;:
print <p>ID: . $info[ID];
print <p>Name: . $info[Name];
print <p>Address: . $info[Address];
print <p>Phone: . $info[phone];
|
|
|
Ex. 2
You would like to find only the address: |
|
$select = Address ;
$condition = WHERE Name = Jonson ;
$table = customer;
$info = doSelect($table, $condition, $select);
print <p>Address: . $info[Address];
|
|
|
OR |
|
print <p>Address: . $info[0];
|
|
|
| |
| Miles To Go Before I Sleep... Categories : PHP, Calendar, Databases, MySQL | | | Alternating row colors with PHP and mySQL Categories : PHP, Databases, MySQL, HTML and PHP | | | Multicolumn Output from a Database with PHP Categories : PHP, Databases, HTML and PHP, MySQL | | | Simple Connection to MySQL with PHP Categories : PHP, MySQL, Databases | | | Date Arithmetic With MySQL Categories : PHP, Databases, MySQL, Date Time | | | Time Is Money Part 1 of 2 - Designing and implementing a Web-based application Categories : PHP, Databases, MySQL, Complete Programs | | | User identification using cookies in PHP and MySQL Categories : PHP, Databases, MySQL, Cookies | | | PHP and MySQL News with Comments Categories : PHP, Databases, MySQL | | | Watching The Web Categories : PHP, Databases, MySQL, HTTP, MD5 | | | How To add paging (Pagination) with PHP and MySQL Categories : PHP, Beginner Guides, Databases, MySQL, HTML and PHP | | | Saving Images in MySQL Categories : MySQL, PHP, Graphics, Databases | | | Referer Statistics Categories : PHP, MySQL, HTTP, Databases | | | Descriptions of Common Data Types Categories : MySQL, Databases, PHP, PHP options/info, General | | | Beginners guide to PHP and MySQL Categories : PHP, Beginner Guides, Databases, MySQL, Installation | | | Creating an IE-Only Database Driven Menu System With PHP, MySQL and DHTML Categories : PHP, MySQL, Databases, DHTML | |
| | | | dyna18 wrote : 26 Could you explain to us why you use functions instead
of classes? | | | Kai Roer wrote : 27 Thanks for your question.
I use functions because I have not had the time to learn
much about OOP yet. Unlike a lot of other programmers,
I have learned it all the hard way.
I would really love to understand the concept of OOP
too - and use classes instead of functions - still, these
functions does solve my needs and time is not around
to build classes.
Of course, if you are able to help me - that would really
be appreciated!
Kai
| | | Dave B wrote : 96 Functions are very much apart of OOP. Classes are nice
but I haven`t learned how to use them yet. You should
see scripts written in a prof. environment where not
even functions were used. It`s very messy. In a Top
Down Design or OOP design functions can be put in a
global library and included in every script which makes it
part of OOP and very re-usable. | | | Anonymous wrote : 230 OOPs ! is a comment my 2 year old grandchild utters
when he has a "little accident"....... great article ! jargon
does NOT substitute for content... procedures and
functions are essential for clarity... after all, we are not
machines ! (right ? huh?)
thanks again kai ! | | | Anonymous wrote : 239 Guys i am looking for c++ Api for Mysql on linux platfrom.
Can anyone please helpe me | | | Bryan H wrote : 408 This is really a cool article... I'd really like to use something like this... but can anyone help me find an updated version? | |
|
|
|