19.09.2011, 16:51
(
Последний раз редактировалось Kingunit; 20.09.2011 в 21:20.
)
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.
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
?>
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 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(10) AUTO_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(50) NOT NULL, // New column, VARCHAR (50) means - The column can hold up to 100 characters. NOT NULL seems clear.
Lastname VARCHAR(50) NOT NULL, // New column, VARCHAR (50) means - The column can hold up to 100 characters. NOT NULL seems clear.
) TYPE=MyISAM;
:: 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");
?>
:: 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.
PHP код:
<?php
?>
PHP код:
<?php
include ("config.php"); // This will include our 'config' file we've created some minutes ago!
?>
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.
?>
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");
?>
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.
?>
Quote:
There are currently 55 users registered in our server database! |
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