Re: advise needed on a mysql select library function

From: Date: Fri, 13 Jan 2012 19:28:46 +0000
Subject: Re: advise needed on a mysql select library function
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-316291@lists.php.net to get a copy of this message
I'm leaning towards this; function bp_select($db_name,$sql,&$result) { bp_conn($db_name,$db_server,$db_username,$db_pass); //with that, I pass the $db_name and the rest gets byRef'ed by the bp_conn! and I keep the bp_conn in a sep. file $link = mysql_connect($db_server, $db_username, $db_pass) or die(mysql_error()); mysql_select_db($db_name, $link) or die(mysql_error()); $result = mysql_query($sql,$link) or die(mysql_error()); mysql_close($link); } On Fri, Jan 13, 2012 at 11:18 AM, Haluk Karamete <halukkaramete@gmail.com> wrote: > Why discover the wheel, where there are so many qualified pros are > here that pick up their brains... > > I'm building a php library of functions, I mean day to day functions > that eases my RAD. > > Since I am new to PHP, I'm trying to wrap php's built-in-functions and > funtionalities into new function names of my own creation from my old > ASP library so that I can work within the new php environment - using > still the familiar function names and arguments ... for example, i had > a leftof function which worked as > > leftof("abcef","bc") //returns "ef" >        //which is the "leftof" the "bc" in the haystack > "abcdef". > > That's the idea... I have over 100 functions that does all kinds of > things... the goal is to write those functions' php equaivalents. I'm > done with the string and utility functions, now I m getting into db > stuff! > > My goal now is to write a one-liner sqlselect functionality which will > work like this > > <?php > > _select("my_wordpress_database_name_here","SELECT * FROM > wp_posts",$result); > > //so that I can instantly get into business as follows; > > while ($row = mysql_fetch_assoc($result)) { >        echo "<li>" . $row['post_title']; > } > > basically, what the _select is supposed to do is to take the passed > database name, look that up in a switch statement to get the sql > server, db uid & pwd info and run the whole show all the way until it > puts the results in the $result resource. And I'm already done with > that...  I posted the code below...  but I want to isolate the switch > statement ( that contains the sqlserver,uid,pwd data ) out from this > library file...  I do not want to keep them in the library. ideally, > that info should be kept in say, connection_info.php file... > > how would you go about it? > > write an include directive ( for the switch section only) and > implement it that way? or is there a better way - such as using a > function for the switch? > please fell free to not only answer this question but also improve the > code segment I posted below. I will be using the principles I gain > from this thread in writing the update,delete and insert versions... > > this is where I am now and following code snippet works as intended... > > > > _select( "wordpress_XYZ" , "SELECT * FROM wp_posts" , $result ); > > while ( $row = mysql_fetch_assoc($result)) { >        echo "<li>" . $row['post_title']; > } > > function _select($db_name,$sql,&$result) > { > > > > >        switch (bp_lcase($db_name)) >        { > >                case  "wordpress_XYZ"; > >                        $db_name = ""; // this is usually the same as the > 1st argument > passed by the user >                        $db_server = ""; >                        $db_username = ""; >                        $db_pass = ""; >                        break; > >                case " ": > >                        echo "Unknown database."; >                        die; > >                break; > >                default: > >                        echo "Database name not passed"; >                        die; >                        break; > >        } > >        $link = mysql_connect($db_server, $db_username, $db_pass) or > die(mysql_error()); >        mysql_select_db($db_name, $link) or die(mysql_error()); >        $result = mysql_query($sql,$link) or die(mysql_error()); >        mysql_close($link); > > > > > } > > in the above code, ideally I would want to store the switch stuff > somewhere else... the question is what's the most elegant/proper way > of doing this... > > now... that switch could be included as is from a plain file, that's > easy enough... > > or it could be put into a function so that _select function internally > calls it and get the handle of the "$link" so that > mysql_select_db($db_name, $link) can run fine...  in that case, should > the $link be passed &$ by ref? any issues with that? > > which approach is better? or are there other issues that I must be > aware of in starting building such a library?

« previous php.general (#316291) next »