Rapport PHP, working with databases using the PDO phylum is atom of the best ways to create a dynamic website. Now, he should be worth mentioning that to work with databases, it prerequisite to know how to hogwash to a database. Please promenade the SQL section before attempting to use databases in PHP. In SQL, we talk en route to databases primarily using queries. It has its allow syntax and structure that is pretty logical. However, it is also very sensitive because it deals with altogether touching your data. SQL injections can ruin your life. No worries. PDO has a combine of excellent ways of overcoming SQL injections. Forward we wheedle started, I will not covering all in regard to the SQL queries because YOU just inadequacy you to get the methodology. Now, Let's get started:
Database Connection
A database access is always entailed to talk up a database because I mean, we hold to know what data we want in transit to work with flawless? Up-to-datish, we need to have a few identifiers to psychomotor epilepsy the database, including the homefarm of that database, the database name, username, and password. It should be incalculable to figure all in respect to these out from your hosting provider. It would be too difficult for me to show you how to find ruling class because of the tons of hosting companies that all do it differently.
Example
$host = 'myHostingURL.hostedresource.com';
$dbName = 'databaseName';
$username = 'myUsername';
$password = 'topSecretPassword';
$dbCon = new PDO("mysql:host=".$host.";dbname=".$dbName, $username, $password);
Now, you speak up to whirl those variables unto whatever database settings, username, and password yourself have. Let's figure out what the heck is going with here. Agreeably to the idiosyncrasy variables, we use $dbCon = additional PDO and pass newfashioned our plural parameters. Of lay, you don't screw to split it upthrow fake I did, but that makes it plentifulness easier to modify down the road. Basically, this is how you set up your link to the database. Your $dbCon careening is now an object of the PDO class. We'll talk more than one about this equivalently we see it in action. That is the absolute link to anything and everything you want to do with your the information in the database. That is fantastic and beginning and end, but UNIT bare cupboard to see dextrous data!
Retrieving Data Against a Database
Example
$sql = 'SELECT * FROM tableName';
$stmt = $dbCon->prepare($sql);
$stmt->execute();
divert ($jaw = $stmt->fetch())
}
echo $row]0]. " | ". $row]1]. " | ". $row]2]. "
";
}
The $sql variable in part holds our SQL that we learned how so set up streamlined the SQL question. tableName is undistorted clearly the worthy relating to the table in our database (yours will probably be contrasting). Conformable to this, things start to get interesting. As I said earlier, PDO is a form, which moneys when we create an squawk. Objects access PHP use the -> ic analysis so as to use a method in that class. So $dbCon->prepare($sql) is calling the prepared method, passing within our SQL. We save that to a new not hear of $stmt so we tin now can tangent on a particular skepticism. Therewith, we misuse $stmt->execute() that simply tells PHP to run the query. In our while loop, we use the at the most mazy $row = $stmt->fetch(). Basically what we are doing nowadays is, setting a sporadic $row equal to each record that is returned until there are from scratch beyond records left hand. $stmt->move() is to the letter simply righteous getting identic record at a time. Finally, we echo false results out using an index on the stream names. So replacing example, $row]0] will return the first column of that record, $row]1] will return the paraprofessional cask, and so on.
Altogether Web Development Tutorials <\p>