Jump to content

PHP PDO Tutorial v2


Wife

Recommended Posts

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
Link to comment
Share on other sites

  • 1 month later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

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