Jump to content

query issue in PHP


HaggisQueen

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/288695-query-issue-in-php/
Share on other sites


“<?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);

?>

Link to comment
https://forums.phpfreaks.com/topic/288695-query-issue-in-php/#findComment-1480523
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/288695-query-issue-in-php/#findComment-1480524
Share on other sites

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!

Link to comment
https://forums.phpfreaks.com/topic/288695-query-issue-in-php/#findComment-1480529
Share on other sites

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. :happy-04:

Link to comment
https://forums.phpfreaks.com/topic/288695-query-issue-in-php/#findComment-1480608
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.