Sticky Posts
Jun 13, 2009
MySQL Functions Stub | Easy Use Of SQL Links (link_identifier)
This function will be used in several scripts I will publish these days so make sure you set it aside. It's meant to make using sql links in mysql functions easy.
Let's say you write a function:
<?php
// ------------------------------------------------------------ //
function sqlQuery($query, $sqlink = null){
return is_resource($sqlink) ? mysql_query($query, $sqlink) : mysql_query($link);
}
/* OR */
function sqlQuery($query, $sqlink){
$args = func_get_args();
return call_user_func_array('mysql_query', $args);
}
// ------------------------------------------------------------ //
?>
As you can see, the above function makes calling mysql_query easy and allows you to choose whether to use a mysql link as 2nd parameter. Problem is if you call mysql_query($query, null); it will fail so you need such a helper to make the optional $sqlink easy to set. The second sample allows the optional $sqlink parameter but needs to be called using call_user_func_array as you can't directly call a function with an optional parameter.
There are more than 1 mysql_ functions that accept an optional (but not nullable) link_identifier parameter.
- int mysql_affected_rows
[ resource $link_identifier ] - int mysql_insert_id
[ resource $link_identifier ] - int mysql_change_user
string $user , string $password [, string $database [, resource $link_identifier ]] - string mysql_client_encoding
[ resource $link_identifier ] - bool mysql_close
[ resource $link_identifier ] - bool mysql_create_db
string $database_name [, resource $link_identifier ] - resource mysql_db_query
string $database , string $query [, resource $link_identifier ] - bool mysql_drop_db
string $database_name [, resource $link_identifier ] - int mysql_errno
[ resource $link_identifier ] - string mysql_error
[ resource $link_identifier ] - string mysql_get_host_info
[ resource $link_identifier ] - int mysql_get_proto_info
[ resource $link_identifier ] - string mysql_get_server_info
[ resource $link_identifier ] - string mysql_info
[ resource $link_identifier ] - int mysql_insert_id
[ resource $link_identifier ] - resource mysql_list_dbs
[ resource $link_identifier ] - resource mysql_list_fields
string $database_name , string $table_name [, resource $link_identifier ] - resource mysql_list_processes
[ resource $link_identifier ] - bool mysql_ping
[ resource $link_identifier ] - resource mysql_query
string $query [, resource $link_identifier ] - string mysql_real_escape_string
string $unescaped_string [, resource $link_identifier ] - bool mysql_select_db
string $database_name [, resource $link_identifier ] - bool mysql_set_charset
string $charset [, resource $link_identifier ] - string mysql_stat
[ resource $link_identifier ] - int mysql_thread_id
[ resource $link_identifier ] - resource mysql_unbuffered_query
string $query [, resource $link_identifier ]
As you see, these functions need a valid link_identifier as parameter or none at all. NULL is not an option. That's making your choice of mentioning a sqlink parameter or null a bit difficult.
My SQLStub function: el_sql_stubFunction
This function [el_sql_stubFunction] will allow you to call mysql_query like this: sqlStub('mysql_query', $query, null) or sqlStub('mysql_query', $query, $valid_link). Any of the above functions can be called using it.
Registration is FREE, quick, painless and worth its weight in gold.
Use the function like this: $res = sqlStub('mysql_query', $query, $link); . This function knows mysql functions that accept the optional link identifier and validates the last parameter you feed to it to be a valid resource. If it's not, it simply removes it and works on default link.
Final Comments
Some of you may consider this code redundant but it's not really like that if you know how to play with parameters and this function is vital for all my eLIB mysql functions. I use a lot of functions that accept an $sqlink last parameter predefined to null. This function validates it and makes sure all my calls work.
I will share more mySQL helper Functions these days and they will need this function to exist.


