[PHP] How to parse Inventory?
#4

Here is a nice script to help you not waste time creating classes for every, table, just nun it and it will create your php class files for you to use in your scripts(saves a lot of time).

<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "ghst";
$dbname = "newproject_db";

$mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}

echo "Checking directory: Database/".$dbname."<br>";
//directories creating the;
if(!is_dir("Database")){
mkdir("Database");
}

if(!is_dir("Database/".$dbname)){
mkdir("Database/".$dbname);
}

//create a query and execute
$query = "Show Tables";
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);
$info = $result->fetch_fields();
$table =array();

//for every row (table it founds)
for($i = 0;$row = $result->fetch_assoc(); $i++) {
$table[$i] = $row[$info[0]->name];
}

//open a file and write the data inot the file
$numTable = count($table);
echo " Tables foud:".$numTable."<br>";
for($i = 0; $i < $numTable; $i++){
echo "Writing: ".$table[$i]." to the file<br>";
$file = fopen("Database/".$dbname."/".$table[$i].".class.php", "w");
$fData = "<?php\n class ".$table[$i]."{\r\n";
$query = "describe ".$table[$i].";";
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);
$info = $result->fetch_fields();
//for every field in the table write the field name
if($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$field = $row[$info[0]->name];
$fData.= "\tpublic $".$field.";\r\n";
}
}
//finish the string and close the file
$fData .="};\r\n?>";
fwrite($file, $fData);
fclose($file);
}

//close mysql connection
mysqli_close($mysqli);
?>



Messages In This Thread
[No subject] - by SeaLife - 10-18-2012, 10:53 AM
[No subject] - by someone - 10-18-2012, 02:41 PM
[No subject] - by SeaLife - 10-18-2012, 03:09 PM
[No subject] - by someone - 10-18-2012, 04:11 PM
[No subject] - by Wizatek - 10-18-2012, 05:20 PM
[No subject] - by Wizatek - 10-18-2012, 05:33 PM
[No subject] - by SeaLife - 10-18-2012, 05:49 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)