Database connecting

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
roguetide
Forum Newbie
Posts: 2
Joined: Thu Jan 21, 2010 11:21 am

Database connecting

Post by roguetide »

Hey guys, I realize that there is another post about 3 down from mine (viewtopic.php?f=1&t=111800) that addresses a similar issue, but not quite the same problem.

I'm using CodeIgnighter as a framework to try to connect a mySQL database (created through phpmyadmin, if that makes a difference?) So I have the database set up as follows;

Code: Select all

 
<?php  if ( ! defined('BASEPATH')) exit('...');
 
$active_group = "default";
$active_record = TRUE;
 
$db['default']['hostname'] = "localhost";
$db['default']['username'] = "**";
$db['default']['password'] = "**";
$db['default']['database'] = "**";
$db['default']['dbdriver'] = "mysql";
$db['default']['dbprefix'] = "";
$db['default']['pconnect'] = FALSE;
$db['default']['db_debug'] = FALSE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = "";
$db['default']['char_set'] = "utf8";
$db['default']['dbcollat'] = "latin1_swedish_ci";
 
?>
 


And I'm connecting using the database code within code ignighter too, which is;

Code: Select all

<?php
 
function &DB($params = '', $active_record_override = FALSE)
{
    if (is_string($params) AND strpos($params, '://') === FALSE)
    {
        include(APPPATH.'config/database'.EXT);
        
        if ( ! isset($db) OR count($db) == 0)
        {
            show_error('No database connection settings were found in the database config file.');
        }
        
        if ($params != '')
        {
            $active_group = $params;
        }
        
        if ( ! isset($active_group) OR ! isset($db[$active_group]))
        {
            show_error('You have specified an invalid database connection group.');
        }
        
        $params = $db[$active_group];
    }
    elseif (is_string($params))
    {
    
        if (($dns = @parse_url($params)) === FALSE)
        {
            show_error('Invalid DB Connection String');
        }
        
        $params = array(
                            'dbdriver'  => $dns['scheme'],
                            'hostname'  => (isset($dns['host'])) ? rawurldecode($dns['host']) : '',
                            'username'  => (isset($dns['user'])) ? rawurldecode($dns['user']) : '',
                            'password'  => (isset($dns['pass'])) ? rawurldecode($dns['pass']) : '',
                            'database'  => (isset($dns['path'])) ? rawurldecode(substr($dns['path'], 1)) : ''
                        );
        
        if (isset($dns['query']))
        {
            parse_str($dns['query'], $extra);
 
            foreach($extra as $key => $val)
            {
                if (strtoupper($val) == "TRUE")
                {
                    $val = TRUE;
                }
                elseif (strtoupper($val) == "FALSE")
                {
                    $val = FALSE;
                }
 
                $params[$key] = $val;
            }
        }
    }
    
    if ( ! isset($params['dbdriver']) OR $params['dbdriver'] == '')
    {
        show_error('You have not selected a database type to connect to.');
    }
    
    if ($active_record_override == TRUE)
    {
        $active_record = TRUE;
    }
    
    require_once(BASEPATH.'database/DB_driver'.EXT);
 
    if ( ! isset($active_record) OR $active_record == TRUE)
    {
        require_once(BASEPATH.'database/DB_active_rec'.EXT);
        
        if ( ! class_exists('CI_DB'))
        {
            eval('class CI_DB extends CI_DB_active_record { }');
        }
    }
    else
    {
        if ( ! class_exists('CI_DB'))
        {
            eval('class CI_DB extends CI_DB_driver { }');
        }
    }
            
    require_once(BASEPATH.'database/drivers/'.$params['dbdriver'].'/'.$params['dbdriver'].'_driver'.EXT);
 
    $driver = 'CI_DB_'.$params['dbdriver'].'_driver';
    $DB =& instantiate_class(new $driver($params));
    
    if ($DB->autoinit == TRUE)
    {
        $DB->initialize();
    }
    
    return $DB;
}   
?>
I've barely touched upon PHP before, so I'm not sure if I'm doing anything wrong here, the layout of folders to files is correct so it's not the structure of the base files or anything. I'm also pretty sure that these set of codes won't print the database on screen either?

I'm trying to create a frontend that'll allow the user to input data then search for data at another time (it's a database for offenders at a resort). Any help is appreciated!

TIA RT.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Database connecting

Post by Eran »

Is there a question in here? I must have missed it. What problems are you encountering?
roguetide
Forum Newbie
Posts: 2
Joined: Thu Jan 21, 2010 11:21 am

Re: Database connecting

Post by roguetide »

Oh, sorry I didn't write up what I actually needed, lol.

Uh, I'm trying to figure out how to get the data to print/echo on screen upon connecting.
aravona
Forum Contributor
Posts: 347
Joined: Sat Jun 13, 2009 3:59 pm
Location: England

Re: Database connecting

Post by aravona »

roguetide wrote:Oh, sorry I didn't write up what I actually needed, lol.

Uh, I'm trying to figure out how to get the data to print/echo on screen upon connecting.
if connected echo whatever you want?

Put the echo and whatever your wanting to put onscreen after the point where your finally connected (you're using a more complex log in than I'm used to lol)
roguetide wrote:I'm also pretty sure that these set of codes won't print the database on screen either.
doesnt look that way to me but you can get the database contents onscreen in a long string using:

Code: Select all

$variable = "Select * from Database";
echo $variable;
but you can put it in a list using something like:

Code: Select all

$variable = "select * from database;";
       $result = mysql_query($variable);
 
       while($row = mysql_fetch_array($result)) {
      echo "$row[id] $row[name] <br/>";
       }
but obviously tweaked to your own needs.
Post Reply