Jump to content

PHP - Getting data from your database. **Noob Friendly!**


xXMADEXx

Recommended Posts

This tutorial is brought to you by Revolution of Scripts.

Also avalible here.

Hello guys. So, in this tutorial I am going to teach you guys how to get data from your MySQL database. First of all, you will need to have the following:

XAMPP or USBWebhost 
MySQL Database (Most Apache's support them) 

So, now that you have your MySQL database up and running you will need to make a new file in the htdocs file directory. For this tutorial we are going to use two files:

Database.php - Where database executions will be created 
Index.php - Where we will list he given data  

database.php

So, to start off, go ahead and make the database.php file. Inside the database.php file you'll need the PHP tags:

<?php 
  
?> 

The PHP functions that we will be using are: mysqli_connect & mysqli_query. Inside the PHP tags in database.php, make a new function and name it "executeDatabase."

<?php 
    function executeDatabase ( ) { 
     
  
    } 
?> 

The executeDatabase will be the only thing we will be working with inside database.php. Inside the function, create some new variables to define your host, username, password and database name.

<?php 
    function executeDatabase ( ) { 
         
        $user = "xXMADEXx"; 
        $pass = "databasePassword"; 
        $host = "127.0.0.1"; 
        $dbname = "ForumExample"; 
  
    } 
?> 

Your doing great! Just keep calm and code on!

Now that we have our connection info entered in, we will make a variable of "$dbc" to define the MySQL connection using mysqli_connect.

mysqli_connect arguments:

bool mysqli_connect ( string $host, string $username, string $password, string $database); 
//$host: The MySQL Host 
//$username: The username to the MySQL database 
//$password: The password to the username of the database. 
//$database: The name of the database that you'll be using 

So, now lets make the connection in database.php!!

<?php 
    function executeDatabase ( ) { 
         
        $user = "xXMADEXx"; 
        $pass = "databasePassword"; 
        $host = "127.0.0.1"; 
        $dbname = "ForumExample"; 
        $dbc = mysqli_connect ( $host, $user, $pass, $dbname ) or die ( "Failed to connect to database." ); 
  
    } 
?> 

Don't worry, we are almost done with database.php!!

Now, we need to define a variable name "$executeString" in the function, that way we can use mysqli_query to execute what ever the code says.

<?php 
    function executeDatabase ( $executeString ) { 
         
        $user = "xXMADEXx"; 
        $pass = "databasePassword"; 
        $host = "127.0.0.1"; 
        $dbname = "ForumExample"; 
        $dbc = mysqli_connect ( $host, $user, $pass, $dbname ) or die ( "Failed to connect to database." ); 
  
    } 
?> 

Finally, we are one function away from being done.

Now, we need to return what ever mysqli_query returns. That way, if the user is requesting something like "SELECT * FROM tableName" it will return it.

<?php 
    function executeDatabase ( $executeString ) { 
         
        $user = "xXMADEXx"; 
        $pass = "databasePassword"; 
        $host = "127.0.0.1"; 
        $dbname = "ForumExample"; 
        $dbc = mysqli_connect ( $host, $user, $pass, $dbname ) or die ( "Failed to connect to database." ); 
        return mysqli_query ( $dbc, $executeString  ); 
  
    } 
?> 

Nice job! Now database.php is finally long at last, complete.

index.php

So, start off by creating a new PHP file called index.php

<?php 
  
?> 

So, first of all we will need to include database.php that way it gives us our executeDatabase function.

<?php 
    include "database.php"; 
?> 

Now, all you'll need to do is execute what you want. The rest is up to you.

Example:

<?php 
    include "database.php"; 
    $query = executeDatabase ( "SELECT * FROM TableName" ); 
    while ( $row = mysqli_fetch_array ( $query ) ) { 
        echo $row['ColumnName']; 
    } 
?> 

I hope that you have learned something!

(If you need further help, just message me on the Revolution of Scripting forum. )

Link to comment

It's not about what we like, it's about how you structure the tutorial and your non-properly working code. This section is for experienced scripters to share their knowledge, not for beginners who just finished reading a tutorial - if you don't know, don't make anything whatsoever. Limit yourself to learning, instead of filling this section with yet another bad "tutorial", don't make this worse than it already is.

If you want to make something, do it properly.

This is a proper tutorial: https://wiki.multitheftauto.com/wiki/Tu ... /PM_System

(Isn't this section also only for Lua? - It is on the Lua scripting help board, makes no sense to have unrelated tutorials on that section.)

Link to comment
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...