PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Just thought I would pose the question. I am doing alot of large database updates, inserts, ect... and I am doing alot of my work with while loops and if then statements which works but at this point I believe there must be a better way.
<?php
// inserting data
// say you've got an array with all the INSERT command lines
$query = "";
for ($i = 0; $lines[$i]; $i++) {
$query .= $lines[$i];
if ($lines[$i + 1]) $query .= ";\n";
}
// next line inserts all the data at once
mysql_query($query) or die('failed doing that shizzle');
// parsing data
function get_sql($query) {
if ($result = mysql_query($query)) {
while ($rows[] = mysql_fetch_array($result, MYSQL_ASSOC)) {}
return $rows;
}
else return FALSE;
}
// next line puts all the results into a 2-dim array
$final_array = get_sql($query_array);
foreach ($final_array as $row) {
print "blah blah blah";
}
?>
The get_sql() function could be improved to distinguish between single-dimension and two-dimension arrays, etc, but that's the basic idea.
The key to database usage is to use a good database structure design. You need to use SQL joins to get only the data you want letting the DB do the work rather than Php.
I write a class (which perhaps takes a while to write depending upon the DB structure) but once I have that down I can play with my database really easily