Jump to content

Access to mysql database using php


novice_programmer

Recommended Posts

I'm trying to use the code below to access a mysql database, but i cant seem to figure out why it isn't working. I'm novice at this so excuse my ignorance. Any help is appreciated.

 

<?php
require_once '/home/tirhonec/php/DB.php';
require_once '/home/tirhonec/php/XML/Feed/Parser.php';

class DatabaseConnection
{
  public static function get()
  {
    static $db = null;
    if ( $db == null )
      $db = new DatabaseConnection();
    return $db;
  }

  private $_handle = null;

  private function __construct()
  {
    $sqlinfo = array('localhost', 'username', 'password', 'tirhonec_db');
    $dsn = "mysqli://".$sqlinfo[1].":".$sqlinfo[2]."@".$sqlinfo[0]."/".sqlinfo[3];
    $this->_handle =& DB::Connect($dsn);
  }
  
  public function handle()
  {
    return $this->_handle;
  }
}

class FeedList
{
  public static function add( $url )
  {
    if ( FeedList::getFeedByUrl( $url ) != null )
      return;

    $db = DatabaseConnection::get()->handle();

    $tirhonec_db =& new XML_Feed_Parser( $url );
    $tirhonec_db->parse();

    $info = $rss->getChannelInfo();

    $isth = $db->prepare( "INSERT INTO rss_feeds VALUES( null, ?, ?, null )" );
    $db->execute( $isth, array( $url, $info['title'] ) );

    $info = FeedList::getFeedByUrl( $url );
    Feed::update( $info['rss_feed_id'] );
  }

  public static function getAll( )
  {
    $db = DatabaseConnection::get()->handle();
    $res = $db->query( "SELECT * FROM rss_feeds" );
    $rows = array();
    while( $res->fetchInto( $row, DB_FETCHMODE_ASSOC ) )
    {
      $rows []= $row;
    }
    return $rows;
  }

  public static function getFeedInfo( $id )
  {
    $db = DatabaseConnection::get()->handle();
    $res = $db->query( "SELECT * FROM rss_feeds WHERE rss_feed_id=?", array( $id ) );
    while( $res->fetchInto( $row, DB_FETCHMODE_ASSOC ) )
    {
      return $row;
    }
    return $null;
  }


  public static function getFeedByUrl( $url )
  {
    $db = DatabaseConnection::get()->handle();
    $res = $tirhonec_db->query( "SELECT * FROM rss_feeds WHERE url=?", array( $url ) );
    while( $res->fetchInto( $row, DB_FETCHMODE_ASSOC ) )
    {
      return $row;
    }
    return null;
  }

  public static function update()
  {
    $db = DatabaseConnection::get()->handle();

    $usth1 = $db->prepare( "UPDATE rss_feeds SET name='' WHERE rss_feed_id=?" );
    $usth2 = $db->prepare( "UPDATE rss_feeds SET name=? WHERE rss_feed_id=?" );

    $res = $db->query( "SELECT rss_feed_id,name FROM rss_feeds WHERE last_update<now()-600" );
    while( $res->fetchInto( $row, DB_FETCHMODE_ASSOC ) )
    {
      Feed::update( $row['rss_feed_id'] );
      $db->execute( $usth1, array( $row['rss_feed_id'] ) );
      $db->execute( $usth2, array( $row['name'], $row['rss_feed_id'] ) );
    }
  }
}

class Feed
{
  public static function update( $id )
  {
    $db = DatabaseConnection::get()->handle();

    $info = FeedList::getFeedInfo( $id );
    $tirhonec_db =& new XML_Feed_Parser( $info['url'] );
    $tirhonec_db->parse();

    $dsth = $db->prepare( "DELETE FROM rss_articles WHERE rss_feed_id=?" );
    $db->execute( $dsth, array( $id ) );

    $isth = $db->prepare( "INSERT INTO rss_articles VALUES( ?, ?, ?, ? )" );

    foreach ($tirhonec_db->getItems() as $item) {
      $db->execute( $isth, array( $id,
        $item['link'], $item['title'],
        $item['description'] ) );
    }
  }

  public static function get( $id )
  {
    $db = DatabaseConnection::get()->handle();
    $res = $tirhonec_db->query( "SELECT * FROM rss_articles WHERE rss_feed_id=?", array( $id ) );
    $rows = array();
    while( $res->fetchInto( $row, DB_FETCHMODE_ASSOC ) )
    {
      $rows []= $row;
    }
    return $rows;
  }
}
?>

Link to comment
Share on other sites

There are other files, php, that calls on this one to get the info from the database. Everything else seems to work except for actual access to the database. Like my name suggests, I'm a novice at this. Let me know if you need the other files as well and I'll point you to a link to obtain them. Thanks for your time and for responding.

 

Oh! as for error messages, the only ones I was able to see referred to the path of the "use once" php files being incorrect. I corrected that error, but am not sure how to troubleshoot the actual accessing of the database.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.