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.
Related
my while loop stops after executed another query inside... can you correct my codes? I want to update the column status in table ordered_items_supplier to "Pending" when the pi_number is found in the table purchased_items_supplier and if not found the column status is "Active".
$sql2 = "select * from ordered_items_supplier";
$result = $connect->query($sql2);
if($result->num_rows > 0){
while ($row = $result->fetch_assoc()) {
$pi_number = $row['pi_number'];
$sql = "select * from purchased_items_supplier where pi_number = '$pi_number'";
$result = $connect->query($sql);
if($result->num_rows > 0){
while ($row2 = $result->fetch_assoc()) {
$pi_number = $row2['pi_number'];
$sql = "update ordered_items_supplier set status = 'Pending' where pi_number = '$pi_number'";
$query = $connect->query($sql);
}
}else{
$sql = "update ordered_items_supplier set status = 'Delivered' where pi_number = '$pi_number'";
$query = $connect->query($sql);
}
}
}
here's my mysql.. it should update the status "Delivered" in ID 11
The problem is overwriting the same variable each time.
Check that you use $result for the outer and inner query both.That's why the problem occur. So don't overwriting the $result variable.
I want to get some of the entries from my db which has dates of only today, here is my working code which gets all of the data;
$result = mysql_query("SELECT *FROM products") or die(mysql_error());
// check for empty result
if (mysql_num_rows($result) > 0) {
// looping through all results
// products node
$response["products"] = array();
while ($row = mysql_fetch_array($result)) {
// temp user array
$product = array();
$product["pid"] = $row["pid"];
$product["name"] = $row["name"];
$product["price"] = $row["price"];
$product["description"] = $row["description"];
$product["created_at"] = $row["created_at"];
$product["updated_at"] = $row["updated_at"];
// push single product into final response array
array_push($response["products"], $product);
}
// success
$response["success"] = 1;
// echoing JSON response
echo json_encode($response);
}
As well as allowing the user to pick a date and display only the data that was created at that date.
The field is;
name created_at
type timestamp
null No
default CURRENT_TIMESTAMP
In the Mysql query you can use DATE() to strip the time of day off of your timestamp like so:
SELECT * FROM products WHERE DATE(created_at) = '<date>';
i.e. this could be implemented as
$result = mysql_query("SELECT * FROM products WHERE DATE(created_at) = '".date("Y\-m\-d")."'") or die(mysql_error());
or if you have $inputdate set to be the user specified date.
$result = mysql_query("SELECT * FROM products WHERE DATE(created_at) = '".$inputdate->format("Y\-m\-d\")."'") or die(mysql_error());
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!!
Quick Question;
$sql = mysql_query("SELECT * FROM cronjobs WHERE status = 0 ");
while($cronjob = mysql_fetch_array($sql)){
if($cronjob['suid'] != $cronjob['cuid']){
//echo 'not equal<br>';
$set = 0;
$sid = $cronjob['sid'];
$suid = $cronjob['suid'];
$cuid = $cronjob['cuid'];
$set = notify($sid, $suid, $cuid);
if($set==1){
//echo 'notified<br>';
$sql = "UPDATE cronjobs SET status = '1' WHERE id='".$cronjob['id']."'";
if(mysql_query($sql)){
echo '1<br>';
$set = 0;
}
}
}
}
}
notify() will return 1 (numeric)
The problem is only one iteration of the while loop is executed even though there are more records. I don't know what's wrong here. Help me out pls.
Please change inner $sql variable name to something else..outer $sql and inner one are making conflict
$sql = mysql_query("SELECT * FROM cronjobs WHERE status = 0 ");
while($cronjob = mysql_fetch_array($sql)){
if($cronjob['suid'] != $cronjob['cuid']){
//echo 'not equal<br>';
$set = 0;
$sid = $cronjob['sid'];
$suid = $cronjob['suid'];
$cuid = $cronjob['cuid'];
$set = notify($sid, $suid, $cuid);
if($set==1){
//echo 'notified<br>';
$sql_2 = "UPDATE cronjobs SET status = '1' WHERE id='".$cronjob['id']."'";
if(mysql_query($sql_2)){
echo '1<br>';
$set = 0;
}
}
}
}
}
Just an observation:
Because you have:
$sql = mysql_query("SELECT * FROM cronjobs WHERE status = 0 ");
while($cronjob = mysql_fetch_array($sql)){
Its going to execute the Query EVERY SINGLE time it goes through the loop. If you have a 100 rows, its going to execute 100 times. If you do this instead, then it executes only once.
$sql = mysql_query("SELECT * FROM cronjobs WHERE status = 0 ");
$res = mysql_fetch_array($sql);
while($cronjob = $res){
It wouldnt have conflicted either!
It is clearly an issue that occurs when you have the same variable for query ($query)
and Result Object ($result).try different name for mysql_query() inside the WHILE Loop.
Im trying to generate an array but not sure how to go about it.
I'm currently getting my data like so:
$query = mysql_query("SELECT * FROM users WHERE userEmail LIKE 'test#test.com'");
$row = mysql_fetch_array($query);
$query1 = mysql_query("SELECT * FROM categories");
while($row1 = mysql_fetch_array($query1)){
$query2 = mysql_query("SELECT * FROM usersettings WHERE userId = ".$row['userId']." AND usersettingCategory".$row1['categoryId']." LIKE 'y'");
$isyes = mysql_num_rows($query2);
if($isyes > 0){
$cat1 = mysql_query("SELECT * FROM shops WHERE shopstateId = 1 AND (categoryId1 = ".$row1['categoryId']." OR categoryId2 = ".$row1['categoryId']." OR categoryId3 = ".$row1['categoryId'].")");
$cat1match = mysql_num_rows($cat1);
if($cat1match > 0){
while($cat1shop = mysql_fetch_array($cat1)){
$cat1msg = mysql_query("SELECT * FROM messages WHERE shopId = ".$cat1shop['shopId']." and messagestateId = 1");
while($cat1msgrow = mysql_fetch_array($cat1msg)){
echo $cat1msgrow['messageContent']." - ".$cat1msgrow['messageCode'];
$cat1img = mysql_query("SELECT shopimagePath FROM shopimages WHERE shopimageId = ".$cat1shop['shopimageId']);
$imgpath = mysql_fetch_array($cat1img);
echo " - ".$imgpath['shopimagePath']."<br/>";
}
}
}
}
}
But this can cause duplicates when a user has all 3 of a shops categories picked in their preferences. I am trying to find a way to just pull the message ID out instead of the whole thing and put it into an array giving me, for example:
1,3,5,7,1,3,5,2,4,7,8
Then I can just run a separate query to say get me all messages where the ID is in the array, but i am unsure of the most constructive way to build such an array and examples of array from a while loop I have seen do not seem to be what I am looking for.
Is there anyone out there that can push me in the right direction?
Can't help with this code. But if you want an array from a query without duplicate result, you can use " select DISTINCT (id) " in your query or for more simple solution :
$id_arr = array();
$sql = mysql_query("select id from id_table");
while ($id_result = mysql_fetch_array($sql) {
$id = $id_result['id'];
if (!in_array($id, $id_arr)) {
$id_arr[] = $id;
}
}
I have found a much easier way to create the required result. I think at 6am after a hard night coding my brain was fried and I was making things a lot more complicated than I needed to. A simple solution to my issue is as follows:
$query = mysql_query("SELECT * FROM users WHERE userEmail LIKE 'test2#test2.com'");
$row = mysql_fetch_array($query);
$categories = "(";
$query1 = mysql_query("SELECT * FROM categories");
while($row1 = mysql_fetch_array($query1)){
$query2 = mysql_query("SELECT usersettingCategory".$row1['categoryId']." FROM usersettings WHERE userId = ".$row['userId']);
$row2 = mysql_fetch_array($query2);
if($row2['usersettingCategory'.$row1['categoryId']] == y){
$categories .= $row1['categoryId'].",";
}
}
$categories = substr_replace($categories ,")",-1);
echo $categories."<br />";
$query3 = mysql_query("SELECT * FROM shops,messages WHERE shops.shopId = messages.shopId AND messages.messagestateId = 1 AND (shops.categoryId1 IN $categories OR shops.categoryId2 IN $categories OR shops.categoryId3 IN $categories)");
while($row3 = mysql_fetch_array($query3)){
$query4 = mysql_query("SELECT shopimagePath FROM shopimages WHERE shopimageId = ".$row3['shopimageId']);
$row4 = mysql_fetch_array($query4);
echo $row3['messageContent']." - ".$row3['messageCode']." - ".$row4['shopimagePath']."<br />";
}