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?
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.