Dustin013 Posted June 17, 2008 Share Posted June 17, 2008 I have been trying to figure this out but I guess my syntax has been all messed up... I have 18 tables I want to search for any blank fields and remove the post. I could simply type out all the queries but I know that is not the best way to do it. <?php include 'include/config.php'; include 'include/connect.php'; mysql_select_db($dbname); $query = "DELETE FROM SOMETABLE WHERE FIELD = ''"; $result = mysql_query($query) or die('Error, this query failed'); $query2 = "DELETE FROM SOMETABLE2 WHERE FIELD = ''"; $result2 = mysql_query($query) or die('Error, this query failed'); $query3 = "DELETE FROM SOMETABLE3 WHERE FIELD = ''"; $result3 = mysql_query($query) or die('Error, this query failed'); //Blah Blah Blah... anyway to define all the table names and simply create one query to loop through them all? ?> Link to comment https://forums.phpfreaks.com/topic/110648-help-with-array/ Share on other sites More sharing options...
hitman6003 Posted June 17, 2008 Share Posted June 17, 2008 $result = mysql_list_tables('mysql'); while ($row = mysql_fetch_row($result)) { $query = "DELETE FROM " . $row[0] . " WHERE FIELD = ''"; $result = mysql_query($query) or die($query . "<br />" . mysql_error()); } Link to comment https://forums.phpfreaks.com/topic/110648-help-with-array/#findComment-567643 Share on other sites More sharing options...
Dustin013 Posted June 17, 2008 Author Share Posted June 17, 2008 First and foremost thanks.. that is exactly what I needed to find. When I run it here is what I get displayed in my browser... Table: SOMETABLE DELETE FROM SOMETABLE WHERE _TOPIC_ID = '' Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in C:\Program Files\VertrigoServ\www\testsite\include\cleaner.php on line 8 Here is my Code <?php include 'config.php'; include 'connect.php'; mysql_select_db($dbname); $result = mysql_list_tables($dbname); while ($row = mysql_fetch_row($result)) { echo "Table: {$row[0]}<br />"; $query = "DELETE FROM {$row[0]} WHERE _TOPIC_ID = ''"; echo $query; echo "<br />"; $result = mysql_query($query) or die($query . "<br />" . mysql_error()); } ?> Link to comment https://forums.phpfreaks.com/topic/110648-help-with-array/#findComment-567652 Share on other sites More sharing options...
hitman6003 Posted June 17, 2008 Share Posted June 17, 2008 You are reassigning the value of $result in your loop. Use a different variable name for the result of mysql_query inside the while loop. Link to comment https://forums.phpfreaks.com/topic/110648-help-with-array/#findComment-567655 Share on other sites More sharing options...
Dustin013 Posted June 17, 2008 Author Share Posted June 17, 2008 Duh! Thanks man :-) Works perfectly! That mysql_list_tables will help me a lot in the future :-) Oh the joys of learning php... hehe Here is the completed code working in case anyone else ever needs it! <?php include 'config.php'; include 'connect.php'; mysql_select_db($dbname); $result = mysql_list_tables($dbname); while ($row = mysql_fetch_row($result)) { echo "Table: {$row[0]} was selected...<br />"; $query = "DELETE FROM {$row[0]} WHERE _TOPIC_ID = ''"; echo "The following query was run : ".$query." sucessfully!"; echo "<br />"; $result2 = mysql_query($query) or die($query . "<br />" . mysql_error()); } ?> Link to comment https://forums.phpfreaks.com/topic/110648-help-with-array/#findComment-567661 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.