<?php
$host= "localhost";
$user= "xxxxxx";
$pass= "xxxx";
$db="xxxxxxx";
$connect= mysql_connect($host,$user,$pass);
if (!$connect)die ("Cannot connect!");
mysql_select_db($db, $connect);
$result = mysql_query("
SELECT
*
FROM
values
");
if($result){
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
$url = $row['value'];
echo '<li><iframe src="http://xxxxxxx.xxxx/xxxx.php?value='.$url.'" width="300" height="100" scrolling="no" frameBorder="0""></iframe></li>';
}
}
?>
this is my php code I am using to get values from database. I want to use a time delay in each of the value.
like
http://xxxxxxx.xxxx/xxxx.php?value='.$url.'
wait 5 sec
http://xxxxxxx.xxxx/xxxx.php?value='.$url.'
wait 5 sec
and so on.
Is there any way I can do that.
Thanks.
wrong answer: use sleep(5) inside while().
right answer:
a) you should not use mysql_* functions
b) if you need a delay, get all rows, then output them one by one using JS.
OR:
c) again, using JS and ajax, query for new row every 5 seconds
If you'll stick to wrong answer, your script will die of timeout at ~6th row (in default php install)
You should understand client/server architecture better:
mysql query and your php-code is a server-side, it will not return any output to browser (your visitor), until its end, and you don't want your visitors to wait 30 or 300 seconds for the server to reply.
so only option you have is: query for new image every 5 seconds, or query them all and iterate over them.
There are many jquery/javascript tutorials on that subject.
Related
I have a large table with 500,000 records, on clicking of a button the data gets downloaded, but it is very slow especially on a bad internet.
I was thinking of Zipping the file and then save it but again I am sure it will take up extra memory for the whole process.
Is there a better way to optimize this CSV download.
<?php
// mysql database connection details
$host = "localhost";
$username = "admin";
$password = "root";
$dbname = "db_books";
// open connection to mysql database
$connection = mysqli_connect($host, $username, $password, $dbname) or die("Connection Error " . mysqli_error($connection));
// fetch mysql table rows
$sql = "select * from tbl_books";
$result = mysqli_query($connection, $sql) or die("Selection Error " . mysqli_error($connection));
$fp = fopen('books.csv', 'w');
while($row = mysqli_fetch_assoc($result))
{
fputcsv($fp, $row);
}
fclose($fp);
//close the db connection
mysqli_close($connection);
?>
I would use MySQL INTO OUTFILE. It is much faster than looping through the results of your query. You add this to your select statement and MySQL will take care of creating your file for you.
See more documentation on the abilities here.
It sounds like you're using the page-load thread to compile the CSV before sending to the user. This is why it seems so slow.
If possible, you might want to simply pre-compile the CSV downloads, before the user gets to that point. That way their browser will simply receive the file, not hang while you generate it. If you're concerned about wasting too much time generating files that users never download, perhaps have a background job that generates files when needed, but only if the user has logged on (or into a certain area of your site) within the last X hours.
Alternatively, maybe you could use jQuery/Ajax to display a pop-up dialog that tells the user to wait while their file is being generated, and then disappears once the download is ready.
I have a mysql database and i want to execute a query and while this query is being executed the data should be displayed in page.
so for example if i have 1,000 result row from the query result i want to display each row while the query is being executed instead of waiting till the query finishes executing then displaying them at once.
here is my php code:
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
// Database Name
mysql_select_db("dbname", $con);
$test_query = mysql_query("SELECT * FROM V_Posts where _PID > 100 and _PID < 10000");
while($CHECK_PCID_R = mysql_fetch_array($test_query))
{
echo $CHECK_PCID_R['_PID'] . "<br />";
}
?>
I tried
echo $CHECK_PCID_R['_PID'] . "<br />";
flush();
But it didn't work :(
One query will produce one dataset and you'll have all the data at once. If your query is slow any latency in displaying the data will be small compared to the delay in receiving it. Using flush() might force the server to send parts of the page, but you're really just tinkering at the edges.
If you want to break this down you'll have to run multiple queries, which will arguably be much slower since you'll be running the same query repeatedly. This will load the database server unnecessarily, and will achieve only a minor cosmetic effect.
If you use an AJAX call to retrieve your data you can display a 'loading' message while you wait. You could use multiple AJAX calls to display the data bit by bit - this is even worse than using multiple queries in the PHP script.
I've been trying to figure this out for a few days now. I have 4 databases that I need to connect to. I am trying to alternate between the connections but it wont let me. It always takes the last connection.
$link = mysql_connect("localhost","root","");
mysql_select_db("front",$link);
$link2 = mysql_connect("mysite.com","user","2012");
mysql_select_db("db1",$link2);
$link3 = mysql_connect("mysite.com","user","2012");
mysql_select_db("appsdb",$link3);
$link4 = mysql_connect("mysite.com","user","2012", TRUE);
mysql_select_db("storagedb",$link4);
what do I need to do?
This works but takes too long
$link = mysql_connect("localhost","root","");
mysql_select_db("front",$link);
$link2 = mysql_connect("mysite.com","user","2012",true);
mysql_select_db("db1",$link2);
$link3 = mysql_connect("mysite.com","user","2012",true);
mysql_select_db("appsdb",$link3);
$link4 = mysql_connect("mysite.com","user","2012", true);
mysql_select_db("storagedb",$link4);
Is it possible to separate the connections altogether but still use $link, $link2, $link3, $link4 inside the queries? I dont think its efficient to keep all connections open.
I won't comment on using mysqli like others did, I guess from all these comments, you are already aware that you should switch, so I will just reply with the answer and maybe it can help other people who just can't upgrade php for some reason.
The function mysql_connect creates OR reuse a connection, so if you create multiple connections to the same server with the same credentials, you will get just a single connection.
If you NEED to force mysql_connect to create a new connection, you need to specify the new_link parameter as true as follow:
$link = mysql_connect("localhost","root","",true);
mysql_select_db("front",$link);
$link2 = mysql_connect("mysite.com","user","2012",true);
mysql_select_db("db1",$link2);
$link3 = mysql_connect("mysite.com","user","2012",true);
mysql_select_db("appsdb",$link3);
$link4 = mysql_connect("mysite.com","user","2012", true);
mysql_select_db("storagedb",$link4);
You can find more information here: http://php.net/manual/es/function.mysql-connect.php
Questions like these are rampant in this site, I suppose your search for one like this for more help.
$db_conn = connect_db(host, user, pwd);
mysql_select_db('existing_db', $db_conn);
-- do selects and scrub data --
mysql_select_db('new_db', $db_conn);
-- insert the required data --
Full link Here
just add the ",$link" to the end of your query statement...
mysql_query("SELECT * FROM table", $link);
why not using persistent connections mysql_pconnect, but consider to switch to pdo or mysqlnd
(counterparts of persistent connections 1 2)
I'm having trouble getting a table to lock in MySQL. I've tried testing for concurrent requests by running two scripts at the same time on different browsers.
Script 1:
mysql_query("lock tables id_numbers write");
$sql = "select number from id_numbers";
$result = mysql_query($sql);
if ($record = mysql_fetch_array($result))
{
$id = $record['number'];
}
sleep(30);
$id++;
$sql = "update id_numbers set number = '$id'";
$result = mysql_query($sql);
mysql_query("unlock tables");
Script 2:
$sql = "select number from id_numbers";
$result = mysql_query($sql);
if ($record = mysql_fetch_array($result))
{
$id = $record['number'];
}
echo $id;
I start Script 1 first, then start Script 2. Theoretically, Script 2 should wait 30+ seconds until Script 1 unlocks the table, and then output the updated ID. But it immediately outputs the original ID, so the lock is obviously not taking effect. What could be going wrong?
BTW, I know I shouldn't still be using mysql_*, but I'm stuck with it for now.
EDIT: I've discovered that the lock does happen on the live site, but not on my dev site. They are both on shared hosts, so I'm assuming there's some setting that's different between the two. Any idea what that could be?
Using the code you have here, I get exactly the behavior you expect: script 2 will block until script 1 issues the unlock tables.
Your problem must lie elsewhere. Things to check:
Does the table actually exist on the database? (Check your mysql_query() return values and use mysql_error() if you get any FALSE returns.)
Are both scripts connecting to the same database?
Are you sure the table is not a temporary (thus connection-local) table?
Could script 1's mysql connection (or the script itself) be timing out during the sleep(30), thus releasing the lock earlier than you expect?
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).