I have to insert around 75000 numbers of data which is to be retrieved from another table calculation. I have tried the following code.
$start = new DateTime('2018-09-01');
$end = new DateTime('2018-12-31');
$interval = DateInterval::createFromDateString('1 month');
$period = new DatePeriod($start, $interval, $end);
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
foreach ($period as $dt) {
$inputyear = $dt->format("Y");
$inputmonth = $dt->format("m");
Sql = " insert into tbl1()select from ... "
//HERE I JOIN 3 tables
$result = $conn->query($sql);
}
$conn->close();
Its giving me timeout error. I have tried increasing the wamp timeout to 300 as well but it didnot work. How can I optimize above code?
The worst solution
set_time_limit(0);
Best practices: try to delegate this task to background worker (think use Queue system)
You should avoid using queries in the loop. I would recommend to use one query which gets all the data at once and parse in on PHP side.
I would also recommend to check indexes. Multiple joins are very heavy when indexes not used. See https://dev.mysql.com/doc/refman/8.0/en/explain.html
In case that you need this data accessible in runtime, please consider the abilities for denormalisation of your table structure.
Related
My host limits my concurrent_connections to 70. I have a mock election program that will require 250 students to vote roughly at the same time. The vote process requires 3 pages each with one connection which I kill as soon as the queries are done. Can someone help me come up with a retry algorithm in my conn code please?
$ID_sql = $_POST['name'];
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error || $ID_sql == NULL) {
echo "<h1>The system is experiencing too much traffic at the moment, please try again in a few minutes</h1>";
echo "<a href='index.html'>Click here to go back</a><br>";
$conn->close();
die("Connection failed: Congestion" . $conn->connect_error);
//Instead of die, I would like to try 10-25 times after short sleep() before it dies
//This should randomize the connections a bit.
}
I want to insert time into MySQL and retrieve it then compare with current time.
Despite of the fact, a plenty of questions regarding to this topic, after hours of searching, most of them answered with mySQL Queries only or how to format timestamp. It is really hard to find out for my case. but I guess my problem differs. I cannot even start with the retrieved datum.
The idea is that when a data posted, it checks the last_update_time in DB with country_code. If last_update_time is within an hour, it just retrieve the time and other data. and If the the time difference is over an hour, it updates the row in DB.
The server is located on a remote site so the timezones are different.And using php 7 and mySQL 5.7
The inserting time into DB in a timezone specified works well, the last_updated_time field type is DATETIME in MySQL.
function insertData($countryCode) {
// Create connection
$conn = new mysqli(DBHOST, DBUSER, DBPASSWORD, DBNAME);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
date_default_timezone_set('Asia/Seoul');
$currentTime = date("Y-m-d H:i:s");
$sql = "INSERT INTO my_table (country_code, last_updated_time)
VALUES ('$countryCode', '$currentTime')";
if (mysqli_query($conn, $sql)) {
echo "INSERT Succeeded";
}else {
echo "Failed INSERT";
}
$conn->close();
}
and later I need to compare the old time(saved time) with current time when a page refreshes. I expect people from several timezones so I set date_default_timezone_set().
function compareTime($countryCode){
$conn = new mysqli(DBHOST, DBUSER, DBPASSWORD, DBNAME);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT last_updated_time FROM my_table WHERE country_code = '$countryCode'";
$savedTime = mysqli_query($conn, $sql);
if($savedTime->num_rows == 0) {
echo 'does not exist';
} else {
date_default_timezone_set('Asia/Seoul');
$currentTime = date("Y-m-d H:i:s");
$oldtime = strtotime($savedTime);
$timeDiffInSec = intval(strtotime($currentTime) - $oldtime) % 60;
$formattedOldTime = date("Y-m-d H:i:s",$oldtime);
echo '<p>Current Time: '.$currentTime.'</p>';
echo '<p>Old Time: '.$formattedOldTime.'</p>';
echo '<p>Time Difference: '.$timeDiffInSec.'</p>';
// Do the job only after 1 hour
if ($timeDiffInSec > 60 && $currentTime > $oldtime) {
// Do the job and update DB
}
}
$conn->close();
}
compareTime('us');
The problem I have is that I don't know how to properly get the saved time from mySQL in PHP. I cannot print the old time on the webpage or compare those two time.
Although the saved time looks like 2017-12-26 17:07:37 when I see via myPhpAdmin, those echos print like below.
Current Time: 2018-01-01 06:35:55
Old Time: 1970-01-01 09:00:00
Time Difference: 55
Even echo $savedTime; prints nothing. How can I resolve this? Thank you in advance.
$savedTime is a query result, you should fetch the results later on.
Try this:
$oldtime = strtotime(mysqli_fetch_array($savedTime)[0]);
By the way, as Funk Forty Niner reminded me on the comments, you should consider to use prepared statements to avoid SQL injection attacks. Have a look on the link, it's worth it.
Below is a simple example program to show the problem I am having. I am searching the database for new jobs executing them then going to sleep until there are new jobs. I am doing it this way because they must be run 1 at a time. However when I execute the script the server will not let any scripts run that access the database. Why is the database locked?
ignore_user_abort(true);//if caller closes the connection (if initiating with cURL from another PHP, this allows you to end the calling PHP script without ending this one)
//set_time_limit(0); //run forever
//start up
session_start();
ini_set("log_errors", 1);
ini_set("error_log", __file__.".log");
require_once("includes/loginMaster.php");
// Create connection
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_DATA);
if ($conn->connect_error) die("Connection failed: " . $conn->connect_error);
//setup statement for getting tasks
$query='SELECT `id`,`uid`,`kid`,`type`,`value`,`time` FROM `data_que` WHERE `executed` IS NULL ORDER BY `time` ASC, `id` ASC LIMIT 1';
$stmtNext = $conn->prepare($query);
$stmtNext->bind_result($id,$uid,$kid,$type,$value,$time);
//get packets
while(true) {
$stmtNext->execute();$stmtNext->store_result();
while ($stmtNext->fetch()) {
/*
should do somthing here but removed for simplicity
*/
//reexecute job
$stmtNext->execute();$stmtNext->store_result();
usleep(100);
}
sleep(5);
echo "slept";
}
I was wondering if anyone could shine a light on how to read from a database and pass it on to a sessions variable. I have tried with a product id and get but it did not work.
I'm looking for the basics on how to approach the issue.
I assume that you need to read from MySQL Database (for example).
First thing to do is reading PHP/MYSQL documentation.
http://www.w3schools.com/php/php_mysql_intro.asp
Second thing is to read about PHP Sessions.
http://www.w3schools.com/php/php_sessions.asp
And for example some code:
// Connect to MySQL Database and select all records from your_table
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$query = $conn->prepare("SELECT * FROM your_table");
$query->execute();
if($query == TRUE) {
// query success
}
To store information in $_SESSION variable you need to:
Call session_start(); before accessing this variable
$_SESSION['your_var'] = 'your_value';
Ugh. I've spent hours searching for a solution for this, so it's probably something simple. I have a stored procedure in SQL Server (2008) that I'm trying to call from PHP code. I'm using ODBC, and everything works if I use a hard-coded SQL statement.
But I'm trying to call a stored procedure that returns a set of rows (cursor, dataset, etc.), passing two parameters. When I run the code through my browser I just get a 500 http error. It is bombing on the odbc_execute() line. Here's my code:
// DSN-less connection:
$conn = odbc_connect("Driver={SQL Server};Server=$server;Database=$database;", '', '');
if (!$conn)
{exit("Connection Failed: " . $conn);}
$fromDate = new DateTime('2012-1-1');
$toDate = new DateTime('2013-12-31');
$sql = odbc_prepare($conn, "{call GET_EVENTS_BY_DATE_RANGE(?, ?)}");
$params = array($fromDate, $toDate);
$result = odbc_execute($sql, $params);
if ( $result === false)
{
exit("Query failed");
}
odbc_close($conn);
Again, no problems until it hits the odbc_execute() function. Any help is appreciated.