I am trying to populate a table displaying statistics. I have a list of campaign id's have been placed into an array using the following query:
$query = "SELECT `id` FROM `c_templates` ORDER BY `id`";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$id[] = $row['id'];
}
Once I have all of my id's, I am using a foreach loop to make 5 queries per id gathering all the table data.
foreach($id as $i){
// sent
$query = "SELECT `template`, COUNT(*) as count FROM `s_log` WHERE `template` = '".$i."' AND `time_sent` BETWEEN '".$start."' AND '".$stop."'";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$template[] = $row['template'];
$count[] = $row['count'];
}
// opens
$query = "SELECT COUNT(*) as count FROM `t_opens` WHERE `campaign_id` = '".$i."' AND `timestamp` BETWEEN '".$start."' AND '".$stop."'";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$opens[] = $row['count'];
}
// clicks
$query = "SELECT `campaign_id`, COUNT(*) as count FROM `t_analytics` WHERE `campaign_id` = '".$i."' AND `timestamp` BETWEEN '".$start."' AND '".$stop."'";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$click_count[] = $row['count'];
}
// conversions
$query = "SELECT `conversion_value`, COUNT(*) as count FROM `t_analytics` WHERE `campaign_id` = '".$i."' AND `timestamp` BETWEEN '".$start."' AND '".$stop."' AND `conversion_value` > 0";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$conversion_value[] = $row['conversion_value'];
$conversion_count[] = $row['count'];
}
// bounce rate
$query = "SELECT COUNT(*) AS `ck` FROM `s_log` WHERE `time_sent` BETWEEN '".$start."' AND '".$stop."' AND `status` = 'hardbounce' OR `status` = 'softbounce' AND `template` = '".$i."'";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$bounce_count[] = $row['ck'];
}
}
The issue is that this process takes 4-5 seconds to perform over 40-50 id records. I placed some timers after each query to confirm the queries were performing correctly and they were. I ran OPTIMIZE and double checked to be sure everything was properly indexed. As far as I can tell, the issue is not in mysql.
The only thing I could find is that there is a .1-.5 second delay after each loop cycle. When trying to run 40-50 rows, this definitely begins to add up.
My question: Is there a better and faster way to fetch this data? Is there something else I should be checking to speed the process up?
The only solution I could think of was to run one query for each statistic (5 total queries) fetching the data for all id and placing them in a array for later display. I'm not sure how that could be done or if it's even possible. It seems to me I would have to run a separate query for each id, but I'm hoping I'm wrong.
Any help will be greatly appreciated!!
Related
I'm having an issue of retrieving values from two different tables. Here's the code so far:
$result = mysqli_query($conn,"SELECT * FROM articles");
$num = mysqli_num_rows($result);
while ($row = mysqli_fetch_array($result)) {
$uid=$row['_uid'];
$result2 = mysqli_query($conn, "SELECT _username FROM users WHERE _id = '$uid' ";
$num2 = mysqli_num_rows($result2);
while ($row2 = mysqli_fetch_array($result2)) {
$username = $row2['_username'];
}
$divtext='<h3>'.$row['_posttype'].'</h3> <h2>'.$username.' </h2>';
}
I've been reading that I should preform this while inside a while with multiple query, also found on w3 that you could directly assign a value to a variable directly using:
"SELECT _username INTO $username FROM users WHERE _id = '$uid' LIMIT 1";
But this works in SQL inside myadmin, in a php I can't find how to cast it.
I have also replace the fetch_row for fetch_assoc and still nothing, im struggling for two days already with this.
you could select al the value using a single query
SELECT a._uid , a._posttype, u._username
FROM articles a
INNER JOIN users u on a._uid = u._id
..
$result = mysqli_query($conn,
"SELECT a._uid , a._posttype, u._username
FROM articles a
INNER JOIN users u on a._uid = u._id");
$num = mysqli_num_rows($result);
while ($row = mysqli_fetch_array($result)) {
$divtext='<h3>'.$row['_posttype'].'</h3> <h2>'.$username.' </h2>';
}
$echo $divtext;
I have a table with a column called 'status'. The defaut value of 'status' is 0. I want to update the value to '1' after using it.
I basically want to check if the status is 0, if it is, do an operation and then change the value to 1.
Here is the code. All works perfectly except that the value of 0 is not changed to 1.
I am a novice so maybe is a very basic mistake :(
<?php
$sql = "SELECT number, status FROM summonerid";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$SummonerID = $row["number"];
$status = $row["status"];
if($status=='0'){
$recentgames=$lol->getRecentGames($SummonerID);
$MatchID1=$recentgames->games[0]->gameId;
$sql = "INSERT INTO matchid (number) SELECT * FROM (SELECT '$MatchID1') AS tmp WHERE NOT EXISTS (SELECT number FROM matchid WHERE number = '$MatchID1') LIMIT 1;";
$sql = "UPDATE summonerid SET status='1' WHERE status='0';"; // THIS IS THE PART THAT DOES NOT WORK WELL
}
}
}
?>
Any help would be highly appreciated
Try this.. you are not executing the sql statements
$sql = "SELECT number, status FROM summonerid";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$SummonerID = $row["number"];
$status = $row["status"];
if($status=='0'){
$recentgames=$lol->getRecentGames($SummonerID);
$MatchID1=$recentgames->games[0]->gameId;
$sql = "INSERT INTO matchid (number) SELECT * FROM (SELECT '$MatchID1') AS tmp WHERE NOT EXISTS (SELECT number FROM matchid WHERE number = '$MatchID1') LIMIT 1;";
$result1 = $conn->query($sql);
$sql = "UPDATE summonerid SET status='1' WHERE status='0';";
$result1 = $conn->query($sql);
}
}
}
?>
I can see in my logcat that the $check query is only running once, when it should be running once for every result of the $result query, which is 6. I've been troubleshooting this for hours and I just can't figure out why it's not working properly.
Is there an obvious reason why $check is only running once?
$result = mysql_query("SELECT * FROM `posts` WHERE facebook_id = $fbid ORDER BY id DESC") or die(mysql_error());
while ($row1 = mysql_fetch_array($result))
{
$mytime = $row1['time'];
$mylat = $row1['latitude'];
$mylon = $row1['longitude'];
$mypostid = $row1['id'];
// get all products from products table
$check = mysql_query("SELECT facebook_id as fid, id as uid,
TIMESTAMPDIFF(SECOND, `time`, '$mytime') AS timediff
FROM `posts`
WHERE `facebook_id` != $fbid
HAVING `timediff` <= '180'
ORDER BY `time` DESC") or die(mysql_error());
if (mysql_num_rows($check) > 0)
{
$response["products"] = array();
while ($row = mysql_fetch_array($check))
{
// temp user array
$product = array();
$product["facebookid"] = $row["fid"];
$product["timediff"] = $row["timediff"];
$product["theirpostid"] = $row["uid"];
$product["mypostid"] = $mypostid;
// push single product into final response array
array_push($response["products"], $product);
}
}
}
$response["success"] = 1;
echo json_encode($response);
I guess this is because you set
$response["products"] = array();
inside every cycle. Move it out of this inner while.
Hello my question is quite simple, yet I can not understand why it works like this.
When I have set the varialbe $query once and then set it again it does not work.
Only when using an other name for the variable, for example $query2 it works.
WORKS :
$connect = mysql_connect("","","") or die(mysql_error());
mysql_select_db("");
$query = "select post_title,id from wp_posts where post_status='publish'";
$result = mysql_query($query);
while($row = mysql_fetch_row($result))
{
WHILE 1
$query2 = "SELECT meta_key FROM `wp_postmeta` WHERE post_id='$id'";
$result2 = mysql_query($query2);
while($row2 = mysql_fetch_row($result2))
{
WHILE 2
}
}
NOT WORKS :
$connect = mysql_connect("","","") or die(mysql_error());
mysql_select_db("");
$query = "select post_title,id from wp_posts where post_status='publish'";
$result = mysql_query($query);
while($row = mysql_fetch_row($result))
{
WHILE 1
$query = "SELECT meta_key FROM `wp_postmeta` WHERE post_id='$id'";
$result = mysql_query($query);
while($row2 = mysql_fetch_row($result))
{
WHILE 2
}
}
You are not only changing the $query variable, but also the $result. Which is in your while, screwing up the first result set. You just have to assign a new result set.
This will work:
$query = "select post_title,id from wp_posts where post_status='publish'";
$result = mysql_query($query);
while($row = mysql_fetch_row($result))
{
WHILE 1
$query = "SELECT meta_key FROM `wp_postmeta` WHERE post_id='$id'";
$result2 = mysql_query($query);
while($row2 = mysql_fetch_row($result2))
{
WHILE 2
}
}
$result is a HANDLE representing the results of the query. Your second query is OVERWRITING the result of the first query, effectively killing the first query's results:
$result = mysql_query($query1);
while($row = mysql_fetch_row($result)) {
$result = mysql_query(...);
^^^^^^^---- overwrite occurs here
You run query #1, start the loop, run query #2. That query's results replaces the result from the first query, and gets completely consumed by the inner while() loop. When the inner loop finishes and control goes back up to the outer loop, there's no more data in this new $result handle, so the outer loop terminates as well.
Its because you are using while in while, it means yours second while:
$result = mysql_query($query);
while($row2 = mysql_fetch_row($result))
will overwrite yours first $result.
I have a query that gets 5 lines of data like this example below
$query = "SELECT ref,user,id FROM table LIMIT 0, 5";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$ref = $row['ref'];
}
I want to run a query inside each results like this below
$query = "SELECT ref,user,id FROM table LIMIT 0, 5";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$ref = $row['ref'];
$query = "SELECT domain,title FROM anothertable WHERE domain = '$ref'";
$result = mysql_query($query) or die(mysql_error());
if (mysql_num_rows($result) )
{
$title = $row['title'];
} else {
$title = "No Title";
}
echo "$ref - $tile";
}
but for some reason it's only display the first line when I add the query inside it. I can seem to make it run all 5 queries.
SELECT
t1.ref,
t1.user,
t1.id,
t2.domain,
t2.title
FROM
table AS t1
LEFT JOIN anothertable AS t2 ON
t2.domain = t1.ref
LIMIT
0, 5
The problem is that inside the while-cycle you use the same variable $result, which then gets overridden. Use another variable name for the $result in the while cycle.
You change the value of your $query in your while loop.
Change the variable name to something different.
Ex:
$query = "SELECT ref,user,id FROM table LIMIT 0, 5";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$ref = $row['ref'];
$qry = "SELECT domain,title FROM anothertable WHERE domain = '$ref'";
$rslt = mysql_query($qry) or die(mysql_error());
if (mysql_num_rows($rslt) )
{
$title = $row['title'];
} else {
$title = "No Title";
}
echo "$ref - $tile";
}
Use the following :
$query = "SELECT ref,user,id FROM table LIMIT 0, 5";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$ref = $row['ref'];
$query_domain = "SELECT domain,title FROM anothertable WHERE domain = '$ref'";
$result_domain = mysql_query($query_domain) or die(mysql_error());
if (mysql_num_rows($result_domain) )
{
$row_domain = mysql_fetch_row($result_domain);
$title = $row_domain['title'];
} else {
$title = "No Title";
}
echo "$ref - $title";
}
This is a logical problem. It happens that way, because you are same variable names outside and inside the loop.
Explanation:
$query = "SELECT ref,user,id FROM table LIMIT 0, 5";
$result = mysql_query($query) or die(mysql_error());
// Now $results hold the result of the first query
while($row = mysql_fetch_array($result))
{
$ref = $row['ref'];
//Using same $query does not affect that much
$query = "SELECT domain,title FROM anothertable WHERE domain = '$ref'";
//But here you are overriding the previous result set of first query with a new result set
$result = mysql_query($query) or die(mysql_error());
//^ Due to this, next time the loop continues, the $result on whose basis it would loop will already be modified
//..............
Solution 1:
Avoid using same variable names for inner result set
$query = "SELECT ref,user,id FROM table LIMIT 0, 5";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$ref = $row['ref'];
$query = "SELECT domain,title FROM anothertable WHERE domain = '$ref'";
$sub_result = mysql_query($query) or die(mysql_error());
// ^ Change this variable so that it does not overrides previous result set
Solution 2:
Avoid the double query situation. Use joins to get the data in one query call. (Note: You should always try to optimize your query so that you will minimize the number of your queries on the server.)
SELECT
ref,user,id
FROM
table t
INNER JOIN
anothertable t2 on t.ref t2.domain
LIMIT 0, 5
Learn about SQL joins:
SELECT table.ref, table.user, table.id, anothertable.title
FROM table LEFT JOIN anothertable ON anothertable.domain = table.ref
LIMIT 5
You're changing the value of $result in your loop. Change your second query to use a different variable.
it is not give proper result because you have used same name twice, use different name like this edit.
$query = "SELECT ref,user,id FROM table LIMIT 0, 5";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$ref = $row['ref'];
$query1 = "SELECT domain,title FROM anothertable WHERE domain = '$ref'";
$result1 = mysql_query($query1) or die(mysql_error());
if (mysql_num_rows($result1) )
{
$title = $row['title'];
} else {
$title = "No Title";
}
echo "$ref - $tile";
}