Page 1 of 1

SQL class help

Posted: Wed May 31, 2006 10:19 am
by bogdan
Hi. I am trying to create my own SQL Class for learning purposes but something is wrong with it.

I keep getting a :
Warning: mysql_error(): supplied argument is not a valid MySQL-Link resource in /home/bogdan/public_html/work/database/CData.php on line 105
respectively: line 117

This is my code in the class:

Code: Select all

class CSql
{
      var $sqlHost;
      var $sqlUser;
      var $sqlPass;
      var $sqlDbName;
      var $sqlError;
      var $sqlHandler;
      var $sqlLink;
      
      
// function constructor
      function CSql()
      {
            $this->sqlHost   = "localhost";
            $this->sqlUser   = "root";
            $this->sqlPass   = "";
            $this->sqlDbName = "DnD";
            $this->sqlError  = "";   
      }

// function connect
   function Open($sqlHost="",$sqlUser="",$sqlPass="",$sqlDbName ="")
   {
      if ($sqlHost != "")   $this->sqlHost = $sqlHost; 
      else                  $sqlHost = $this->sqlHost;
     
      
      if ($sqlUser != "")   $this->sqlUser = $sqlUser; 
      else                  $sqlUser = $this->sqlUser;
      
      
      if ($sqlPass != "")   $this->sqlPass = $sqlPass; 
      else                  $sqlPass = $this->sqlPass;
      
      
      if ($sqlDbName != "") $this->sqlDbName = $sqlDbName; 
      else                  $sqlDbName = $this->sqlDbName;
      
      
                      
      $this->sqlHandler=mysql_connect($sqlHost,$sqlUser,$sqlPass);
      
      if (!$this->sqlHandler)
      {
               return($this->GetError());   
               //exit;    
      }
      else 
      {
               $this->sqlUse =mysql_select_db($this->sqlDbName,$this->sqlHandler);
               
               if ($this->sqlUse)
               {
                  return true;
               }
               else 
               {
                  return ($this->GetError());
                  mysql_close($this->sqlHandler);
                  //exit;
               }
      }
   }
   
//// create database
   function CreateDb($sqlDbName)
   {
               
      $this->sqlHandler = mysql_query("create database $sqlDbName");
            
      if (! $this->sqlHandler)
      {
         return $this->GetError();
         mysql_close($this->sqlHandler);
         //exit;
      }
      else
      {
         $this->sqlDbName = $sqlDbName;
         return true;
      }
   }
   
/////
   function GetError()
   {
      $this->sqlError=mysql_error("$this->sqlHandler");      // this is line 105
              
      if ($this->sqlError)
      {
         return("$this->sqlError");
      }
      else return true;          
   }
   
/////
   function Close()
   {
      mysql_close("$this->sqlHandler");   //this is line 117
   }
       
}

This from where I run it:

Code: Select all

include "CData.php";

$sqlCon = new CSql();
$sqlCon->Open("localhost","aaa","","DnD");
$sqlCon->CreateDb("alfa");
$sqlCon->Close();
User aaa does not have access to database DnD;
User aaa can not create a database;

Use root instead of aaa. error still applies so :(

Any help is greately apreciated.

Regards

Posted: Wed May 31, 2006 10:31 am
by feyd
sqlHandler is not a string and should not be converted to one; remove the quotes around it.

Posted: Wed May 31, 2006 10:38 am
by bogdan
Thanks for the fast reply.

I did remove the quotes and now with root and DnD everything is ok.

There's another thing: if I set the user to rrrrroot it doesn't give out an error. I was hoping it did return some error.
I may be tired and missing something..

The thing is no other user than root should create , update anything other than test database and tables within.