note 47516 added to function.mysql-query
| From: | andregodin at gmail dot com | Date: | Thu, 18 Nov 2004 21:03:40 +0000 |
| Subject: | note 47516 added to function.mysql-query | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-80721@lists.php.net to get a copy of this message | ||
Another "dumping" function but with the optional possibility to choose wich field_name to
be dumped. "Have Fun and please email me if you do optimization of this code"
<?php
function mysql_format($strTemp){
//Format sql sentence for insert
$bad_chars= array("\\", "'", "\"");
$good_chars = array("\\\\", "''", "\"\"");
return str_replace($bad_chars, $good_chars, $strTemp);
}
function mysql_dump_table(){
/*******************\
* MYSQL_DUMP_TABLE *********************************\
* Paramêtres : *
* 1- Table Name *
* 2- Field(s) (in string format) Name to be dumped *
* If empty, all field will be dumped *
\******************************************************/
if (!(func_num_args())) die ("<b>mysql_dump_table</b>: Need At Least A Table
Name");
$arg_list = func_get_args();
$arrReturn = "";
$strTblName = mysql_format("
{$arg_list[0]}");
$strFields = "*";
if (func_num_args() > 1){
$strFields = "";
for ($noArg=1; $noArg<func_num_args(); $noArg++){
if ($strFields) $strFields .= ", ";
$strFields .= "$arg_list[$noArg]";
}
}
$result = mysql_query("SELECT $strFields FROM $strTblName") or die ("Incapable
d'exécuter la requête");
$nbRecord = 0;
while ($row = mysql_fetch_assoc($result)){
$strFieldsNames = "";
$strFieldsValues = "";
foreach ($row as $field_name => $field_value){
if ($strFieldsNames) $strFieldsNames .= ", ";
$strFieldsNames .= "$field_name";
if($strFieldsValues) $strFieldsValues .= ", ";
$strFieldsValues .= "'" . mysql_format($field_value) . "'";
}
$arrReturn[$nbRecord++] = "INSERT INTO $strTblName($strFieldsNames)
values($strFieldsValues);";
}
mysql_free_result($result);
return $arrReturn;
}
require_once("config_connexion_db_test.php");
/****************
* AUTRE EXEMPLE **********************************************
* Vous pouvez décider d'afficher quelques champs seulements *
* en spécifiant les champs désiré *
**************************************************************/
$db = mysql_connect(DBTEST_HOST, DBTEST_USER, DBTEST_PASSWORD) or die("Impossible de se
connecter : ");
mysql_select_db(DBTEST_NAME, $db);
$result = mysql_dump_table("tbl_produits", "code_produit",
"description");
foreach ($result as $sentence){
//Afficher chaque élément du tableau
print "$sentence<br>";
}
mysql_close($db);
//Retourne
/********************************************************
INSERT INTO tbl_produits(code_produit,description)
VALUES('produit1', 'don\'t have description');
INSERT INTO tbl_produits(code_produit,description)
VALUES('produit2', 'without \"description\" too');
INSERT INTO tbl_produits(code_produit,description)
VALUES('produit3', '1\\3 more than before');
...
...
*********************************************************/
?>
PHP is a very nice language!
André Godin :)
----
Manual Page -- http://www.php.net/manual/en/function.mysql-query.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+47516
Delete -- http://master.php.net/manage/user-notes.php?action=delete+47516&report=yes
Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+47516&report=yes&reason=bad+code
Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+47516&report=yes&reason=spam
Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+47516&report=yes&reason=useless+example
Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+47516&report=yes&reason=contains+commercial+links
Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+47516&report=yes&reason=useless+note
Reject -- http://master.php.net/manage/user-notes.php?action=reject+47516&report=yes
Search -- http://master.php.net/manage/user-notes.php