Jump to content

PHP PDO Tutorial v2


Recommended Posts

Posted

Hello,

Okay, so in this tutorial we will be creating a dynamic PHP Script, that changes database values when the correct user agent is requesting your server, and creating another PHP file for fetching that data from the database. No fancy design here, if you are after that - stop reading.

 

Part 1. What is User Agent?

User agent is a software application 'agent' that performs tasks behalf of a user. We can use user agent for our PHP script only allowing requests from a certain user agent. Useful in cases for example dynamic signatures and other tasks that we want to apply automation on.

 

Example of User Agent usage

$ua_name = "FacialSlave"; //The name of expected user agent
$ua = $_SERVER['HTTP_USER_AGENT'];

if(!preg_match('/'.$ua_name.'/', ua)) { //Returns false if incorrect HTTP_REQUEST
echo '<h1>Invalid user agent.</h1>';
} else if(isset($_GET['variable'])) { //If above returns true let's execute our statement defined somewhere.
$statement->execute();
}

Note! You need also to define the user agent from the other end of your application, in this we would be creating a dynamic script leaderboard, so you need to define the user agent from your Java files also, like this

private static final String USER_AGENT = "FacialSlave"; 

Part 1.5 The Java side for this app

The Java side not exactly what this guide is about, I'm just going to briefly talk this through.

//Variables
private static final String URL = "http://localhost/pdo_updateDatabase.php?";
private static final String USER_AGENT = "FacialSlave";

//Actual code
public static void httpAgent(String var_str, int var_int) {
String urlStr = URL + "variableOne=" + var_str + "&var_int=" + var_int;
StringBulder http = new StringBuilder();
URL url;
try {
url = new URL(urlStr);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.addRequestProperty("User-Agent", USER_AGENT);

BufferedReader brd = new BufferedReader(new InputStreamReader(connection.getInputStream()));

String line;
while((line = brd.readLine()) != null) {
result.append(line);
}
brd.close();
} catch (Exception err) {

}
}

Part 2. SET Query for updating data in database

SET changes already existing data in database to new given values

require 'pdo_dbConnection.php';
//UA CHECK
...


Now if the user agent is correct, let's start executing our qery
else if(isset($_GET['variableOne'] && $_GET['var_int'])) {
//Query, where we set value of variableOne from Table where integerValue equals to given integer
$query = 'UPDATE Table SET variableOne = :variableOne WHERE integerValue = :var_int';
//Preparing the query above
$statement = $connection->prepare("$query");
//Executing statement as array, where we define placeholders for the query
$statement->execute(array(
':variableOne' => $_GET['variableOne],
':var_int' => $_GET['var_int']
));
echo 'Updated successfully.';
}

Part 2.5 Fetching data from our database

Fetching data can be done in multiple ways, this is just one example of it.

require 'pdo_dbConnection.php';

$query = 'SELECT * FROM Table'; //Query, * is a wildcard for selecting ALL fields from table.
//Using * is not recommended in production environment.
$statement = $connection->prepare($query); //And again, preparing the query for connection.
$statement->execute(); //Executing statement.

//Fetching data
foreach($statement as $row) {
echo '
<table style="width=250px">
<tr>
<th>My Script</th>
</tr>
<tr>
<td>Variable One: '.$row['variableOne'].'</td>
<td>Variable Two: '.$row['variableTwo'].'</td>
</tr>
</table>
';
}


I have NOT tested the code above, just wrote it. If you face any errors with my code, please let me know and I will take a look at it. Because I do make often alot of easy typos etc. 

  • Like 2
  • 1 month later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...