1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

php mysql class

Discussion in 'PHP' started by dracula51, May 4, 2012.

  1. #1
    hi
    to make things easier & safe...i use this class to connect & run query to mysql from php
    class mysql{
    
    	function query($qdata){
    		global $hostname, $db_username, $db_password, $database;
    		$con = mysql_connect($hostname,$db_username,$db_password);
    		 if (!$con)
    		   {
    		   die('Could not connect: ' . mysql_error());
    		   }
    		mysql_select_db($database, $con);
    		$query = mysql_query($qdata) or die(mysql_error());
    		mysql_close($con);
    	}
    	
    	function fetch_array($query){
    		$array = mysql_fetch_array($query);
    		return $array;
    	}
    	
    	function num_rows($query){
    		$num = mysql_num_rows($query);
    		return $num;
    	}
    	
    	function result($query){
    		$result = mysql_result($query,0);
    		return $result;
    	}
    }
    
    $mysql = new mysql;
    PHP:
    so whenver i need, i call it like
    $result = $mysql->query("SELECT * FROM table");
    $row = $mysql->fetch_array($result);
    PHP:
    so my first question is, IS it a good practice? (plz read the whole codes plzz)
    any improvment needed for this class??
    any suggestion?


    2nd question, sometimes i need to use mysql_insert_id
    but it doesnt work with the class.
    i tried like this:
    $mysql->query("INSERT INTO `table`..........");
    $id = mysql_insert_id();
    PHP:
    but it returns 0
    how can i add this to my class? or how can i use it?
     
    dracula51, May 4, 2012 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    A few words: Learn to use PDO instead of mysql_ (or, at a bare minimum, use mysqli_ instead).

    PDO already have all these things built in, and you don't have to reinvent the wheel to do stuff.
     
    PoPSiCLe, May 5, 2012 IP