Jump to content

Help with array


Dustin013

Recommended Posts

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.