mysql query error(need help)

Thread Solved

Join Date: Jan 2009
Posts: 125
Reputation: blocker is an unknown quantity at this point 
Solved Threads: 0
blocker's Avatar
blocker blocker is offline Offline
Junior Poster

mysql query error(need help)

 
0
  #1
Mar 23rd, 2009
  1. $_SESSION['MM_paymentstatus']="to be verified";
  2. $modeofpay="Credit Card";
  3. mysql_select_db($database_enamysqldb, $enamysqldb);
  4. $query_recunvesummary=sprintf("SELECT * FROM paymentsummary WHERE username=%s AND paymentmode=%s AND foryear=%s AND initialstatus=%s", GetSQLValueString($_SESSION['MM_Username'], "text"), GetSQLValueString($modeofpay, "text"), GetSQLValueString($_SESSION['MM_yearlynow'], "text"), GetSQLValueString($_SESSION['MM_paymentstatus'], "text"));
  5.  
  6. $recunvesummary = mysql_query($query_recunvesummary, $enamysqldb) or die(mysql_error());
  7. $row_recunvesummary = mysql_fetch_assoc($recunvesummary);
  8. $totalRows_recunvesummary = mysql_num_rows($recunvesummary);
  9.  
  10. $totalunvepayment=0;
  11. while($row = mysql_fetch_array($recunvesummary)){
  12. $totalunvepayment = $totalunvepayment + $row['amountpaid'];
  13. }
  14. $_SESSION['MM_totalunvepayment']=$totalunvepayment;
i dont know if theres something wrong on the query because the debugger does not detect an error.But it retuns $totalunvepayment to zero(0) even if there is a value in the mysqldatabase.!

Whats wrong with this.

Pls help
God bless on this mother Earth.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,517
Reputation: cwarn23 has a spectacular aura about cwarn23 has a spectacular aura about cwarn23 has a spectacular aura about 
Solved Threads: 136
cwarn23's Avatar
cwarn23 cwarn23 is offline Offline
Posting Virtuoso

Re: mysql query error(need help)

 
0
  #2
Mar 23rd, 2009
Try and simplify the mysql query with the following on line 4:
  1. $query_recunvesummary="SELECT * FROM paymentsummary WHERE username='".mysql_real_escape_string($_SESSION['MM_Username'])."' AND paymentmode='".mysql_real_escape_string($modeofpay)."' AND foryear='".mysql_real_escape_string($_SESSION['MM_yearlynow'])."' AND initialstatus='".mysql_real_escape_string($_SESSION['MM_paymentstatus'])."'");
