开发者

PHP OOP with MySQL

开发者 https://www.devze.com 2023-03-06 00:47 出处:网络
ok Guys have a big trouble and cann\'t anderstand what is going on. Please help. first of all I have a DB connection class:

ok Guys have a big trouble and cann't anderstand what is going on. Please help.

first of all I have a DB connection class:

    private $localhost = DB_LOCALHOST;
    private $db = DB_DATABASE;
    private $user = DB_USER;
    private $password = DB_PASSWORD;
    protected $link ;


    function __construct()
      {

       $this->link = mysql_connect($this->localhost,$this->user,$this->password) or die(m开发者_如何学Pythonysql_error()."<br/>".mysql_errno());
       mysql_query("SET NAMES ".DB_CHARSET , $this->link) or die(mysql_error()."<br/>".mysql_errno());
       mysql_select_db($this->db, $this->link)or die(mysql_error()."<br/>".mysql_errno());
           if($this->link===false)
           die("Didn't connect to DB");            

       }

       function sql_query($q)
       {    
          $r = mysql_query($q, $this->link) or die ("DATABASE ERROR! Please, contact the administrator.".mysql_error().);
          return $r;
       }

        function __destruct()
        {
           mysql_close($this->link);
        }

      }

then I use it in another one in two different functions:

     function del_meal($id_meal)
     {
         $db_s = new DBConnect();
     $db_s->sql_query("DELETE FROM `pictures` WHERE `id_target` = '{$id_meal}' AND `type` = 'meal';");
     $db_s->sql_query("DELETE FROM `meal_ingredient` WHERE `id_meal` = '{$id_meal}';"); 
     $db_s->sql_query("DELETE FROM `meal` WHERE `id_meal` = '{$id_meal}';"); 
     unset($db_s);
     $pictures = $this->select_pictures($id_meal,'meal');
         if(count($pictures)>0)
         {
           foreach($pictures as $pic)
           {
            if(file_exists("./pictures/meal/{$pic}"))
             {
               unlink("./pictures/meal/{$pic}");
               unlink("./pictures/meal/thumb/{$pic}");
             }
           }
         }
     }

    function del_category($id_category)
    {
       $db = new DBConnect();
       $id_category = $db->safe_var($id_category);
       $q = $db->sql_query("SELECT * FROM `meal` WHERE `id_category` ='{$id_category}' ");
    while($res = mysql_fetch_assoc($q))
    {
        $this->del_meal($res['id_meal']);
    }
    $db->sql_query("DELETE FROM `meal_category` WHERE `id_category` = '{$id_category}' ");
    unset($db);
    }

ok - here is safe_var()

   function safe_var($var)
  {
      $var = stripslashes($var);
      $var = trim($var);
      $var = strip_tags($var);
      $var = mysql_real_escape_string($var);
      $var = htmlspecialchars($var);
      $var = nl2br($var);

      return $var;
  }

So - when I use just del meal function - everything is fine - but when I use del category - I get "Warning: mysql_query(): 7 is not a valid MySQL-Link resource in DBConnect.php on line 33 DATABASE ERROR! Please, contact the administrator."

What can be the reason. Can somebody help me?


Like SkippyChalmers said, you need a single Database instance.

function db($config)
{
    static $db = NULL;
    if($db === NULL)
    {
        $db = new DBConnect($config);
    }
    return $db;
}

// db()->sql_query(...);

However, I recommend that you switch to PDO.


if($link===false)

Should be:

if($this->link===false)

Should it not?

Also, you can do better than this! Design it so the DB connection is persistent across all uses of the class. One new connection for every query is not good practice. Good start though. As Jason McCreary said, use mysqli, or PDO or something out of the box. Lot's of third party libraries out there too. Have you considered a data-mapper solution? This is a good start though!

EDIT: I'm going to recommend using a dependency injection container to properly persist the DB connection.


I council you, to Extend database class, or make DB class instance 1 time in construct.

Example with Extend:

class Anything Extends Database {
///and here you can use DB methods as $this->sql_query...
}

Second way is:

class Anything {

   private $db;

   public function __construct() {
      $this->db = new DBConnect();
      //then use db as $this->db->sql_query in the class with this same instance.
   }
}

I think these are right ways.

OR Try to edit your constructor. Try this:

function __construct()
      {

       $this->link = mysql_connect($this->localhost,$this->user,$this->password) or die(mysql_error()."<br/>".mysql_errno());
       mysql_select_db($this->db, $this->link)or die(mysql_error()."<br/>".mysql_errno());
       mysql_query("SET NAMES ".DB_CHARSET , $this->link) or die(mysql_error()."<br/>".mysql_errno());
           if($this->link===false)
           die("Didn't connect to DB");            

       }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号