drbeamer Posted October 1, 2010 Share Posted October 1, 2010 noob question: I have following two queries I'd like to combined into one - how is this done? $temp = @mysql_query("SELECT * FROM purchased_leads WHERE leadID = '{$_REQUEST[leadid]}'"); "SELECT refundNotes FROM leads WHERE leadID = '{$_REQUEST[leadid]}'" Quote Link to comment https://forums.phpfreaks.com/topic/214930-two-queries-into-one/ Share on other sites More sharing options...
robert_gsfame Posted October 1, 2010 Share Posted October 1, 2010 use JOIN so u can use this $temp= mysql_query("SELECT purchased_leads.* FROM purchased_leads p JOIN refundNotes r ON p.leadID=r.leadID WHERE p.leadID='{$_REQUEST[leadid]}'"); hope that helps Quote Link to comment https://forums.phpfreaks.com/topic/214930-two-queries-into-one/#findComment-1118045 Share on other sites More sharing options...
drbeamer Posted October 1, 2010 Author Share Posted October 1, 2010 use JOIN so u can use this $temp= mysql_query("SELECT purchased_leads.* FROM purchased_leads p JOIN refundNotes r ON p.leadID=r.leadID WHERE p.leadID='{$_REQUEST[leadid]}'"); hope that helps Thanks for the fast reply, Robert, but I can't get it to work. Here is the complete code before and after the edit. Before teh edit, the code sends an email, but after editing there is no email sent anylonger. ORIGINAL: if(strtolower($prev_status) == "refund requested" && strtolower($status) == "refund denied"){ //refund denied send denied email //grab customer data $temp = @mysql_query("SELECT * FROM purchased_leads WHERE leadID = '{$_REQUEST[leadid]}'"); $cusid = mysql_result($temp, 0, 'recID'); $temp = mysql_query("SELECT email, firstName, lastName FROM user WHERE recid = ({$cusid})"); if(mysql_num_rows($temp)>0){ $customer_email = @mysql_result($temp, 0, 'email'); $customer_first_name = @mysql_result($temp, 0, 'firstName'); $customer_last_name = @mysql_result($temp, 0, 'lastName'); $templatename = "reviewer_deny_refund"; $strings = Array ( '%FIRST_NAME%' => $customer_first_name, '%LAST_NAME%' => $customer_last_name, '%LEADID%' => $_REQUEST[leadid], '%EMAIL_ADDRESS%' => $customer_email, '%SITE_NAME%' => $label['site_name'], '%REQUEST_DATETIME%' => date("Y-m-d H:i:s") ); // Format the e-mail $email_data = format_email($templatename, $strings); // Strings to replace in the subject line $strings = Array ( '%SITE_NAME%' => $label['site_name'], '%LEADID%' => $_REQUEST[leadid]); $email_data[subject] = format_subject($email_data[subject], $strings); //echo "DEBUG: line 63<br>"; // Send the e-mail if($customer_email !=""){ mail($customer_email, $email_data[subject], $email_data[message], "From: {$email_address[admin]}\r\n"); } } } EDITED: if(strtolower($prev_status) == "refund requested" && strtolower($status) == "refund denied"){ //refund denied send denied email //grab customer data $temp = mysql_query("SELECT purchased_leads.* FROM purchased_leads p JOIN refundNotes r ON p.leadID=r.leadID WHERE p.leadID='{$_REQUEST[leadid]}'"); $cusid = mysql_result($temp, 0, 'recID'); $temp = mysql_query("SELECT email, firstName, lastName FROM user WHERE recid = ({$cusid})"); if(mysql_num_rows($temp)>0){ $customer_email = @mysql_result($temp, 0, 'email'); $customer_first_name = @mysql_result($temp, 0, 'firstName'); $customer_last_name = @mysql_result($temp, 0, 'lastName'); $refund_notes = @mysql_result($temp, 0, 'refundNotes'); $templatename = "reviewer_deny_refund"; $strings = Array ( '%FIRST_NAME%' => $customer_first_name, '%LAST_NAME%' => $customer_last_name, '%LEADID%' => $_REQUEST[leadid], '%EMAIL_ADDRESS%' => $customer_email, '%REFUND_NOTES%' => $refund_notes, '%SITE_NAME%' => $label['site_name'], '%REQUEST_DATETIME%' => date("Y-m-d H:i:s") ); // Format the e-mail $email_data = format_email($templatename, $strings); // Strings to replace in the subject line $strings = Array ( '%SITE_NAME%' => $label['site_name'], '%LEADID%' => $_REQUEST[leadid]); $email_data[subject] = format_subject($email_data[subject], $strings); // Send the e-mail if($customer_email !=""){ mail($customer_email, $email_data[subject], $email_data[message], "From: {$email_address[admin]}\r\n"); } } } Quote Link to comment https://forums.phpfreaks.com/topic/214930-two-queries-into-one/#findComment-1118063 Share on other sites More sharing options...
jcbones Posted October 2, 2010 Share Posted October 2, 2010 Of course it will not work like that. You are redefining the variable $temp, and if you are going to use alias' in your query, then you need to define the columns by them as well. if(strtolower($prev_status) == "refund requested" && strtolower($status) == "refund denied"){ //refund denied send denied email //grab customer data $temp1 = mysql_query("SELECT p.recID,r.refundNotes FROM purchased_leads p JOIN refundNotes r ON p.leadID=r.leadID WHERE p.leadID='{$_REQUEST[leadid]}'"); if(mysql_num_rows($temp1) > 0) { $row = mysql_fetch_row($temp1); $refund_notes = $row[1]; $cusid = $row[0]; } $temp2 = mysql_query("SELECT email, firstName, lastName FROM user WHERE recid = ({$cusid})"); if(mysql_num_rows($temp2)>0){ $customer_email = @mysql_result($temp, 0, 'email'); $customer_first_name = @mysql_result($temp, 0, 'firstName'); $customer_last_name = @mysql_result($temp, 0, 'lastName'); $refund_notes = @mysql_result($temp, 0, 'refundNotes'); $templatename = "reviewer_deny_refund"; $strings = Array ( '%FIRST_NAME%' => $customer_first_name, '%LAST_NAME%' => $customer_last_name, '%LEADID%' => $_REQUEST[leadid], '%EMAIL_ADDRESS%' => $customer_email, '%REFUND_NOTES%' => $refund_notes, '%SITE_NAME%' => $label['site_name'], '%REQUEST_DATETIME%' => date("Y-m-d H:i:s") ); // Format the e-mail $email_data = format_email($templatename, $strings); // Strings to replace in the subject line $strings = Array ( '%SITE_NAME%' => $label['site_name'], '%LEADID%' => $_REQUEST[leadid]); $email_data[subject] = format_subject($email_data[subject], $strings); // Send the e-mail if($customer_email !=""){ mail($customer_email, $email_data[subject], $email_data[message], "From: {$email_address[admin]}\r\n"); } } } Quote Link to comment https://forums.phpfreaks.com/topic/214930-two-queries-into-one/#findComment-1118190 Share on other sites More sharing options...
drbeamer Posted October 2, 2010 Author Share Posted October 2, 2010 Thanks jcbones, I tried your code, but it doesn't work either, meaning the email is not being sent. Right now the only code that results in an email being sent is the original code above without the refundNotes values. Quote Link to comment https://forums.phpfreaks.com/topic/214930-two-queries-into-one/#findComment-1118340 Share on other sites More sharing options...
jcbones Posted October 2, 2010 Share Posted October 2, 2010 And you have echo'd the sql queries so you can see how they are built. And you have checked for any errors returned from the database. And you have checked for any errors coming from the script. Quote Link to comment https://forums.phpfreaks.com/topic/214930-two-queries-into-one/#findComment-1118429 Share on other sites More sharing options...
drbeamer Posted October 3, 2010 Author Share Posted October 3, 2010 And you have echo'd the sql queries so you can see how they are built. And you have checked for any errors returned from the database. And you have checked for any errors coming from the script. Sorry; no. I am not a programmer; I don't know how to check this. All I know how to do is swap code and check the result. I received no errors on screen, however. Quote Link to comment https://forums.phpfreaks.com/topic/214930-two-queries-into-one/#findComment-1118447 Share on other sites More sharing options...
jcbones Posted October 4, 2010 Share Posted October 4, 2010 OK, replace: $temp1 = mysql_query("SELECT p.recID,r.refundNotes FROM purchased_leads p JOIN refundNotes r ON p.leadID=r.leadID WHERE p.leadID='{$_REQUEST[leadid]}'"); With: $temp1 = mysql_query("SELECT p.recID,r.refundNotes FROM purchased_leads p JOIN refundNotes r ON p.leadID=r.leadID WHERE p.leadID='{$_REQUEST[leadid]}'") or die('Error: ' . mysql_error()); This will tell you if the change to your database query is failing. Quote Link to comment https://forums.phpfreaks.com/topic/214930-two-queries-into-one/#findComment-1118766 Share on other sites More sharing options...
drbeamer Posted October 5, 2010 Author Share Posted October 5, 2010 OK, replace: $temp1 = mysql_query("SELECT p.recID,r.refundNotes FROM purchased_leads p JOIN refundNotes r ON p.leadID=r.leadID WHERE p.leadID='{$_REQUEST[leadid]}'"); With: $temp1 = mysql_query("SELECT p.recID,r.refundNotes FROM purchased_leads p JOIN refundNotes r ON p.leadID=r.leadID WHERE p.leadID='{$_REQUEST[leadid]}'") or die('Error: ' . mysql_error()); This will tell you if the change to your database query is failing. Thanks - this helped! I found the error, fixed it, but now there is another one: - The first error was that refundNotes is a column in "leads" and not "purchased_leads". Accordingly, I modified the code as follows: $temp1 = mysql_query("SELECT p.recID,r.refundNotes FROM leads p JOIN leads r ON p.leadID=r.leadID WHERE p.leadID='{$_REQUEST[leadid]}'") or die('Error: ' . mysql_error()); - The new error is this: Error: Unknown column 'p.recID' in 'field list' . I believe the reason for this error is that recID is a column in the purchased_leads table. So the correct code would pull recID from purchased_leads, refundNotes from leads, and combine them based upon the leadID variable found in both tables. Quote Link to comment https://forums.phpfreaks.com/topic/214930-two-queries-into-one/#findComment-1119349 Share on other sites More sharing options...
jcbones Posted October 6, 2010 Share Posted October 6, 2010 I hope I understood that. $temp1 = mysql_query("SELECT p.recID,r.refundNotes FROM purchased_leads p JOIN leads r ON p.leadID=r.leadID WHERE p.leadID='{$_REQUEST[leadid]}'") or die('Error: ' . mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/214930-two-queries-into-one/#findComment-1119463 Share on other sites More sharing options...
drbeamer Posted October 7, 2010 Author Share Posted October 7, 2010 This cannot work: $temp1 = mysql_query("SELECT p.recID,r.refundNotes FROM purchased_leads p JOIN leads r ON p.leadID=r.leadID WHERE p.leadID='{$_REQUEST[leadid]}'") or die('Error: ' . mysql_error()); The reason is that this query tries to select "refundNotes" from the table "purchased_leads". However, "refundNotes" is a column in the table named "leads". How do we reformat the query to select "refundNotes" from the table "leads", and select recID from table "purchased_leads" ? Quote Link to comment https://forums.phpfreaks.com/topic/214930-two-queries-into-one/#findComment-1119891 Share on other sites More sharing options...
jcbones Posted October 8, 2010 Share Posted October 8, 2010 This cannot work: $temp1 = mysql_query("SELECT p.recID,r.refundNotes FROM purchased_leads p JOIN leads r ON p.leadID=r.leadID WHERE p.leadID='{$_REQUEST[leadid]}'") or die('Error: ' . mysql_error()); The reason is that this query tries to select "refundNotes" from the table "purchased_leads". However, "refundNotes" is a column in the table named "leads". How do we reformat the query to select "refundNotes" from the table "leads", and select recID from table "purchased_leads" ? You are mistaken. In the query you are using an alias for "leads" called "r". In the SELECT clause we are asking for "r.refundNotes" which is asking for the refundNotes from the table "leads". Quote Link to comment https://forums.phpfreaks.com/topic/214930-two-queries-into-one/#findComment-1120416 Share on other sites More sharing options...
drbeamer Posted October 9, 2010 Author Share Posted October 9, 2010 Sorry if I mis-interpreted the code - but it's not working. There is no email being sent (the whole point of the code is to send an email containing refundNotes) , and I am not getting any errors on the screen either. Quote Link to comment https://forums.phpfreaks.com/topic/214930-two-queries-into-one/#findComment-1120435 Share on other sites More sharing options...
jcbones Posted October 9, 2010 Share Posted October 9, 2010 At the top of your script insert this line: error_reporting(E_ALL);ini_set('display_errors', 1); See if errors report to the screen. Quote Link to comment https://forums.phpfreaks.com/topic/214930-two-queries-into-one/#findComment-1120514 Share on other sites More sharing options...
drbeamer Posted October 9, 2010 Author Share Posted October 9, 2010 Thanks - but there are no errors on the screen. I didn't mean to take so much of your time when I initiated the question, but I just didn't know this was so complicated. Are you available to hire on an hourly basis? Would sending you the whole file make it easier to pinpoint the problem? John Quote Link to comment https://forums.phpfreaks.com/topic/214930-two-queries-into-one/#findComment-1120564 Share on other sites More sharing options...
jcbones Posted October 9, 2010 Share Posted October 9, 2010 Post the file with your email, and database connection details deleted. Make sure you enclose the code inside of [ php ] tags [ /php ]. <-no spaces of course. Quote Link to comment https://forums.phpfreaks.com/topic/214930-two-queries-into-one/#findComment-1120566 Share on other sites More sharing options...
drbeamer Posted October 13, 2010 Author Share Posted October 13, 2010 Post the file with your email, and database connection details deleted. Make sure you enclose the code inside of [ php ] tags [ /php ]. <-no spaces of course. Thanks again for all your help jcbones - problem is resolved. But I have other little coding jobs in case you are available/interested. Cheers. Quote Link to comment https://forums.phpfreaks.com/topic/214930-two-queries-into-one/#findComment-1121802 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.