And also make sure the variables/arrays have valid values.
Try not to bump 10 year old threads as it can be really annoying.
http://syntax.cwarn23.net/
Smilies: ^_* +_+ v_v -_- *~*`
My favourite PC. - MacGyver Fan
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 165
Reputation: Fest3er is an unknown quantity at this point 
Solved Threads: 18
Fest3er Fest3er is offline Offline
Junior Poster

Re: mysql query error(need help)

 
0
  #3
Mar 23rd, 2009
In short, "WHERE field=value", you should have "value" inside single quotes ('). I stubbed my toes on this many times until I learned to put the value in quotes. Unless it was a function, of course.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 125
Reputation: blocker is an unknown quantity at this point 
Solved Threads: 0
blocker's Avatar
blocker blocker is offline Offline
Junior Poster

Re: mysql query error(need help)

 
0
  #4
Mar 23rd, 2009
Originally Posted by cwarn23 View Post
Try and simplify the mysql query with the following on line 4:
  1. $query_recunvesummary="SELECT * FROM paymentsummary WHERE username='".mysql_real_escape_string($_SESSION['MM_Username'])."' AND paymentmode='".mysql_real_escape_string($modeofpay)."' AND foryear='".mysql_real_escape_string($_SESSION['MM_yearlynow'])."' AND initialstatus='".mysql_real_escape_string($_SESSION['MM_paymentstatus'])."'");
And also make sure the variables/arrays have valid values.
Ive modified the code a little bit but still it returns $totalunvepayment to zero:Whats wrong with this code.?

  1. $query_recunvesummary="SELECT * FROM paymentsummary WHERE username='".mysql_real_escape_string($_SESSION['MM_Username'])."' AND paymentmode='".mysql_real_escape_string($modeofpay)."' AND foryear='".mysql_real_escape_string($_SESSION['MM_yearlynow'])."' AND initialstatus='".mysql_real_escape_string($_SESSION['MM_paymentstatus'])."'";
  2.  
  3. $recunvesummary = mysql_query($query_recunvesummary, $enamysqldb) or die(mysql_error());
  4. $row_recunvesummary = mysql_fetch_assoc($recunvesummary);
  5. $totalRows_recunvesummary = mysql_num_rows($recunvesummary);
  6.  
  7. $totalunvepayment=0;
  8. while($row = mysql_fetch_array($recunvesummary)){
  9. $totalunvepayment = $totalunvepayment + $row['amountpaid'];
  10. }
  11. $_SESSION['MM_totalunvepayment']=$totalunvepayment;

When i echo the sql statement its echos (Resource ID #9) what does this mean.?

Pls help.

God bless on this mother earth.
Last edited by blocker; Mar 23rd, 2009 at 9:17 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 165
Reputation: Fest3er is an unknown quantity at this point 
Solved Threads: 18
Fest3er Fest3er is offline Offline
Junior Poster

Re: mysql query error(need help)

 
0
  #5
Mar 23rd, 2009
Originally Posted by blocker View Post
Ive modified the code a little bit but still it returns $totalunvepayment to zero:Whats wrong with this code.?

  1. $query_recunvesummary="SELECT * FROM paymentsummary WHERE username='".mysql_real_escape_string($_SESSION['MM_Username'])."' AND paymentmode='".mysql_real_escape_string($modeofpay)."' AND foryear='".mysql_real_escape_string($_SESSION['MM_yearlynow'])."' AND initialstatus='".mysql_real_escape_string($_SESSION['MM_paymentstatus'])."'";
  2.  
  3. $recunvesummary = mysql_query($query_recunvesummary, $enamysqldb) or die(mysql_error());
  4. $row_recunvesummary = mysql_fetch_assoc($recunvesummary);
  5. $totalRows_recunvesummary = mysql_num_rows($recunvesummary);
  6.  
  7. $totalunvepayment=0;
  8. while($row = mysql_fetch_array($recunvesummary)){
  9. $totalunvepayment = $totalunvepayment + $row['amountpaid'];
  10. }
  11. $_SESSION['MM_totalunvepayment']=$totalunvepayment;

When i echo the sql statement its echos (Resource ID #9) what does this mean.?

Pls help.

God bless on this mother earth.
Are you missing the connect statement, like:
  1. $link = mysql_connect('localhost', 'mysql_user', 'mysql_password');

And why are you fetching data into an associative array via mysql_fetch_assoc() and then using mysql_fetch_array()? I think you can only fetch the data once....

Here's what I do with great success:
  1.  
  2. // function do_sql executes the specified statements, handles error,
  3. // and returns the resource by reference.
  4.  
  5. function do_sql($db, &$resource, $err_msg, $qstring) {
  6. //print "<p>$qstring</p>\n";
  7. $resource = mysql_query($qstring, $db)
  8. or die ($err_msg.": ".mysql_error());
  9. }
  10.  
  11. // Database preparation
  12. $datasetobj = mysql_connect("localhost", "username", "password")
  13. or die ("Unable to connect to database");
  14. mysql_select_db("db_name", $dataestobj)
  15. or die ("Unable to select database");
  16.  
  17. // Query operation
  18. do_sql($datasetobj, $result, "Couldn't fetch data",
  19. "SELECT * FROM my_table
  20. WHERE my_column='5'");
  21. $my_row_count = mysql_num_rows($result);
  22. //print("<pre>Rows returned: $my_row_count</pre>\n");
  23.  
  24. // Result handling
  25. if ($my_row_count>0)
  26. {
  27. while ($my_row = mysql_fetch_array($result))
  28. {
  29. //print("<pre>"); print_r($my_row); print("</pre>\n");
  30. .
  31. .
  32. .
  33. }
  34. }
You can uncomment the print statement in do_sql() to see the query that's being processed. And you can uncomment the prints in the fetch loop to see the data that's returned. These debug prints may show up in weird places, but you'll at least be able to see what's going on. And you can comment them out or delete them for 'production'.

Don't ask how many months it took me to get php/mysql to work reliably. And don't ask how many years it took before I realized I should write do_sql(), and to let PHP put the result into an associate array (with both numeric indices and column-name indices).

With that little bit of code above, I can whip up mysql-based web pages lickety-split. Each query gets a unique error message so I know where the failure is.
Last edited by Fest3er; Mar 23rd, 2009 at 10:20 pm.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 125
Reputation: blocker is an unknown quantity at this point 
Solved Threads: 0
blocker's Avatar
blocker blocker is offline Offline
Junior Poster

Re: mysql query error(need help)

 
0
  #6
Mar 24th, 2009
Originally Posted by Fest3er View Post
Are you missing the connect statement, like:
  1. $link = mysql_connect('localhost', 'mysql_user', 'mysql_password');

And why are you fetching data into an associative array via mysql_fetch_assoc() and then using mysql_fetch_array()? I think you can only fetch the data once....

Here's what I do with great success:
  1.  
  2. // function do_sql executes the specified statements, handles error,
  3. // and returns the resource by reference.
  4.  
  5. function do_sql($db, &$resource, $err_msg, $qstring) {
  6. //print "<p>$qstring</p>\n";
  7. $resource = mysql_query($qstring, $db)
  8. or die ($err_msg.": ".mysql_error());
  9. }
  10.  
  11. // Database preparation
  12. $datasetobj = mysql_connect("localhost", "username", "password")
  13. or die ("Unable to connect to database");
  14. mysql_select_db("db_name", $dataestobj)
  15. or die ("Unable to select database");
  16.  
  17. // Query operation
  18. do_sql($datasetobj, $result, "Couldn't fetch data",
  19. "SELECT * FROM my_table
  20. WHERE my_column='5'");
  21. $my_row_count = mysql_num_rows($result);
  22. //print("<pre>Rows returned: $my_row_count</pre>\n");
  23.  
  24. // Result handling
  25. if ($my_row_count>0)
  26. {
  27. while ($my_row = mysql_fetch_array($result))
  28. {
  29. //print("<pre>"); print_r($my_row); print("</pre>\n");
  30. .
  31. .
  32. .
  33. }
  34. }
You can uncomment the print statement in do_sql() to see the query that's being processed. And you can uncomment the prints in the fetch loop to see the data that's returned. These debug prints may show up in weird places, but you'll at least be able to see what's going on. And you can comment them out or delete them for 'production'.

Don't ask how many months it took me to get php/mysql to work reliably. And don't ask how many years it took before I realized I should write do_sql(), and to let PHP put the result into an associate array (with both numeric indices and column-name indices).

With that little bit of code above, I can whip up mysql-based web pages lickety-split. Each query gets a unique error message so I know where the failure is.
thank you for this reply. Ive appreciate the function defination and the function call. But i cant just imagine why the while loop doesnt execute the loop even if there is va value:

  1. while($row = mysql_fetch_array($recunvesummary)){
  2. $totalunvepayment = $totalunvepayment + $row['amountpaid'];
  3. }

I cant just understand whats wrong with this.! If someone can help find the error, Im so thank you to you and to everyone.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 165
Reputation: Fest3er is an unknown quantity at this point 
Solved Threads: 18
Fest3er Fest3er is offline Offline
Junior Poster

Re: mysql query error(need help)

 
1
  #7
Mar 24th, 2009
Originally Posted by blocker View Post
  1. .
  2. .
  3. .
  4. $row_recunvesummary = mysql_fetch_assoc($recunvesummary);
  5. $totalRows_recunvesummary = mysql_num_rows($recunvesummary);
  6.  
  7. $totalunvepayment=0;
  8. while($row = mysql_fetch_array($recunvesummary)){
  9. $totalunvepayment = $totalunvepayment + $row['amountpaid'];
  10. }
  11. .
  12. .
  13. .
i dont know if theres something wrong on the query because the debugger does not detect an error.But it retuns $totalunvepayment to zero(0) even if there is a value in the mysqldatabase.!

Whats wrong with this.

Pls help
God bless on this mother Earth.
Let me try again. If there is only one matching row in the database, the mysql_fetch_assoc() function grabs it, leaving nothing for your while loop to do. If there is more than one matching row, your sum will not include the first row.

Print out the actual query string you are sending to mysql, so you can see that that string is actually correct.

Print out the result of each fetch so you know what is being fetched from the database, if anything.

Other than your seemingly extraneous mysql_fetch_assoc() call, you code looks like it should work.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 125
Reputation: blocker is an unknown quantity at this point 
Solved Threads: 0
blocker's Avatar
blocker blocker is offline Offline
Junior Poster

Re: mysql query error(need help)

 
0
  #8
Mar 24th, 2009
Originally Posted by Fest3er View Post
Let me try again. If there is only one matching row in the database, the mysql_fetch_assoc() function grabs it, leaving nothing for your while loop to do. If there is more than one matching row, your sum will not include the first row.

Print out the actual query string you are sending to mysql, so you can see that that string is actually correct.

Print out the result of each fetch so you know what is being fetched from the database, if anything.

Other than your seemingly extraneous mysql_fetch_assoc() call, you code looks like it should work.
yes ive echo all the data on my sql.but when there is two matching row only one row will be display. But if there is only one matching row. it returns to zero. What should i do to this.? Something wrong with the loop.? or can i just use for loop.? how can it be done.?

Pls help.Thank you very many for giving time to this.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 165
Reputation: Fest3er is an unknown quantity at this point 
Solved Threads: 18
Fest3er Fest3er is offline Offline
Junior Poster

Re: mysql query error(need help)

 
0
  #9
Mar 24th, 2009
Originally Posted by blocker View Post
yes ive echo all the data on my sql.but when there is two matching row only one row will be display. But if there is only one matching row. it returns to zero. What should i do to this.? Something wrong with the loop.? or can i just use for loop.? how can it be done.?

Pls help.Thank you very many for giving time to this.
Will it help if I shout?

DELETE THE LINE WHERE YOU CALL MYSQL_FETCH_ASSOC()! IT IS EATING THE FIRST ROW RETURNED. IT IS THE LINE RIGHT ABOVE THE LINE WHERE YOU CALL MYSQL_NUM_ROWS()! DELETE THE MYSQL_FETCH_ASSOC() LINE AND YOUR PROGRAM WILL DO WHAT YOU EXPECT!

You now have the answer three different ways. I can't do any more!
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 125
Reputation: blocker is an unknown quantity at this point 
Solved Threads: 0
blocker's Avatar
blocker blocker is offline Offline
Junior Poster

Re: mysql query error(need help)

 
0
  #10
Mar 24th, 2009
Originally Posted by Fest3er View Post
Will it help if I shout?

DELETE THE LINE WHERE YOU CALL MYSQL_FETCH_ASSOC()! IT IS EATING THE FIRST ROW RETURNED. IT IS THE LINE RIGHT ABOVE THE LINE WHERE YOU CALL MYSQL_NUM_ROWS()! DELETE THE MYSQL_FETCH_ASSOC() LINE AND YOUR PROGRAM WILL DO WHAT YOU EXPECT!

You now have the answer three different ways. I can't do any more!
thank youoooooooooooooooooooooooooooo.....very many much.............................Ive seen it now.. I understand what you mean just a minute. If i own a company i will hire you sir.hehe Thank you very much and to all who supported my thread...thanks also to this forum. This problem was solved.
Last edited by blocker; Mar 24th, 2009 at 2:30 am.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC