PHP Connection Mysql Database Steps

PHP Connection Mysql Database Steps

Today, We want to share with you PHP Connection Mysql Database Steps.
In this post we will show you PHP Connection Mysql Database Steps, hear for PHP Connection Mysql Database Steps we will give you demo and example for implement.
In this post, we will learn about Connection Mysql Database using PHP Example with an example.

File Structure using PHP DATABASE CONNECTION :

inc – foldername

filename : connection.php

filename : db_connection.php

filename : dbclass.php

filename : settings.php

The Plan simple database connection in PHP

  • make the connection and select the database.
  • perform the query on the table.
  • print out the data.
  • close the connection.
  • Read Also:  Vuejs color picker Plugins With Examples

    The first thing first of all to do is connect to the database connection.The used mysql function to connect to MySQL is called mysql_connect in database using PHP.

    settings.php

    <?PHP
    ob_start();
    session_start();
    /****LOCAL SERVER DATABASE CONNECTION ******/
    ////////////////// Local Host /////////////////////////
    	// $SERVER_HOST = "localhost";
    	// $SERVER_USER = "root";
    	 //$SERVER_PASSWORD = "";
        // $SERVER_DATABASE = "joyousse_accounts";
    /***************** GLOBLE VARIABLES *********************/
    	$SERVER_HOST = "localhost";
    
    	$SERVER_USER = "ngadmin";
    
    	$SERVER_PASSWORD = "[email protected]***";
    
    	$SERVER_DATABASE = "pakainfo_accounts";
    
    	$SITE_TITLE = "pakainfo admin Administrator";
    /********************************************************/
    	$base_url = "http://www.pakainfo.com/soft/";
     ob_end_clean();
    
    ?>
    

    connection.php

    <?PHP include("settings.php"); ?>
    <?PHP
    include("dbclass.php");
    /************ Class Object Define ***************/
    $DBObj = new DBConnection();
    $MSObj = new FuncMySql();
    $DBObj->Connect($SERVER_HOST, $SERVER_USER, $SERVER_PASSWORD, $SERVER_DATABASE); 
    ?>
    

    dbclass.php

    <?PHP
    class DBConnection {
    	function Connect($SERVER_HOST, $SERVER_USER, $SERVER_PASSWORD, $SERVER_DATABASE)
    	{
    		
    		
    		$CONN = @mysql_connect($SERVER_HOST, $SERVER_USER, $SERVER_PASSWORD) 
    				or die("SERVER CONNECTION IS FAILED : " .mysql_error() . ": ". mysql_errno());
    				
    				mysql_select_db($SERVER_DATABASE, $CONN) 
    				or die("DATABASE COULD NOT CONNECTED : " .mysql_error() . ": ". mysql_errno());
    				
     return $CONN;			
    				
    	}
    }
    
    class FuncMySql extends DBConnection {
       function __Connect() {
           parent::__Connect();
       }	
    	
       function Query($SQL) {
    		$Query = "";
    		$Query = mysql_query($SQL) 
    				 or die("QUERY FAILED : " . mysql_error() . ": " . mysql_errno() . " : " . $SQL);
    		return $Query;
    	}
    
    	function FetchArray($Query) {
    		$FetchArray = "";
    		$FetchArray = mysql_fetch_array($Query);
    		return $FetchArray;
    	}
    
    	function FetchAssoc($Query) {
    		$FetchAssoc = "";
    		$FetchAssoc = mysql_fetch_assoc($Query);
    		return $FetchAssoc;
    	}
    
    	function FetchAllData($Query) {
    		$FetchAllData = array();
    		$i=0;
    		while($Rows = mysql_fetch_assoc($Query)) {
    			foreach($Rows as $Key => $Values){
    				$FetchAllData[$i][$Key] = $Values;
    			}
    		$i++;	
    		}
    		//print_r($FetchAllData);
    		return $FetchAllData;
    	}
    	
    	function NumRows($Query) {
    		$NumRows = "";
    		$NumRows = mysql_num_rows($Query);
    		return $NumRows;
    	}
    }
    ?>
    

    db_connection.php

    <?PHP
    include("settings.php");
    	
    $CONN = mysql_connect($SERVER_HOST_NAME, $SERVER_USER_NAME, $SERVER_PASSWORD) or die("SERVER CONNECTION FAILED : ". mysql_error());
    	mysql_select_db($SERVER_DATABASE_NAME, $CONN) or die("SERVER DATABASE CONNECTION FAILED : ". mysql_error());
    ?>
    

    View Demo

    Read Also:  VueJs count array of total items Example

    We hope you get an idea about PHP Connection Mysql Database Steps
    We would like to have feedback on my Information blog .
    Your valuable any feedback, Good question, Inspirational Quotes, or Motivational comments about this article are always welcome.
    If you liked this post, Please don’t forget to share this as Well as Like FaceBook Page.

    We hope This Post can help you…….Good Luck!.

    Read Also:  jquery radio button checked

    Related FAQ

    Here are some more FAQ related to this Article:

    1. Read Also:  ng-minlength, ng-maxlength Validation using Angular Example
    2. Read Also:  laravel excel import Export to Database Example
    3. Read Also:  Simple Ajax Upload image using jQuery PHP
    4. Read Also:  PHP Google No CAPTCHA reCAPTCHA jQuery Ajax
    5. Read Also:  call to undefined function base_url() Fixed
    6. Read Also:  PHP Laravel 7 Inner Join Query Example (join query in laravel)
    7. Read Also:  PHP Multiple Checkbox Array Handling Tutorial Example
    8. Read Also:  PHP Multiple Checkbox Array Handling Tutorial Example
    9. Read Also:  PHP Google No CAPTCHA reCAPTCHA jQuery Ajax
    10. Read Also:  call to undefined function base_url() Fixed