HaggisQueen Posted May 22, 2014 Share Posted May 22, 2014 Hello! I have a php page which shows a list of customers displayed in an html table format, The first column in the table contains a link for the user to select the row which then navigates to another php page, which is the customer order page. On this page, I want to get certain information based on the name of the customer to prepopulate the customer fields on this form, so I have some php code near the top of the page, as per attached document. I have checked to ensure that the $_GET['business_name'] contains a value. The query runs fine in myssql and returns only one record when I provide a value for the business_name, however when I run the code in php, the $result variable shows 'Resource id ='7' type='mySQL result'. I am a total newbie on PHP/MySQL and inherited this application. I have tried to find solutions on line through various forums, but no luck. Perhaps I am not asking/looking for the right question. Anyway, any assistance would be greatly appreciated. customer_order_php.php Quote Link to comment https://forums.phpfreaks.com/topic/288695-query-issue-in-php/ Share on other sites More sharing options...
ginerjm Posted May 22, 2014 Share Posted May 22, 2014 If you had posted the code, we could see it. Some of us don't click on links that we are unfamiliar with. Quote Link to comment https://forums.phpfreaks.com/topic/288695-query-issue-in-php/#findComment-1480517 Share on other sites More sharing options...
HaggisQueen Posted May 22, 2014 Author Share Posted May 22, 2014 “<?php include("session.php"); ?> <!--get customer information for customer name selected in view_customer_list--> <?php $business_name = $_GET['business_name']; $this_query = "Select business_name, account_num, main_contact, business_phone, business_email, business_suite, business_address, business_city, business_region, business_province, business_postal FROM orders WHERE sale_datetime in (Select Max(sale_datetime) from orders Group by business_name) and business_name = '$business_name' ORDER BY business_name ASC "; $result = mysql_query($this_query); $row_customer = mysql_fetch_array($result); ?> Quote Link to comment https://forums.phpfreaks.com/topic/288695-query-issue-in-php/#findComment-1480523 Share on other sites More sharing options...
ginerjm Posted May 22, 2014 Share Posted May 22, 2014 Add this right after the query call and before the fetch call: if (!$result) { echo "Query did not run - ".MySQL_error(); exit(); } I really don't like your where clause. Didn't know you could use a select as the object of an 'in' clause Quote Link to comment https://forums.phpfreaks.com/topic/288695-query-issue-in-php/#findComment-1480524 Share on other sites More sharing options...
joallen Posted May 22, 2014 Share Posted May 22, 2014 (edited) give the following a try: while ($row = mysql_fetch_assoc($result)){ DO WHAT YOU NEED WITH EACH ROW OF THE DATA HERE } By using mysql_fetch_assoc you can use the name of the column (eg. $row['account_num']) instead of referencing the key in the array. You could even build your html and set it to a variable like this: <?php $business_name = $_GET['business_name']; $this_query = "Select business_name, account_num, main_contact, business_phone, business_email, business_suite, business_address, business_city, business_region, business_province, business_postal FROM orders WHERE sale_datetime in (Select Max(sale_datetime) from orders Group by business_name) and business_name = '$business_name' ORDER BY business_name ASC "; $mytablehtml = ''; $result = mysql_query($this_query) or die(mysql_error()); if (!$result) { echo "Query did not run - ".MySQL_error(); //This will spit out any errors with the sql statement exit(); } while ($row = mysql_fetch_assoc($result)){ $mytablehtml .= '<tr><td>'.$row['business_name'].'</td><td>'.$row['account_num'].'</td></tr>'; } ?> <html> <head></head> <body> <table> <thead><tr><th>Business Name</th><th>Account Number</th></tr></thead> <tbody><?php echo $mytablehtml;?></tbody> </table> </body> </html> Keep in mind, when using the if (!result) function it will output the error with the sql statement on the page and that is it. This is a great way to debug your sql statements as it will pinpoint what the problem is. Personally I like to remove the mysql_error() part of echo statement in real applications and just echo a text stating what the problem might be as the mysql_error() statement contains information regarding file paths and server info. Hope that helps! Edited May 22, 2014 by joallen Quote Link to comment https://forums.phpfreaks.com/topic/288695-query-issue-in-php/#findComment-1480529 Share on other sites More sharing options...
HaggisQueen Posted May 23, 2014 Author Share Posted May 23, 2014 Thanks to you both for the time you took to reply and for your suggestions, neither of which worked unfortunately. I am going to go back to the drawing board and rethink my approach to this as I know that there has to be a more simpler way of doing this. gnerjm is right, the query being used (while it works in MySQL) needs to change as I think this is the problem. Quote Link to comment https://forums.phpfreaks.com/topic/288695-query-issue-in-php/#findComment-1480608 Share on other sites More sharing options...
Strider64 Posted May 23, 2014 Share Posted May 23, 2014 Since you're going back to the drawing board, you might as well look into mysqli or PDO (my recommendation) for mysql is depreciated. Quote Link to comment https://forums.phpfreaks.com/topic/288695-query-issue-in-php/#findComment-1480610 Share on other sites More sharing options...
ginerjm Posted May 23, 2014 Share Posted May 23, 2014 Deprecated! Deprecated. Say it 3 times fast. In a way MySQL is "depreciated" or ever under-appreciated but the true term for what has happened to the interface is "deprecated'. To quote that well-known mantra - "there is no I in deprecated". Quote Link to comment https://forums.phpfreaks.com/topic/288695-query-issue-in-php/#findComment-1480617 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.