I am running a couple of live and vod streams being segmented with ffmpeg. I run one channel per cloud server. I have a central, more robust server which i use for various tasks one of those task being ingesting the main m3u8 playlist and storing the segment names and creation times in a database.
The segments are created about every 8 seconds currently for 8 channels.
This means that every 8 seconds i call the playlist.m3u8 (which is on a remote server) and store the information for each segment in the database.
This allows me to quickly create the vod (Select from channel 1 between time and time).
However, once or twice the database seems to have stopped working and I need to repair it before it can take new information.
I am wondering, am I hitting it too hard? Is there a better way?
My statement opens a new connection, checks if the entry already exists, if not it will enter it, then it closes the connection.
I do this once per channel per 8 seconds.
This feels like it could be better but I don't quite know how. Should I / can I keep the connection open maybe? Maybe its not a problem at all.
Anyone have any advice?
If you need more information just ask.
Query:
$conn = new mysqli($servername, $username, $password, $dbname);
$eight = date('Y-m-d', strtotime("-8 day"));
$vq = "SELECT * FROM `".$channels['channels'][$id]['fo_id']."`
WHERE `segment_name` = '".$oline."'";
$query = mysqli_query($conn, $vq);
if(mysqli_num_rows($query) == 0){
$sql = "INSERT INTO `".$channels['channels'][$id]['fo_id']."`
(`segment_name`, `server`, `created`, `lenght`)
VALUES
('".$oline."', '".$channels['channels'][$id]['server']."',
'".date("Y-m-d H:i:s", $date)."', '".$otime."')";
$conn->query($sql);
}
$query = mysqli_query($conn, "DELETE FROM `".$channels['channels'][$id]['fo_id']."`
WHERE `created` < '".$eight." 00:00:00'");
$conn->close();
Related
I have a table in a hosting I want to communicate with it remotely through my localhost, but I can't reference that I want the table from that database how do I do that?
*im using php *
<?php
// Create connection
$conn = mysqli_connect('localhost', 'root', '','contador');
$conn2 = mysqli_connect('hostingIP', 'userIp', 'userPass');
$tableName='counter';
$dbname2='dbHosting';
$result = mysqli_query($conn2,$dbname2);
$sql = "INSERT INTO tab2 (Data,price) VALUES ( now(),(SELECT COUNT(*) FROM $result.$tableName WHERE DATE(Data)= CURRENT_DATE() - INTERVAL 0 DAY))";
?>
Make an export script in localhost.
Trigger it regularly via cron jobs.
Include FTP credentials to upload it to your server.
Make an import script in your server.
Trigger it regularly via cron jobs.
Thank me later :)
This is a problem I'm having for quite some time now.
I work for a banking institute: our service provider lets us access our data via ODBC through a proprietary DB engine.
Since I need almost a hundred tables for our internal procedures and whatnot, I set up some "replication" scripts, put em in a cron and basically reloading from scratch the tables I need every morning.
When the number of records is small (approx. 50.000 records and 100 columns or so) everything goes smooth, but whenever I get a medium-big table (approx. 700.000 records), more often than not the script restarts itself (I look at my MySQL tables while the import scripts are running and I see them going 400k, 500k... and back from 1).
This is an example of one of my import scripts:
<?php
ini_set('max_execution_time', '0');
$connect = odbc_connect('XXXXX', '', '') or die ('0');
$empty_query = "TRUNCATE TABLE SADAS.".$NOME_SCRIPT;
$SQL->query($empty_query);
$select_query = " SELECT...
FROM ...";
$result = odbc_exec($connect, $select_query);
while($dati = odbc_fetch_object($result)) {
$insert_query = " INSERT INTO ...
VALUES ...";
$SQL->Query($insert_query);
}
// Close ODBC
odbc_close($connect);
?>
Any ideas?
I'm building a Joomla site, but I'm stuck at something, wondering if you guys could kindly help me out.
I'm trying to get an alerting system working.
I essentially need to create a cron job that runs a PHP script every 10 minutes. This PHP script should basically do a select query on the database, and if a particular field has a specific value, then open a website.
So, this is what I have now:
$result = mysql_connect(localhost, myusername, mypassword);
mysql_select_db("database_name");
$result = mysql_query( "SELECT from_table");
The table name is: comments.
The field name is: published.
So what I would ideally need, is that if the value of published = 0, then the php script should open this URL, which will send me an sms message https://www.voipbuster.com/myaccount/sendsms.php?username=xx&password=xx&from=xx&to=xx&text=Alert
And if there is any other value, I don't want it to do anything.
So, anytime there is value 0 I receive a text message.
Hope this makes sense, thanks for your immense patience reading.
Get Andrew Eddie's script for cron plugins and run that. Then write a plugin in the cron group that sends your email.
https://github.com/eddieajau/jc-kodaly
Switch to PDO (or MySQLi). The API you're using is deprecated.
(based on nothing - this is a guess)
$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
$sql = 'SELECT published FROM sometable';
$stmt = $dbh->prepare($sql);
$stmt->execute();
$stmt->setAttribute(PDO::FETCH_ASSOC);
$row = $stmt->fetch();
if( $row['published'] == 0 )
{
$ch = curl_init(" https://www.voipbuster.com/myaccount/sendsms.php?username=xx&password=xx&from=xx&to=xx&text=Alert");
curl_exec($ch);
curl_close($ch);
}
How can I change a value in mysql database after 24 hours?
Does a user need to visit the website for the query to run?
For example I have a column named "user-rank" and it's value is "10" ,I want it to be "20" after 24 hours ...
Crontab aka CRON is the most flexible solution but you need to have privileges using it in shell or specific control panel as many hosting providers do not like to share privileges to it due security reason.
First create PHP script whit update code, something like this:
<?php
//connect to database
//....
//prepare MySQL query
$sql = "UPDATE myTable SET `user-rank` = `user-rank` + 10";
//execute query and handle errors
//...
//optinal reporting on e-mail
//...
//end
?>
Next you need to add entry to crontab by calling "crontab -e" in shell, paste your code like this and save changes:
0 0 * * * /usr/bin/php -f /path/to/my_update_script.php > /dev/null
This entry will define call to script every day at midnight (server time) and ignore any output from script.
NOTE: You need to specify exact path for php binary which can be different for various OS or user defined profile. Check it with "which php" shell command.
--------------- UPDATE ---------------
Solution without crontab.
Create one new database table php_cron and insert initial record (firs and only one required) with following MySQL code:
CREATE TABLE `php_cron` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`last_ts` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
);
INSERT INTO `php_cron` (`id`, `last_ts`) VALUES (1,'2012-08-10 00:00:00');
Then create new PHP script which will hold all required logic for such task and include it in your master index.php file or other places where it can be loaded every time when user visit your site, for example in some config or similar script. I suppose that you will have at least one visitors per day.
This new script need to do two task, first to query those php_cron table and calculate time difference and if difference is larger than 24h (86400 seconds) then execute code for updating user-rank and save new timestamp into database.
That script may look like this:
<?php
//connect to database
//....
//get time difference in seconds from last execution
$sql1 = "SELECT TIME_TO_SEC(TIMEDIFF(NOW(), last_ts)) AS tdif FROM php_cron WHERE id=1";
$res1 = mysql_query($sql1) or die("[1] MySQL ERROR: ".mysql_error());
$dif = mysql_fetch_assoc($res1['tdif']);
if ($dif >= 86400) { //24h
//following code will run once every 24h
//update user's page rank
$sql2 = "UPDATE myTable SET `user-rank` = `user-rank` + 10";
mysql_query($sql2) or die("[2] MySQL ERROR: ".mysql_error());
//update last execution time
$sql3 = "UPDATE php_cron SET last_ts = NOW() WHERE id=1";
mysql_query($sql3) or die("[3] MySQL ERROR: ".mysql_error());
}
?>
This script is not time accurate as CRON but for sure will be run once per day. That can be corrected with some more php/mysql logic but I thing it will do the job.
A common solution is to set up a cron job to periodically run a script to update whatever needs to be updated.
You could either set up something that runs a query checking the database for a last run time and then you could run the query (All of this when someone visits the site). Or you could set up a cron job.
Depending on your version of MySQL, you can use events.
First edit your crontab file (In a terminal)
crontab -e
Add this line to the file :
0 0 * * * php /var/www/myquery.php
The first 0 means the minute 0, and the second zero means at 0h. The other stars means the job will be executed every day.
In the file myquery.php you put some php that will connect to your database and that will execute the query you want.
This is a way to do it on linux systems.
I'm trying to increment +1 impression every time an ad is displayed on my site, however the variable increments +2 to +3 arbitrarily. I've removed everything that's working correctly and I made a page with only this code in it:
<?php
require "connect_to_mydb.php";
echo 'Hello***** '.$testVariable=$testVariable+1;
mysql_query("UPDATE `imageAds` SET `test`=`test`+1 WHERE `id`='1'");
?>
Every time the page is refreshed the, test increments arbitrarily either +2 or +3 and my page displays Hello***** 1 (Just to show its not looping). Access is restricted to this page so it's not other users refreshing the page.
Also, id and test are int(11) in the DB.
My DB required connection has nothing in it that would interfere.
Edit
Here is an updated code:
<?php
require "connect_to_mydb.php";
mysql_query("UPDATE `imageAds` SET `test`=`test`+1 WHERE `id`='1'");
$sql = mysql_query("SELECT * FROM imageAds WHERE id='1' LIMIT 1");
$check = mysql_num_rows($sql);
if($check > 0){
$row = mysql_fetch_array($sql);
echo $row['test'];
}
?>
Increments by +2 everytime
Edit
This is whats in connect_to_mydb.php
<?php
$db_host = "*************************";
$db_username = "*********";
$db_pass = "**********";
$db_name = "**************";
mysql_connect("$db_host","$db_username","$db_pass") or die ("could not connect to mysql");
mysql_select_db("$db_name") or die ("no database");
?>
Either there's a bug in MySQL's implementation of UPDATE, or you're doing something wrong in some code you haven't posted.
Hint: It's very unlikely to be a bug in MySQL. Other people would have noticed it.
From what you've shown, it looks like your page is being loaded multiple times.
This attempt to prove that the code is only being called once doesn't prove anything:
echo 'Hello***** '.$testVariable=$testVariable+1;
This will always print the same thing (Hello***** 1) even if you open this page multiple times because the value of $testVariable is not preserved across seperate requests.
This +2/+3 error is occurring only with Chrome and my Mobile Android browser and the code is solid. I looked to see if there is any issue with Chrome sending more than one http request (thx user1058351) and there is which is documented here:
http://code.google.com/p/chromium/issues/detail?id=39402
So since this way was unreliable I just completed a work around that is solid. Instead of including a PHP file that updates the amount of ad impressions on reload, I now have it so when the page loads, an AJAX request is sent to a separate PHP file which updates the ad stats and returns the appropriate data. The key I think is to send it through the JS code so only one http request can be sent to increment the data.
Thank you to all who responded especially user1058351 and Mark Byers (not a bug in MYSQL but possibly appears to be a bug in Chrome).