Class PHP for connection to MySQL database
November 30, 2007 8 comments
A few months ago, created a class PHP for connection to MySQL database. Nothing very complex - reaches up to be quite simple, actually. She has the ability to connect to 1 server at a time and was made using the guidance objects, with it, you can make any operation SQL, as insert, select, update, delete and so on.
The file is well reviewed, in my view, and I believe there will be no major problems to understand the functioning of the class.
Everything that should be done to begin to use it, is filling in the values of some constants, early in the code. Alternatively, you can change the messages for each type of error. Also, remember to correctly identify the link to "return" to when each error happens.
Depending on the application, it sure is not restricted to only a query by page. Then, for each SQL query that if you want to make, not where we need to call the constructor method. As the official handbook of PHP, regarding the function "mysql_query":
The connection MySQL. if the identifier of connection is not specified, the last link opened by mysql_connect () is assumed. If a connection is not found soon, she will try to create one as the function mysql_connect () was called without arguments. If a connection is not found or established, a warning level E_WARNING is generated.
Then, after the first call from the constructor method, if you want to perform another query, simply call the function native mysql_query () which, according to the official specifications, the script uses the last connection open.
So far the class PHP has served me well, but if anyone has any advice on how the class can be improved and want to share with other readers, I thank you! Oh, and if someone down the class, use and enjoy, also comment! :-D














Well Tárcio interesting. A try catch a result there would be nice. The use of self was legal but but would prefer $ this. That query you usually go to the manufacturer to test the bank? Or you use a new connection each time you make a query?
@ Gevã
It is, at the time I thought a treatment of exceptions, but just that implementei this way, even talk about it… So, you know me say what would be the benefits in using try / catch, to the detriment of what is today?
As for how to use, put a <ins> in the article in your honor! :-)
About try / catch in one word: "buniteza":-D, code is more elegant. Advantage of truth is that you do not need to block @ notice the error. The try / catch also offers you a way to deal with the acknowledgement of error more flexibly. And it is also useful when one sent by a user can cause error. But to be honest not always use it even.
Good morning I am director of this entity and we are looking for a person to develop a database for people unemployed can through our site under construction can register on our site are an entity that is starting now and we need help we can help the unemployed people in the know that when our site goes live many people will register because of the number of unemployed persons who are today by iste reason I am asking for help to launch this website in the air.
@ Jailton
Hello, Jailton! This virtual space does not provide consultancy service and web development, directly. However, since I sent an e-mail to Mr. to deal with your question.
Hello, Tarcio!
This class can be improved.
I use a unique method to insert, update and delete one that returns true if implemented correctly, and still guardo the last id in an attribute (last_id)!
Another method I object to export a querys that result only one line, using the function: mysqli_fetch_object ()
And one last method that returns an array with an object in each. So I have an array object with 1 each, for querys that return a list.
We will soon put on my site and show him!
Abs
Leo @
Hi, so good? Thank you for tips and comments on this class! As I said, is something quite simple, even, only to ease some of the common connection to databases.
When you write the article about the class you did put it in the comments that I olhadinha and a guide here in development for web!
Abraços and appears to switched ideas! ;-)
Pingback on November 30, 2007