PixelBanter
Acquaintance
This tutorial was written by my co-adiminstrator.
A big part of learning how to program and making an efficient application whether it be web sided (like this) or otherwise is using databases. They make a programmers life easier, saving accounts, saving news, posts, threads, all content that is generated dynamically making for good user interaction, without databases (you could use other saving methods, flat file etc) making something like a forum and having it function correctly would be rather difficult. I'm going to show how to connect to a database and run a query in one of the latest, up to date methods added to PHP for database manipulation. I say latest, it's not new. Since mysql_ functions have been deprecated PDO and MySQLi have been become increasingly more sought after and it's gotten to the point now that if someone posts work using the mysql_ functions it's slated for that reason, which is completely fair, deprecation should not be present in a script. This is going to be a basic tutorial more than anything else, it shows the essentials and anything else put into it is more or less done by you. If you have queries I will be available but I won't be doing the work for you.
Without further ado, take a look here.
The above is a configuration file. This has all of the variables required for a database connection to be initiated and a database selected for manipulation and retrieval. The server you're connecting to is almost always localhost, if it isn't you would know when you got the hosting. Your username is simple, whatever the account assigned to the database is. Password is also simple, whatever the password to the account assigned to the database is and the database is just the database you're looking to connect to. Now that's understood we can move on to the connection initiation.
This should look familiar enough to a lot of the people who have used mysql_ functions in the past. All you're doing is including the configuration file and then assigning the MySQL connection to the $con variable.
To run a query, you will notice a difference here. This is probably the biggest difference in using MySQLi or PDO, prepared statements. Prepared statements are a replacement for escaping strings using mysql_real_escape_string();. This works out a lot better.
Told you it'd be a bit different, this isn't a bad thing though. Learning is good, especially while improving. What you're doing is taking your regular SQL query and preparing it as a whole this time, the ? placeholders are then corresponding in place while binding the parameters, the use of the three "s" is to bind the parameters of a string, there are other options, double, integer etcetera. One of the things you'll need to look into yourself. The end is executing the prepared statement assigned to $stmt.
As you can see, this is pretty much done. It is a rather simple tutorial and I did state that at the beginning. This is essential knowledge to anyone wishing to progress in PHP and knowing this alone can make some pretty impressive applications, if you put your mind to it.
As I said at the start, let me know of any queries.
Credits: Introduction To MySQLi
A big part of learning how to program and making an efficient application whether it be web sided (like this) or otherwise is using databases. They make a programmers life easier, saving accounts, saving news, posts, threads, all content that is generated dynamically making for good user interaction, without databases (you could use other saving methods, flat file etc) making something like a forum and having it function correctly would be rather difficult. I'm going to show how to connect to a database and run a query in one of the latest, up to date methods added to PHP for database manipulation. I say latest, it's not new. Since mysql_ functions have been deprecated PDO and MySQLi have been become increasingly more sought after and it's gotten to the point now that if someone posts work using the mysql_ functions it's slated for that reason, which is completely fair, deprecation should not be present in a script. This is going to be a basic tutorial more than anything else, it shows the essentials and anything else put into it is more or less done by you. If you have queries I will be available but I won't be doing the work for you.
Without further ado, take a look here.
Code:
<?php
/**
* Connection variables
*/
$server = "localhost";
$user = "root";
$pass = "123";
$db = "database_name";
The above is a configuration file. This has all of the variables required for a database connection to be initiated and a database selected for manipulation and retrieval. The server you're connecting to is almost always localhost, if it isn't you would know when you got the hosting. Your username is simple, whatever the account assigned to the database is. Password is also simple, whatever the password to the account assigned to the database is and the database is just the database you're looking to connect to. Now that's understood we can move on to the connection initiation.
Code:
<?php include('./config.php');
$con = new MySQLi($server, $user, $pass, $db);
This should look familiar enough to a lot of the people who have used mysql_ functions in the past. All you're doing is including the configuration file and then assigning the MySQL connection to the $con variable.
To run a query, you will notice a difference here. This is probably the biggest difference in using MySQLi or PDO, prepared statements. Prepared statements are a replacement for escaping strings using mysql_real_escape_string();. This works out a lot better.
Code:
if ($stmt = $con->prepare("INSERT INTO `users` VALUES(?, ?, ?)") ) {
$stmt->bind_param("sss", $username, $email, $message);
$stmt->execute();
} else {
echo 'error preparing statement';
}
Told you it'd be a bit different, this isn't a bad thing though. Learning is good, especially while improving. What you're doing is taking your regular SQL query and preparing it as a whole this time, the ? placeholders are then corresponding in place while binding the parameters, the use of the three "s" is to bind the parameters of a string, there are other options, double, integer etcetera. One of the things you'll need to look into yourself. The end is executing the prepared statement assigned to $stmt.
As you can see, this is pretty much done. It is a rather simple tutorial and I did state that at the beginning. This is essential knowledge to anyone wishing to progress in PHP and knowing this alone can make some pretty impressive applications, if you put your mind to it.
As I said at the start, let me know of any queries.
Credits: Introduction To MySQLi







