Pastulio Posted June 25, 2007 Share Posted June 25, 2007 hey everybody, I have a question that should be quite simple. I'm writing a class to create Databases, and I'll also make one to create Tables. But how do I check if a database already exists? just by checking if you can select it like so: // Select the MySQL database $this -> db_select = mysql_select_db ($this -> database); // Test the database selection if (!$this -> db_select) { return false; } (don't mind the "$this" it's coded in OOP but I didn't think I would need to put it in the OOP forum for this simple example) So is this the right way to check it or is there another way? Quote Link to comment Share on other sites More sharing options...
chocopi Posted June 25, 2007 Share Posted June 25, 2007 you could use mysql_list_dbs() and do a check and see if its in the list ~ Chocopi Quote Link to comment Share on other sites More sharing options...
Pastulio Posted June 25, 2007 Author Share Posted June 25, 2007 so I should do something like this?: $db_list = mysql_list_dbs($connection); while ($row = mysql_fetch_array($db_list)) { $database_List[] = $row['database']; } And then use a for loop to check the array contents row by row, or could it be done with one loop? Quote Link to comment Share on other sites More sharing options...
TreeNode Posted June 25, 2007 Share Posted June 25, 2007 from php.net: If by chance no connection is found or established, an E_WARNING level warning is generated. This doesn't mean that the link will return false Quote Link to comment Share on other sites More sharing options...
Pastulio Posted June 25, 2007 Author Share Posted June 25, 2007 I feel quite stupid now because I don't know what you're talking about ??? Quote Link to comment Share on other sites More sharing options...
per1os Posted June 25, 2007 Share Posted June 25, 2007 so I should do something like this?: $db_list = mysql_list_dbs($connection); while ($row = mysql_fetch_array($db_list)) { $database_List[] = $row['database']; } And then use a for loop to check the array contents row by row, or could it be done with one loop? Yes but a for loop is not necessary, www.php.net/in_array should work just fine. Quote Link to comment Share on other sites More sharing options...
Pastulio Posted June 25, 2007 Author Share Posted June 25, 2007 Thanks a lot all *solved* Quote Link to comment Share on other sites More sharing options...
chocopi Posted June 25, 2007 Share Posted June 25, 2007 Shouldnt using the mysql_select_db work anyway, I just suggested the mysql_list_dbs() as its the only other one i know ~ Chocopi Quote Link to comment Share on other sites More sharing options...
Pastulio Posted June 25, 2007 Author Share Posted June 25, 2007 I don't really know but it looked quite noob to me, because what if the database could not be selected because of another reason (of which I know absolutely nothing) it would just give a big error. this is my final code: <?php function check_Existence () { $result = mysql_list_dbs ($this -> connection) while ($list = mysql_fetch_array($result)) { $database_list[] = $list['database']; } if (in_array($this -> database, $database_list)) { return true; } else { return false; } } ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.