Mammouth du PHP |
1029 Messages
09 mars 2007, 13:17
Voici un exemple de code tout droit sorti de min IDE favoris.
Il me semble assez clair.
N'hésite pas pour des infos complémentaires
<?php
//--------------------------------------------------------------------------
// This is where you specify your Database connection stuff
//
// mysql_connect -- Open a connection to a MySQL Server / die -- Alias of exit()
//
//--------------------------------------------------------------------------
$db_name = "mycompany"; //This is your database Name
$link = mysql_connect("localhost", "UserName", "PassWord") or die("Could not connect to server!");
//This is your table name. This is a one table config to do more table you will need to rework the code.
$table_name = 'userinfo';
$select_db = mysql_select_db($db_name, $link); // mysql_select_db -- Select a MySQL database
$query = "SELECT * FROM " . $table_name;
$result = mysql_query($query, $link) or die("Could not complete database query"); //mysql_query -- Send a MySQL query
$num = mysql_num_rows($result); //mysql_num_rows -- Get number of rows in result
if ($num != 0) {
//--------------------------------------------------------------------------
// If you would like to save a copy of the XML Doc being created uncomment this
// line and the two lines at the bottom containing fwrite and fclose.
//
//
//--------------------------------------------------------------------------
//$file= fopen("results.xml" , "w"); //fopen -- Opens file or URL
//--------------------------------------------------------------------------
// XML Header Tag Goes Here
//
//
//
//--------------------------------------------------------------------------
$_xml ="<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\r\n";
$_xml .="<results>\r\n";
//--------------------------------------------------------------------------
// This while loop loops throught the data found from the above query and
// generates the XML Doc.
//
//
//--------------------------------------------------------------------------
while ($row = mysql_fetch_array($result)) { //mysql_fetch_array -- Fetch a result row as an associative array, a numeric array, or both.
$_xml .=" <item>\r\n";
$_xml .=" <recordnumber>" . $row[RecordID] . "</recordnumber>\r\n";
$_xml .=" <username>" . $row[username] . "</username>\r\n";
$_xml .=" <address>" . $row[address] . "</address>\r\n";
$_xml .=" <city>" . $row[city] . "</city>\r\n";
$_xml .=" <state>" . $row[state] . "</state>\r\n";
$_xml .=" <zip>" . $row[zip] . "</zip>\r\n";
$_xml .=" <phonenumber>" . $row[phonenumber] . "</phonenumber>\r\n";
$_xml .=" <email>" . $row[email] . "</email>\r\n";
//You can use if statements to check if the returned value is null.
//If it is just place a default value in the tags like below.
if ($row[website]) {
$_xml .=" <website>http://" . $row[website] . "</website>\r\n";
} else {
$_xml .=" <website>http://www.ipdg3.com</website>\r\n";
}
$_xml .=" <deleted>" . $row[deleted] . "</deleted>\r\n";
$_xml .=" </item>\r\n";
}
$_xml .="</results>";
//--------------------------------------------------------------------------
// If you would like to save a copy of the XML Doc being created uncomment this
// line and the two lines at the bottom containing fwrite and fclose.
//
//
//--------------------------------------------------------------------------
//fwrite($file, $_xml); //fwrite -- Binary-safe file write
//fclose($file); //fclose -- Closes an open file pointer
//--------------------------------------------------------------------------
// This will echo the XML file out to the screen so you can view it.
//
//
//
//--------------------------------------------------------------------------
echo $_xml;
} else {
echo "No Records found";
}
?>
L'expérience est la somme de toutes nos erreurs.