Hello, i am trying to make a php file which will get a list of all of the databases in MySQL and then delete them all apart from one (named Database1).
The problem is this is coming up with two errors
Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in /home/learn2ha/public_html/list_mysql_dbs.php on line 21
Warning: mysql_free_result(): supplied argument is not a valid MySQL result resource in /home/learn2ha/public_html/list_mysql_dbs.php on line 55
I was wondering if anyone could see where i was going wrong?
<?php
// FILENAME: LIST_MYSQL_DBS.PHP
// Original Source Code From: "http://www.desilva.biz/mysql/listdbs.html"
// Source Code has been edited from original
define( 'NL', "\n" );
define( 'TB', ' ' );
// connecting to MySQL.
$conn = @mysql_connect( 'localhost', 'username', 'password' )
or die( mysql_errno().': '.mysql_error().NL );
// attempt to get a list of MySQL databases
// already set up in my account. This is done
// using the PHP function: mysql_list_dbs()
$result = mysql_list_dbs( $conn );
///* USING: mysql_fetch_object()
// ---------------------------
$database = "Database1";
while( $row = mysql_fetch_object( $result ) ):
if ($row->Database != $database)
{
//echo TB. '' .$row->Database.'<br>'.NL;
$m = $row->Database;
$conn = @mysql_connect( 'localhost', 'username', 'password' )
or die( mysql_errno().': '.mysql_error().NL );
$query = "DROP DATABASE $m";
$result = mysql_query($query)
or die( mysql_errno().': '.mysql_error().NL );
}
endwhile;
// Free resources / close MySQL Connection
mysql_free_result( $result );
mysql_close( $conn );
?>
Thanks alot in advance.
Please notice the note at the top of the code, as not all of this code is my own, it has been based around some pre-written code, which i have given a link to.