Escolar Documentos
Profissional Documentos
Cultura Documentos
● Skip
Barattalo
● Skip
Jan ● Categories
25 ❍ Asp
2010
❍ Facebook Connect
Here is my personal collection of 10 php function that I always
include in my database function library.
I think they are very usefull and I’d like to share with you. ❍ Google Maps
❍ MsSQL
1 function connectDb() {
3 // working db
Php
4 if (mysql_connect( DBHOST, DBUSER, DBPWD ) && mysql_select_db( DBNAME ))
❍
10 } ❍ Twirl.it
11 }
EXAMPLE: So, make a php file cronfixdb.php with this code and call everynight to fix your tables:
● Print and study
❍ Building website for iphone
3 if connectDb() fixTables(DB);
❍ htaccess mp3
01 function getHtmlTable($result){
04 $out = '<table>';
● Tags
05 for($i = 0; $i < mysql_num_fields($result); $i++){
❍ Php
06 $aux = mysql_field_name($result, $i);
10 $out .= "<tr>";
❍ MySql
11 foreach ($linea as $valor_col) $out .= '<td>'.$valor_col.'</td>';
12 $out .= "</tr>";
❍ Regular expression
13 }
16 } ❍ Web bot
❍ Google Maps
1 <?
2 include("db.lib.php");
❍ curl
3 if connectDb() {
6 } ❍ Google
7 ?>
❍ Php
FUNCTION 4: MAKE A STRING WITH FIELD NAMES
Found the fields-name of a table and return them in a comma-separeted string. This is usefull to automatic
❍ spiders
build queries in some applications. The $excepts parameter can be used to skip some field in the output
string:
❍ Spider
4 $out = "";
7 return $out ;
8 }
1 function getCommaValues($sql) {
5 $out = "";
7 while($r=mysql_fetch_row($rs)) $out.=($out?",":"").$r[0];
8 return $out;
9 }
1 ...
4 if ($ids) mysql_query($sql);
5 // note since mysql 5 you can use sub select in delete statements.
6 ...
06 $row = mysql_fetch_array($result);
08 else $row[1]=str_replace("set('","",$row[1]);
09 $row[1]=str_replace("','","\n",$row[1]);
10 $row[1]=str_replace("')","",$row[1]);
11 $ar = split("\n",$row[1]);
13 return $arOut ;
14 }
01 function getScalar($sql,$def="") {
05 if (mysql_num_rows($rs)) {
06 $r = mysql_fetch_row($rs);
07 mysql_free_result($rs);
08 return $r[0];
09 }
10 return $def;
11 }
EXAMPLE:
01 function getRow($sql) {
05 if (mysql_num_rows($rs)) {
06 $r = mysql_fetch_array($rs);
07 mysql_free_result($rs);
08 return $r;
09 }
10 mysql_free_result($rs);
11 return "";
12 }
EXAMPLE:
http://www.barattalo.it/2010/01/25/10-php-usefull-functions-for-mysql-stuff/ (4 of 8)15/02/2010 07:11:07 p.m.
Barattalo » 10 PHP usefull functions for MySQL stuff
1 function duplicateRow($table,$primaryField,$primaryIDvalue) {
4 $fields = getCommaFields($table,$primaryField);
5 $sql = "insert into $table ($fields) select $fields from $table where
$primaryField='".mysql_real_escape($primaryIDvalue)."' limit 0,1";
6 mysql_query($sql) or die(mysql_error().$sql);
7 return mysql_insert_id();
8 }
EXAMPLE:
1 // duplicate the user with id_user=12 and get the new user id:
2 $newuser = duplicateRow("users","id_user","12");
04 $jsonArray = array();
05 $csvString = "";
06 $csvcolumns = "";
07 $count = 0;
08 while($r = mysql_fetch_row($rs)) {
11 $csvString.=",\"".$r[$k]."\"";
12 }
14 $csvString.="\n";
15 $count++;
16 }
17 $jsondata = "{\"$jsonmain\":".json_encode($jsonArray)."}";
18 $csvdata = str_replace("\n,","\n",$csvcolumns."\n".$csvString);
19 return ($type=="csv"?$csvdata:$jsondata);
20 }
EXAMPLE:
3 $rs = mysql_query($sql);
Related posts:
Tags: csv, function, json, MySql, mysql_fetch_array, mysql_query, optimize, Php, Recordset, repair, snippets
Comments (6)
1.
Stfr says:
January 26th, 2010 12:27 pm
2.
admin says:
January 26th, 2010 12:49 pm
Mmm ok, in the future I will use mysqli, that’s better and I have to study it.
But why PDO? PDO should be used when you plan to change db, or when you have
different dbs to connect to.
3.
Stfr says:
January 26th, 2010 1:27 pm
Sure, but you can use it in order to create ORM or just in order to use Object syntax for
your Db.
It depends on the use you do and the importance of the project.
4.
Notable Tech Posts – 2010.01.31 | The Life of Lew Ayotte says:
February 1st, 2010 2:32 pm
5.
6.
10 PHP usefull functions for MySQL stuff | DbRunas says:
February 11th, 2010 9:50 pm
Leave a Reply
Name (required)
Website
Submit Comment