• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Simple MySQL connection + use of array

Archez

'
Senator
Joined
Jun 26, 2008
Messages
6,589
Solutions
1
Reaction score
70
Location
Mexico
You are now going to learn how to use an array in a simple MySQL connection, using the scripting language: PHP (short for: PHP: Hypertext Preprocessor) if you don't know how to do it already.

First of all, I would like to encourage all beginners to look at this tutorials first:
http://otland.net/f481/paxtons-web-development-school-part-1-a-28932/
http://otland.net/f481/paxtons-web-development-school-part-2-a-29066/

Let's start...

1. - let's open notepad and open our php tags.
PHP:
<?php
// PHP tags
?>

2. - we want to use arrays to connect to our mysql server.
PHP:
<?php

$mysql = array('host' => 'localhost', 'user' => 'root', 'pass' => 'dbpasswordhere', 'db' => 'tfs');

?>

# as long as you keep everything inside the parenthesis and respect the PHP syntax, the web server won't drop any error, so you can use different ways to put the array, like:

PHP:
<?php

$mysql = array(

'host' => 'localhost', 
'user' => 'root', 
'pass' => 'dbpasswordhere', 
'db' => 'tfs'

);

?>

# this might look cleaner to some people and dirtier to others...

3. - how are we going to use the values in $mysql? well, here is the solution:

PHP:
<?php

$mysql = array('host' => 'localhost', 'user' => 'root', 'pass' => 'dbpasswordhere', 'db' => 'tfs');

echo 'The value of host in the mysql variable is: ' . $mysql['host'];

?>

# output would be: The value of host in the mysql variable is: localhost

4. - time to connect to the mysql server using (mysql_connect & mysql_select_db)

PHP:
<?php

$mysql = array('host' => 'localhost', 'user' => 'root', 'pass' => 'dbpasswordhere', 'db' => 'tfs');

$connect = mysql_connect($mysql['host'], $mysql['user'], $mysql['pass']);

if (!$connect) {
die('Unable to reach MySQL server.<br />Error: ' . mysql_error());
}

?>

# we are now attempting to connect to the MySQL database on localhost as root. If the connection fails, web server will drop the following error: Unable to reach MySQL server. Error: [mysql error].

5. - once we have managed to connect to our database, we will select the database we want to connect with by adding the following:

PHP:
<?php

$mysql = array('host' => 'localhost', 'user' => 'root', 'pass' => 'dbpasswordhere', 'db' => 'tfs');

$connect = mysql_connect($mysql['host'], $mysql['user'], $mysql['pass']);

if (!$connect) {
die('Unable to reach MySQL server.<br />Error: ' . mysql_error());
}

$database = mysql_select_db($mysql['db'], $connect);

if (!$database) {
die('Unable to reach MySQL database.');
}

?>

# pretty obvious, if the attempt to select database fails, it will drop the following error: Unable to reach MySQL database.

6. - now that we have established a stable connection with the MySQL server, we are now able to run queries and be happy.

PHP:
<?php

$mysql = array('host' => 'localhost', 'user' => 'root', 'pass' => 'dbpasswordhere', 'db' => 'tfs');

$connect = mysql_connect($mysql['host'], $mysql['user'], $mysql['pass']);

if (!$connect) {
die('Unable to reach MySQL server.<br />Error: ' . mysql_error());
}

$database = mysql_select_db($mysql['db'], $connect);

if (!$database) {
die('Unable to reach MySQL database.');
}

$query = mysql_query('SELECT `rlname` FROM `accounts` WHERE `name` = "archez"');

if (mysql_num_rows($query) == 0) {
die('Account with name "archez" does not exist!');
} else {
$row = mysql_fetch_assoc($query);

echo 'The registered real name of account with name "archez" is: ' . $row['rlname'];
}

?>

# that will output the following: The registered real name of account with name "archez" is: [rlname]

Now that you know this, you are now capable of doing your own standalone panel to manage your OTS via website.

If you feel like doing some extra work, here is a simple task you can try to accomplish.

1. make a script that shows all the players in database with the use of the while() function and the mysql_fetch_array() function.

^^
 
LaO89qEDN2.png

this is fcking epic!!!!
 
Back
Top