Tutorial # 1 using class.php: Connecting PHP to MySQL Database

Words
372
Reading
2 min
Listen
Play
8y

Picsture1.jpg
Hello everyone and all the programmers in the world. Today, I will gonna show you a different way of PHP programming using class.php and class. I include in this tutorial some basic way to code your PHP programs. Lets start.

How to connect PHP to MySQL server using class.php


Connect using traditional PHP coding

<?php 
    $host = "your hosting";  
    $user = "MySQL username";  
    $pass = "MySQL password";    
   $conn = @mysql_connect($host, $user, $pass);        if (!$conn) {          die('Could not connect: ' . mysql_error());   
   }      mysql_select_db($dbase, $conn);    ?>

Connect using class.php and class

<?php 
    include 'class.php';  
    $class=new main;  
    $class->database_connect("your host", 'username", "password", "database name");   
?>

The class.php is the source of your code and the functions build on it. Make sure you download class.php and include it into the folder where your index is located. The code $class=main; will tell the computer that you wanna include the main class of class.php.

Replace the host with your hosting name, the username is the MySQL server username and, password is also the password of your MySQL server and lastly is the database name is the name of the database you created.

Coding system with class.php is more simple and easy to understand. Make sure that you always put this code on the top of your code.

How to retrieved information from MySQL to PHP


I made a simple code that is very easy to understand. Retreiving information from MySQL Server to PHP is a process where information from database can be use to display in HTML form. Lets add the code here

<?php 
   include 'class.php';  
   $class=new main;  
   $class->database_connect("your host", 'username", "password", "database name");   
   $fetch = $class->sql_query("enter your mysql query here");  
   while($row = mysql_fetch_assoc($fetch)){  
    echo $row["field name"];  
   }  
?>

You should create a MySQL query and place it in $class->sql_query("enter your mysql query here") and the $row["field name"] displays the information from MySQL to html.

I can give you the class.php for free. Just send your email address in the comment below.
Tomorrow I will discuss how to retrieve information from MySQL to PHP.

Follow me alfren@alfren. Keep upvoting and resteeming. Support surpassinggoogle@surpassinggoogle and STEEMGIGS


Support and Follow ankarlie@ankarlie and the steemitpowerupph@steemitpowerupph


Tutorial # 1 using class.php: Connecting PHP to MySQL Database | Ecency