Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jun 2005
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Aaah! I'm trying to design a CMS and can't get it to connect to database!

    I've been working on getting a content management system up for my site. But I can't get the **** to connect to my database! I've tried everything! Here's what I have so far:

    SystemComponent.php (store settings)
    Code:
    <?php
    class SystemComponent {
    
    var $settings;
    
    function getSettings() {
    
    // System variables
    $settings['siteDir'] = '/home/nymfans/public_html/bab/cmsadmin';
    
    // Database variables
    $settings['dbhost'] = 'localhost';
    $settings['dbusername'] = 'nymfans_nw';
    $settings['dbpassword'] = 'homicide';
    $settings['dbname'] = 'nymfans_nwcms';
    
    return $settings;
    
    }
    
    }
    ?>
    DbConnector.php (connect to database)
    Code:
    <?php
    ////////////////////////////////////////////////////////////////////////////////////////
    // Class: DbConnector
    // Purpose: Connect to a database, MySQL version
    ///////////////////////////////////////////////////////////////////////////////////////
    require_once 'SystemComponent.php';
    
    class DbConnector extends SystemComponent {
    
    var $theQuery;
    var $link;
    
    //*** Function: DbConnector, Purpose: Connect to the database ***
    function DbConnector(){
    
    // Load settings from parent class
    $settings = SystemComponent::getSettings();
    
    // Get the main settings from the array we just loaded
    $host = $settings['dbhost'];
    $db = $settings['dbname'];
    $user = $settings['dbusername'];
    $pass = $settings['dbpassword'];
    
    // Connect to the database
    $this->link = mysql_connect($host, $user, $pass);
    mysql_select_db($db);
    register_shutdown_function(array(&$this, 'close'));
    
    }
    
    //*** Function: query, Purpose: Execute a database query ***
    function query($query) {
    
    $this->theQuery = $query;
    return mysql_query($query, $this->link);
    
    }
    
    //*** Function: fetchArray, Purpose: Get array of query results ***
    function fetchArray($result) {
    
    return mysql_fetch_array($result);
    
    }
    
    //*** Function: close, Purpose: Close the connection ***
    function close() {
    
    mysql_close($this->link);
    
    }
    
    
    }
    ?>
    newArticle.php (post to DB)
    Code:
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>
    <?php
    // Get the PHP file containing the DbConnector class
    require_once('../includes/DbConnector.php');
    
    // Check whether a form has been submitted. If so, carry on
    if ($HTTP_POST_VARS){
    
    // Create an instance of DbConnector
    $connector = new DbConnector();
    
    // IMPORTANT!! ADD FORM VALIDATION CODE HERE - SEE THE NEXT ARTICLE
    
    // Create an SQL query (MySQL version)
    $insertQuery = "INSERT INTO cmsarticles (title,tagline,section,thearticle) VALUES (".
    "'".$HTTP_POST_VARS['title']."', ".
    "'".$HTTP_POST_VARS['tagline']."', ".
    $HTTP_POST_VARS['section'].", ".
    "'".$HTTP_POST_VARS['thearticle']."')";
    
    // Save the form data into the database 
    if ($result = $connector->query($insertQuery)){
    
    // It worked, give confirmation
    echo '<center><b>Article added to the database</b></center><br>';
    
    }else{
    
    // It hasn't worked so stop. Better error handling code would be good here!
    exit('<center>Sorry, there was an error saving to the database</center>');
    
    }
    
    }
    ?>
    
    <body>
    <form name="form1" method="post" action="newArticle.php">
            <p>&nbsp;Title:
              <input name="title" type="text" id="title">
            </p>
            <p>&nbsp;Tagline:
              <input name="tagline" type="text" id="tagline">
            </p>
            <p>&nbsp;Section:
              <input name="section" type="text" id="section">
            </p>
            <p>&nbsp;Article:
              <textarea name="thearticle" cols="50" rows="6" id="thearticle"></textarea>
            </p>
            <p align="center">
              <input type="submit" name="Submit" value="Submit">
            </p>
    </form>
    </body>
    </html>
    Help!

  • #2
    Regular Coder
    Join Date
    Nov 2004
    Location
    The Netherlands
    Posts
    551
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Why do you have two classes for a mysql connection?
    CATdude about IE6: "All your box-model are belong to us"

  • #3
    Regular Coder
    Join Date
    Aug 2004
    Location
    The US of A
    Posts
    767
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Furthermore, why didn't you censor the connection data?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •