[Tutorial] PHP + MySQL
#1

PHP + MySQL


:: Introduction
Since everyone was asking about a tutorial about PHP + MySQL. I've decide to create one! Let's get it straight, this tutorial isn't created for people without any knowledge about PHP. Probably I will create a 'newby' PHP tutorial, but I will start with this since alot of people are willing to learn this. Maybe some of you guys are thinking: ,, What the hell, this is SA:MP we don't need PHP". Then you are completely wrong, you can use PHP + MySQL alot for your server. For example, you are willing to create a UCP (User Control Panel). That's always nice for you server! Let's start.

:: What I need?
  • Webhosting with PHP and MySQL.
  • Little knowledge about PHP and MySQL.
:: Config File
First of all, everyone who is working with PHP knows that he need to open PHP with a tag, and he need to close it:
PHP код:
<?php
// Script
?>
Let's start with our database connection. Like PAWNO you need to connect with the MySQL database before getting information. A basic 'config' file is looking like this:
PHP код:
<?php
$dbhost 
""// The MySQL host. Most of the time localhost, not at all. You can find it in your control panel.
$dbuser ""// Your custom MySQL database username. (Most of the time I use the same name as the database name)
$dbpass ""// Seems clear, MySQL database password.
$dbase ""// Your cutom MySQL database. (Most of the time I use the same name as the username)
mysql_connect($dbhost$dbuser$dbpass) or die ("WARNING: There is a fail at : ".$_SERVER["PHP_SELF"]."<br />Fail at : ".mysql_error()); // This will connect to the MySQL database. If there are any errors, it will return the fail.
mysql_select_db($dbase) or die("WARNING: There is a fail at : ".$_SERVER["PHP_SELF"]."<br />Fail at : ".mysql_error()); // This will select the database, like above here. If it failed it will return the MySQL error.
?>
Create a new folder like: ,, My website " and save this file with the name: config.php Let's continue!

:: Create table
Huh, table? I don't need a home table. Haha! It's something else then a home table. Honestly, before you are starting with this tutorial, you need to know how to make a table. So I will keep this short. Hopefully you know how to paste this into your PHPMYADMIN or whatever you are using.

PHP код:
CREATE TABLE Users // This will create a new table with the name ,, Users "
  
