topical media & game development
[] readme course(s) preface I 1 2 II 3 4 III 5 6 7 IV 8 9 10 V 11 12 afterthought(s) appendix reference(s) example(s) resource(s) _

talk show tell print

portal-classes-core.db.php



  <?PHP
  error_reporting(E_ALL);
  
  class Db {
  
          // Basic rules:
          // Als de query niet werkt/fout gaat wordt er false geretourneerd
          // Als het resultaat leeg is, wordt er een leeg resultaat/array teruggestuurd
          // Als er een resultaat terug verwacht wordt, wordt er dus nooit true geretourneerd
  
          var core;
          var connection;
  
          function Db (&core) {
                  this->core = &core;
                  this->connectDb(); // Nog geen db...
          }
          
          function connectDb () {
                  dbHost        = 'tornado';
                  dbUser        = 'mwg400';
                  dbPass        = '48458749';
                  dbName        = 'mwg400-dbs';
                  
                  this->connection = mysql_connect(dbHost,dbUser,dbPass);
                  mysql_select_db(dbName, this->connection);
          }
          
          function getQueryNumRows (sql) {
                  if (query = mysql_query(sql)) {
                          return mysql_num_rows(query);
                  } 
                  return false;
          }
          
          function getLastInsertedId () {
                  if (mysql_insert_id() == 0) {
                          return false;
                  }
                  return mysql_insert_id();
          }
          
          function getQueryResults (sql) {                
                  if ( !(query = mysql_query(sql)) ) {
                          echo "Error:  ".mysql_error();
                          // Error afhandeling
                          echo '<br><br>De foute sql:<br>'.sql.'<br><br>';
                          return false;
                  }
                                  
                  i = 0;
                  resultArray = array();
                  while (row = mysql_fetch_array(query, MYSQL_ASSOC)) {
                          resultArray[i] = row;
                          i++;
                  }                
                  return resultArray;
          }
          
          function getQueryResult (sql) {                
                  if ( !(query = mysql_query(sql)) ) {
                          // Extra error afhandeling
                          echo '<br><br>De foute sql:<br>'.sql.'<br><br>';
                          return false;
                  }
                  resultArray = mysql_fetch_array(query, MYSQL_ASSOC);
                  return resultArray; // Retourneerd false als er geen rij is
          }
          
          function getPlainQueryResult (sql) {
                  if ( !(query = mysql_query(sql)) ) {
                          // Extra error afhandeling
                          echo '<br><br>De foute sql:<br>'.sql.'<br><br>';
                          return false;
                  }
                  return query;
          }
          
          function doQuery (sql) {
                  if (mysql_query(sql)) {
                          return true;
                  }
                  echo "Error:  ".mysql_error();
                  echo '<br><br>De foute sql:<br>'.sql.'<br><br>';
                  // Error afhandeling
                  return false;
          }
  }
  ?>


(C) A. Eliëns 2/9/2007

You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.