473,322 Members | 1,431 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Wrote a MySQL function - want some feedback and criticism

I wrote this function about 2 years ago when I started in PHP, I haven't touched PHP since so im still not an expert, but I would like to know what people thought of it, and if was any good / secure / fast / easy to use ect ect

Looking for some constructive criticism really and improvements and faults.

Thanks

[PHP]<?php

// Database Function
function db($Action) {

$Username = '';
$Password = '';
$Database = '';
$Host = 'localhost';

if ( $Action == 'open' ) {
mysql_connect($Host, $Username, $Password) or die ( 'Unable to connect to the server. This may be because the server does not exsist, or the username and password given was incorrect. Please check and try again.<br>' );
mysql_select_db($Database) or die ( 'Unable to select the database. Database does not exsist. Please check and try again.<br>' );
} else if ( $Action == 'close' ) {
mysql_close() or die ( 'Unable to disconnect from the server. Maybe there was no connection to disconnect from?<br>' );
} else {
echo 'The handle that was sent is not valid. Please check and try again.<br>';
}
}
function query($query, $line) {
global $queries, $queries_time, $queries_count;
$queries_count++;

// Add the query to the queries list. Dont add a break at the end in case an error occurred
$queries .= 'Line <b>'. $line . '</b> of file <b>' . $_SERVER['PHP_SELF'] . '</b> requested <b>' . $query . '</b>';
// Do the query if it can
$time_overall = round(microtime(), 15);

if (!$result = mysql_query($query)) {
echo 'Error on line <b>'. $line . '</b> of file <b>' . $_SERVER['PHP_SELF'] . '</b> requesting <b>' . $query . '</b><br>';
$queries .= ' <b>ERROR OCCURRED</b><br>';
} else {
$queries .= '<br>';
}

$time2_overall = round(microtime(), 15);
$gen_overall = $time2_overall - $time_overall;
$queries_time = $queries_time + $gen_overall ;

return $result;
}
?>[/PHP]
Sep 3 '08 #1
5 1555
Also IF its is any good, feel free to use it yourself.
Sep 3 '08 #2
Atli
5,058 Expert 4TB
Hi.

I have a few tips.

First, and perhaps most important, I would suggest you try a more OOP approach. By that I mean, rather than create several stand-alone functions, try creating a class to handle all Database related operations.
(Check out this article to see how to do that.)

Second, I would upgrade to the MySQLI extension, rather than the old MySQL extension. The latter has been deprecated since MySQL 5, and PHP5, and now that PHP4 has officially reached it's end-of-life, it's time to let the old ways go.

Lastly, and this is just a best-practice sort of thing, I would suggest using more consistent naming conventions. Even tho this may not be an issue now, a function named "db" or "query" could possibly apply to a number of things, which could seriously confuse you (or somebody adopting your code) in the future. Giving then more specific names, like say "DoDatabaseAction" or "QueryDatabase" would be much more helpful for people who are trying to understand your code.

It's also always a good idea to be consistent when creating variables and function names, like say always using $lowerCaseCamelStyle names for parameters, and the traditional php_underscore_style() for function names.
Helps make the code more easily understandable.

Hope that helps.
Sep 4 '08 #3
Thanks for your comments, I will defiantly be looking into your suggestions.
Sep 4 '08 #4
Markus
6,050 Expert 4TB
Hi.

I have a few tips.

First, and perhaps most important, I would suggest you try a more OOP approach. By that I mean, rather than create several stand-alone functions, try creating a class to handle all Database related operations.
(Check out this article to see how to do that.)

Second, I would upgrade to the MySQLI extension, rather than the old MySQL extension. The latter has been deprecated since MySQL 5, and PHP5, and now that PHP4 has officially reached it's end-of-life, it's time to let the old ways go.

Lastly, and this is just a best-practice sort of thing, I would suggest using more consistent naming conventions. Even tho this may not be an issue now, a function named "db" or "query" could possibly apply to a number of things, which could seriously confuse you (or somebody adopting your code) in the future. Giving then more specific names, like say "DoDatabaseAction" or "QueryDatabase" would be much more helpful for people who are trying to understand your code.

It's also always a good idea to be consistent when creating variables and function names, like say always using $lowerCaseCamelStyle names for parameters, and the traditional php_underscore_style() for function names.
Helps make the code more easily understandable.

Hope that helps.
MySQL deprecated? Say wha..?
Sep 4 '08 #5
Atli
5,058 Expert 4TB
MySQL deprecated? Say wha..?
Technically not, your right, but the MySQL extension was written for MySQL 3, and the MySQLi extension for later MySQL versions:
Although this MySQL extension is compatible with MySQL 4.1.0 and greater, it doesn't support the extra functionality that these versions provide. For that, use the MySQLi extension.
The new MySQLi extension also supports prepared queries (MySQLi_STMT), which are supposedly faster than the normal query functions of either extension. Not to mention safer, as all input data is automatically escaped before execution.
Sep 4 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: GTF | last post by:
PHP Web Survey Idea.. I have been given an opportunity to create a web based survey. This is a fairly lengthy survey of 60 pages on paper (various multiple choice and free form). These are...
8
by: madcap | last post by:
Hi, Our company was looking for contract programmer to develop an internet/intranet application. We were approached by a freelancer who have quite a lot experience and his resume was...
0
by: Mike Chirico | last post by:
Interesting Things to Know about MySQL Mike Chirico (mchirico@users.sourceforge.net) Copyright (GPU Free Documentation License) 2004 Last Updated: Mon Jun 7 10:37:28 EDT 2004 The latest...
0
by: Alex Molochnikov | last post by:
Hello everyone, We are looking for beta-testers for our new product - Scribe Report Generator. At this moment, Scribe supports 6 relational databases, including Oracle, MS SQL Server 2000, and...
4
by: KJemison | last post by:
Well.... just wanted to know if the people at the MySQL.org site ever look at the posts in this group. If they do, they can possibly us a little constructive criticism from a slightly irritated...
0
by: uli2003wien | last post by:
Dear MS-SQL-Server-group maybe my message for MySQL is also for you of interest, since the MS-SQL-Server and it's binary_checksum function is involved...
0
by: Chris Ochs | last post by:
Not to change the topic too much, but as a new postgresql user I have to say that it takes quite a bit of time to find even the basic postgresql tools/interfaces/etc that are available. The main...
4
by: grouchy | last post by:
hello all, not sure if i fully explained myself on an earlier post. All i really need to do is to store an xml file into mysql database. I have no idea how to do this, including any php code i...
1
by: infocaptor | last post by:
Hi Everybody, Using the show commands and information_schema tables, I have designed a dashboard for MySQL and that can also be used as a documentor for all MySQL objects. Its been tested only on...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.