ID INT(10AUTO_INCREMENT// Creating a new column, INT(10) means it's a integer The Auto .. means the ID is going like: 1,2,3,4,5  etc.
  
Firstname VARCHAR(50NOT NULL// New column, VARCHAR (50) means - The column can hold up to 100 characters. NOT NULL seems clear.
  
Lastname VARCHAR(50NOT NULL// New column, VARCHAR (50) means - The column can hold up to 100 characters. NOT NULL seems clear.
TYPE=MyISAM
This was just a short example. I will use this example in this tutorial. Paste this into PHPMYADMIN and we can continue the tutorial.

:: PHP Echo
,, Let's get it straight, this tutorial isn't created for people without any knowledge about PHP. "
I've wrote that, but I see alot of people wants me to explain the PHP echo. Alright ... Let's start!
The PHP Echo is very simple. The thing 'echo' is doing is printing things. For example:
PHP код:
<?php
echo ("Hello world");
?>
This will print: Hello world. It's kinda similar to the SendClientMessage from PAWNO!

:: Getting information using SELECT
Now we are going to create a new page. What I'm going to explain in this tutorial is:
  • How we can get a count of registered users.
  • Getting information out of the MySQL database.
Let's start with creating a new .php file. Call it ,, Information.php ". Open it and start it like a real PHP page.
PHP код:
<?php
?>
Now we need to connect with our MySQL database. There is a 'include' function from PHP. It's helping alot! We don't need to type the connection again and again. Do you remember we created the config file? Now we are going to use it! Include it like this:
PHP код:
<?php
include ("config.php"); // This will include our 'config' file we've created some minutes ago!
?>
Now we are going to create our first MySQL query.
PHP код:
<?php
include ("config.php"); // This will include our 'config' file we've created some minutes ago!
$result mysql_query("SELECT * FROM users") or die(mysql_error());  // Getting all information (*) from the table users. 
echo "<table border='1'>"// Creating a new table. Probably everyone who is reading this tutorial will know how to echo etc.
echo "<tr> <th>Firstname</th> <th>Lastname</th> </tr>";
while(
$row mysql_fetch_array$result )) // This will loop through the table 'users' until there are no more things to get.
{
    echo 
"<tr><td>"// HTML Part of the table.
    
echo $row['Firstname']; // This will print out the 'Firstname'
    
echo "</td><td>"// HTML Part of the table.
    
echo $row['Lastname']; // This will print out the 'Lastname'
    
echo "</td></tr>"// HTML Part of the table.

echo 
"</table>"// HTML part of the table.
// At all this script is looping through the whole table and getting the information (Firstname + Lastname) and it will print in the a smooth table.
?>
:: Counting your users with COUNT
Now we've displayed all the users. But now we want a 'user count' it will count all the users in the table. Let's start! Create a new file called ,, count.php ". We need to connect again with the database. Let's go!
PHP код:
<?php
include ("config.php");
?>
Now we need to create a new query, using COUNT()! Kinda easy, I will give a example:
PHP код:
<?php 
$query 
"SELECT COUNT(ID) FROM users "// Seems clear, we are getting the amount of ID's and we are getting it FROM the table users.
$result mysql_query($query) or die(mysql_error()); // This will make the query and if it's invalid it will returns the MySQL error.
while($row mysql_fetch_array($result)) // This will loop through the table.
{
echo 
"There are currently <b> "$row['COUNT(ID)'] ."</b> users registered in our server database!"// This will display the amount of registered users.
echo "<br />"// HTML 
}
// This script will display the amount of players in the table.
?>
Output:
Quote:

There are currently 55 users registered in our server database!

:: End
Thank you for reading my third tutorial. I hope I've helped alot of people out. Probably I'm going to create some more PHP tutorials for the SA:MP forum.

~ Kingunit
Reply
#2

Nice, I see that you got far away with MySQL; congratulations!
Reply
#3

Nice.Good job.
Reply
#4

Good job Kingunit.
Reply
#5

Thank you very much guys.
Reply
#6

I'll take a look at this, wanted to learn PHP for a while now anyways .

There's one small thing though:
pawn Код:
$query = "SELECT ID, COUNT(ID) FROM users ";
Wouldn't 'SELECT COUNT(ID) FROM users' do the trick as well? AFAIK, that selects both all IDs and the number of IDs.
Reply
#7

Quote:
Originally Posted by Hiddos
Посмотреть сообщение
I'll take a look at this, wanted to learn PHP for a while now anyways .

There's one small thing though:
pawn Код:
$query = "SELECT ID, COUNT(ID) FROM users ";
Wouldn't 'SELECT COUNT(ID) FROM users' do the trick as well? AFAIK, that selects both all IDs and the number of IDs.
Hmm, not sure about it. My method was working so I'm not sure if your one is working. To get the answer you can simply test it. Good to hear that you are learning.
Reply
#8

I'm working on an anticheat, when I finish it I'll try this! Thanks
Reply
#9

PHP код:

$result 
mysql_query("SELECT * FROM users") or die(mysql_error());  // Getting all information (*) from the table users. 
Why would you use this? You're getting all the information for every single player and then making a massive table. More than 100 users would completely lag it.

PHP код:

$query 
"SELECT ID, COUNT(ID) FROM users "// Seems clear, we are getting the amount of ID's and we are getting it FROM the table users. 
Hiddos was right in a previous post. You're selecting the ID and not using it at all.

What you should do is the following
  • Discuss what the CREATE TABLE query does.
  • Discuss what echo actually does
  • Discuss what the row variable actually does and how to access other information using it.
It's not a bad tutorial, but you should put a bit better explanation into it.
Reply
#10

Quote:
Originally Posted by [HiC]TheKiller
Посмотреть сообщение
PHP код:

$result 
mysql_query("SELECT * FROM users") or die(mysql_error());  // Getting all information (*) from the table users. 
Why would you use this? You're getting all the information for every single player and then making a massive table. More than 100 users would completely lag it.

PHP код:

$query 
"SELECT ID, COUNT(ID) FROM users "// Seems clear, we are getting the amount of ID's and we are getting it FROM the table users. 
Hiddos was right in a previous post. You're selecting the ID and not using it at all.

What you should do is the following
  • Discuss what the CREATE TABLE query does.
  • Discuss what echo actually does
  • Discuss what the row variable actually does and how to access other information using it.
It's not a bad tutorial, but you should put a bit better explanation into it.
Quote:

Let's get it straight, this tutorial isn't created for people without any knowledge about PHP.

I'm not going to explain what a echo does. Anyway, thank you. I will look into it.
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)