Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

avoiding MySQL error messages being printed out

Status
Not open for further replies.

martinasv

Technical User
Feb 12, 2006
24
HR
Hi!

I wrote this little peace of code, which is supposed to check if a certain database exists, and if not, create one.
(Maybe there's a much better way to do it, but I don't know it)

Anyway, here's the code:

use DBI;

$username = 'root';$password = '';$database = 'tina'; $hostname = '';
if (!($dbh = DBI->connect("dbi:mysql:database=$database;" .
"host=$hostname;port=3306", $username, )))
{
$result=`mysqladmin -u root create tina`;
print "Creating dabase...\n";
}

Basically, it's doing what I wanted, but on top of creating a database, it still prints out the error:
DBI connect('database=tina;host=;port=3306','root',...) failed: Unknown database 'tina'

Is there a way I can avoid that error being printed out?
 
I wouldn't shell out to mysqladmin. You can do it all in a CREATE DATABASE statement, since that can be told to only create the database if it doesn't already exist.

Try this:
Code:
$username = 'root';
$password = '';
$database = '';
$hostname = '';
$dbh = DBI->connect("dbi:mysql:database=$database;"."host=$hostname;port=3306", $username );
print "Creating dabase...\n" if ( $dbh->do( 'CREATE DATABASE IF NOT EXISTS tina' ) == 1 );
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top