Category: MySQL

Autoloading Data Models

Hi Guys

Consider you have a model like this

class samplemodel
{
public method setName(){/*set the name*}
public method setEmail(){/*set the email*}
}

Models are usually relevant to database tables. So if you have tables with 10-12+ fields you may define accessor methods for all these fields in your model. Now when loading data manually in the model after submitting an HTML form you need to call them like this

$model = new samplemodel();
$model->setName(“some name”);
$model->setEmail(“some email”);
……………………..

For models with few accessor methods it may be easy to type manually all these data. But for 10-20-30 its totally ridiculous.

Thats why I designed this small Automatic Data Loader class which takes a model as first argument and and data array as second argument and then loads data from that array to that model. How is it looking?

$val)
{
if (is_callable(array($module,”get{$key}”),$callable))
{
echo “$key
“;
call_user_func(array(&$module,”set{$key}”),$val);
}
}
}
}
?>

Now if you have two input object named “email” and “name” for example in yoru html form and you call this loader as shown below, it will load all the data from $_POST to the model automatically.

$model = new samplemodel();
dataloader::load($model, $_POST);

as the model is accessed with BY_REFERENCE style, so it will fetch $model->setName() and $model->setEmail with appropriate $_POST data and your model will be loaded automatically.

This comes really handy when you have to load models with many accessor methods. I developed this object while working for Bangla Chat Engine last night and Ha Ha HA, it really saves my time.

Caution: Using raw user submitted data from $_POST or $_GET is not secured. You should filter them first.

Using More DBs concurrently in PHP+MySQL – a Solution

Last night Hasan provided me a new solution which seems awesome. I am describing it here

$db1 = mysql_connect("host","user","pwd")
mysql_select_db("db1", $db1);
$res1 = mysql_query("query",$db1);

$db2 = mysql_connect("host","user","pwd")
mysql_select_db("db2", $db2);
$res2 = mysql_query("query",$db2);

At this point you can only fetch records from you previous ResultSet, i.e $res1 – But you cannot execute new query in $db1, even if you supply the link as because the link was overwritten by the new db.

so at this point the following script will fail
$res3 = mysql_query("query",$db1); //this will fail

So how to solve that?

take a look below.
$db1 = mysql_connect("host","user","pwd")
mysql_select_db("db1", $db1);
$res1 = mysql_query("query",$db1);

$db2 = mysql_connect("host","user","pwd", true)
mysql_select_db("db2", $db2);
$res2 = mysql_query("query",$db2);

So mysql_connect hasa nother optional boolean parameter which indicates whether a link will be created or not. as we connect to the $db2 with this optional parameter set to ‘true’, so both link will remain live.

now the following query will execute successfully.
$res3 = mysql_query("query",$db1);

Thanks goes to Hasan for informing me abt the fourth parameter.

Qucik and Dirty – using 2 DB concurrently in MySQL

Day b4 yesterday I was stuck with a simple and frustrating problem. You know that using builtin mysql support in PHP you cannot use concurrently more than one database to work with. That was also my problem while working in such a project where I badly need to read data from one database, validate it against existing data in second database and then insert the data into the second database. So you got the project? Yes its a simple data migration project. But the problem is that I cannot import the old database tables into the new one and I have to work with them keeping seperate.

So you may ask how about reading all the data from first database, then store it in a temporary media and then again read them while inserting in second db. Well that was not possible because in first DB I had 10 tables and in I have to merge all those info in two table in second database. Also I have to validate some of those data against existing data in second DB.

There comes the idea. What I did is a quick-n-dirty solution of this problem. I connected to the first DB using MySQL API, and I connected to the 2nd DB using MySQLi API. That gives me live connection of two DB at the same time. So I read data from first DB using MySQL API and then inserted using MySQLi API.

For sure I could apply another solutions like temporary storage. But why bother when you can trick??? This is just a one time operation, no matter how you do it. All you need is a merged output, at minimum efforts. Ha Ha Ha.