Created
November 19, 2013 07:40
-
-
Save abdullahbutt/7541695 to your computer and use it in GitHub Desktop.
mysql_connect_function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // USING the OLD MySQL_connect API is an Anti-Pattern .... for reference ONLY! (Anti-Pattern means Bad-Practice) | |
| //echo phpinfo(); | |
| /* -------------------METHOD 1 | |
| $conn=mysql_connect('localhost','root','') or die ('Could NOT Connect'); | |
| mysql_select_db('practice',$conn); | |
| $result=mysql_query('SELECT * FROM users'); | |
| while($row=mysql_fetch_object($result)){ | |
| //print_r($row); | |
| echo $row->username."<br>"; | |
| }*/ | |
| /* -------------------METHOD 2 | |
| function connect($host='localhost',$username,$password){ | |
| return mysql_connect($host,$username,$password); | |
| }; | |
| $conn=connect('localhost','root',''); | |
| mysql_select_db('practice',$conn); | |
| $result=mysql_query('SELECT * FROM users'); | |
| while($row=mysql_fetch_object($result)){ | |
| //print_r($row); | |
| echo $row->username."<br>"; | |
| } | |
| */ | |
| // -------------------METHOD 3 | |
| /* function connect($host='localhost',$username,$password,$db=''){ | |
| $conn=mysql_connect($host,$username,$password); | |
| mysql_select_db('practice',$conn); | |
| //return $conn; | |
| } | |
| $conn=connect('localhost','root',''); | |
| $result=mysql_query('SELECT * FROM users'); | |
| while($row=mysql_fetch_object($result)){ | |
| echo $row->username."<br>"; | |
| }*/ | |
| /* function connect($host='localhost',$username,$password,$db=''){ | |
| $conn=mysql_connect($host,$username,$password); | |
| if(!empty($db)){ | |
| mysql_select_db($db,$conn); | |
| } | |
| return $conn; | |
| } | |
| $conn=connect('localhost','root','','practice'); | |
| $result=mysql_query('SELECT * FROM users'); | |
| while($row=mysql_fetch_object($result)){ | |
| echo $row->username."<br>"; | |
| } | |
| */ | |
| /* function connect($localhost='localhost',$username,$password,$db=''){ | |
| $conn=mysql_connect($localhost,$username,$password); | |
| if(!empty($db)){ | |
| $db=mysql_select_db($db,$conn); | |
| } | |
| return $conn; | |
| }; | |
| $conn=connect('localhost','root','','practice'); | |
| function query($query){ | |
| $result=mysql_query($query); | |
| while($row=mysql_fetch_object($result)){ | |
| echo $row->username."<br>"; | |
| } | |
| }; | |
| $result=query('SELECT * FROM users'); | |
| */ | |
| /* | |
| function connect($localhost='localhost',$username,$password,$db=''){ | |
| $conn=mysql_connect($localhost,$username,$password); | |
| if(!empty($db)){ | |
| $db=mysql_select_db($db,$conn); | |
| } | |
| return $conn; | |
| }; | |
| $conn=connect('localhost','root','','practice'); | |
| function query($query){ | |
| $result=mysql_query($query); | |
| if ($result){ | |
| while($row=mysql_fetch_object($result)){ | |
| echo $row->username."<br>"; | |
| } | |
| } | |
| }; | |
| $result=query('SELECT * FROM users'); | |
| */ | |
| /* function connect($localhost='localhost',$username,$password,$db=''){ | |
| $conn=mysql_connect($localhost,$username,$password); | |
| if(!empty($db)){ | |
| $db=mysql_select_db($db,$conn); | |
| } | |
| return $conn; | |
| }; | |
| $conn=connect('localhost','root','','practice'); | |
| function query($query){ | |
| $result=mysql_query($query); | |
| if ($result){ | |
| $rows=array(); | |
| while($row=mysql_fetch_object($result)){ | |
| $rows[]=$row; | |
| } | |
| return $rows; | |
| } | |
| return false; //means if $rows is blank, return false | |
| } | |
| $result=query('SELECT * FROM users'); | |
| print_r($result); | |
| */ | |
| function connect($localhost='localhost',$username,$password,$db=''){ | |
| $conn=mysql_connect($localhost,$username,$password); | |
| //if (!$conn){die('Could NOT connect'); } | |
| if(!empty($db)){ | |
| $db=mysql_select_db($db,$conn); | |
| } | |
| return $conn; | |
| }; | |
| // function query($query){ // we can add a parameter $conn as well as in below case | |
| function query($query,$conn){ | |
| $result=mysql_query($query,$conn); | |
| if ($result){ | |
| $rows=array(); | |
| while($row=mysql_fetch_object($result)){ | |
| //print_r($row); | |
| $rows[]=$row; //for each one, we are going to add one item to the array $rows... take each item from $row & append it to the array $rows | |
| } | |
| return $rows; | |
| } | |
| return false; //means if $rows is blank, return false | |
| } | |
| // $result=query('SELECT * FROM users'); //we need to add 2nd parameter $conn to support code on line 155 | |
| /* $result=query('SELECT * FROM users',$conn); | |
| if ($result){ | |
| foreach($result as $abc){ | |
| //print_r($abc); | |
| echo $abc->username."<br>"; | |
| } | |
| } | |
| */ | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment