Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $servername = "localhost";
- $username = "user";
- $password = "pass";
- try {
- $conn = new PDO("mysql:host=$servername;dbname=dbname", $username, $password);
- // set the PDO error mode to exception
- $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- echo "Connected successfully";
- }
- catch(PDOException $e)
- {
- echo "Connection failed: " . $e->getMessage();
- }
- /* VARIABLES */
- $url = 'My url i use'; // Change this to the correct URL to access the data
- $display_output = 1; // Change this to 1 if you want to run this in the browser and see the output. Or if you want the cron job to email you the output so that you know it is working.
- $display_queries = 1; // Change this to 1 if you want to see the SQL equivalent statements in the browser, of what is being run.
- $live = 1; // Change this to 1 once you have set up the table in the database and you want the script to run properly and update your DB, rather than just showing you the text output
- // This is storing the output of that page into the variable "$data"
- $data = file_get_contents($url);
- // This line is getting rid of the first 2 characters in the data, ie: the "1 "
- $data = substr($data, 2);
- // This line is adding an extra "@" to the end of the data to fix a small problem
- $data = $data . '@';
- // This is converting all new line characters into line break tags
- $data = nl2br($data);
- // This is replacing all line break tags with "^"
- $data = preg_replace("/\<br \/\>/", "^", $data);
- $output = '';
- $output .= '<h1 style="text-align:center;">Cron Output</h1><br><hr><br>';
- echo '<p style="text-align:center;font-size:10pt;color:red;">If you are viewing this page in the browser, you will need to check the source code on the server to see what is happening and what you need to do.</p>';
- // Start over again with the new String of data, this time explode with "^" so we can get each defence slot
- $slots = explode("^", $data);
- $output .= 'defence Slots: ' . count($slots) . '<br><br>';
- // Explode to get number of elements as well
- $el = explode("@", $data);
- $output .= '<br><br>';
- if($live == 1)
- {
- // Truncate the table to remove all current rows
- mysql_query("TRUNCATE TABLE `_defence`") or die('Error: ' . mysql_error());
- }
- $query = '';
- // Loop through each attck slot
- for($i = 0; $i < count($slots); $i++)
- {
- $slots[$i] = trim($slots[$i]);
- if($slots[$i] != '@')
- { # Not Just "@"
- $liveQuery = '';
- // Update query String if output is desired
- $query .= "<br><br>INSERT INTO `_defence` (`playersname`, `playerslevel`, `playersexp`, `lastupdated`) VALUES (";
- // Live Query String
- $liveQuery .= "INSERT INTO `_defence` (`playersname`, `playerslevel`, `playersexp`, `lastupdated`) VALUES (";
- $output .= '<u>Slot '.$i.'</u><br>';
- // Explode this defence slot with "@" to get each element
- $elements = explode("@", $slots[$i]);
- // Loop through them
- for($j = 0; $j < count($elements); $j++)
- {
- $query .= "'".$elements[$j]."'";
- $liveQuery .= "'".$elements[$j]."'";
- if($j < (count($elements) - 1))
- {
- $query .= ',';
- $liveQuery .= ',';
- }
- $output .= '['.$j.']: '.$elements[$j].'<br>';
- }
- $output .= '<br><br>';
- $now = date('d.m.Y - H:i:s');
- $query .= ", '".$now."');";
- $liveQuery .= ", '".$now."')";
- // If script is live, insert into the DB
- if($live == 1)
- {
- mysql_query($liveQuery) or die('Error (Cannot Insert defenceid ['.$i.']) Query =><br><br>'.$liveQuery.' : ' . '<br><br>' . mysql_error());
- }
- } # Not Just "@"
- }
- if($display_output == 1)
- {
- echo '<br><hr><br>';
- echo $output;
- }
- if($display_queries == 1)
- {
- echo '<br><hr><br>';
- echo $query;
- }
- if($live == 1)
- {
- echo '<p style="text-align:center;font-size:10pt;color:blue;">Database Updated Successfully</p>';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement