Welcome Guest, Not a member yet? Register   Sign In
Check database connectivity.
#1

[eluser]Daniel Samuels[/eluser]
I'm attempting to build a site "wizard" where the user will enter various bits of information for a website.
The section I am currently working on is the database connectivity bit, the user will enter the following information:
- hostname
- username
- password
- database

What is the best way to check that the values are all correct?
Currently I am using this:
Code:
$connect = $this->load->database($config, TRUE);
if($connect->conn_id == ""){
    echo "Not connected.";
}
else {
    echo "Connected.'";
}

But I'm not overly confident with it.. Does anyone know a better way?

Thanks!
#2

[eluser]TheFuzzy0ne[/eluser]
With the above code, if the database can't be connected to, CodeIgniter will show a database error, so really, if you want to handle the error, you'll need to catch it before that stage. The simplest way I can think of to achieve this, would be to extend the database method in the Loader class, and have it load your own version of database/DB.php, where you can handle these errors your own way.

EDIT: On second thoughts, it might actually be simpler if you do the testing without loading the database class. Only load it once you've confirmed that the database can be connected to. However, the you wouldn't be able to autoload the database.
#3

[eluser]Daniel Samuels[/eluser]
Okay then, I shall do that. Thanks!

On a similar note, using
Code:
$this->load->dbforge();
makes CodeIgniter output a lot of errors to do with the loader class.
Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined property: Site_Constructor::$db

Filename: libraries/Loader.php

Line Number: 278
Is this a related issue?

EDIT:
Never mind, got it sorted.
Thanks!
#4

[eluser]TheFuzzy0ne[/eluser]
It's a real shame you can't override the show_error() function in Common.php. Otherwise you would be able to set a variable containing the error message and then have your script handle the error.

If the show_error() function didn't have an exit() right at the end, you could have buffered the output, and checked the message with a regular expression. Pity...
#5

[eluser]Daniel Samuels[/eluser]
Yeah, and I can't extend the DB class either.
I'm going to use the dbforge tools to create databases and stuff, but there is no function to check if a database exists or not, which is odd.

Nevermind, I'll get there eventually..
#6

[eluser]TheFuzzy0ne[/eluser]
Actually, you can extend some of the database classes. It's just a case of extending the Loader.
http://codeigniter.com/wiki/Extending_Database_Drivers/
#7

[eluser]Daniel Samuels[/eluser]
[quote author="TheFuzzy0ne" date="1235857925"]Actually, you can extend some of the database classes. It's just a case of extending the Loader.
http://codeigniter.com/wiki/Extending_Database_Drivers/[/quote]

Ooh, useful! Many thanks indeed for that link - it will help a lot!

Thanks!




Theme © iAndrew 2016 - Forum software by © MyBB