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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.