943,536 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 1008
  • PHP RSS
Mar 23rd, 2009
0

mysql query error(need help)

Expand Post »
php Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 7
Solved Threads: 1
Posting Whiz in Training
blocker is offline Offline
231 posts
since Jan 2009
Mar 23rd, 2009
0

Re: mysql query error(need help)

Try and simplify the mysql query with the following on line 4:
php Syntax (Toggle Plain Text)
  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.
Sponsor
Featured Poster
Reputation Points: 410
Solved Threads: 258
Occupation: Genius
cwarn23 is offline Offline
3,004 posts
since Sep 2007
Mar 23rd, 2009
0

Re: mysql query error(need help)

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.
Reputation Points: 51
Solved Threads: 35
Posting Whiz in Training
Fest3er is offline Offline
238 posts
since Aug 2007
Mar 23rd, 2009
0

Re: mysql query error(need help)

Click to Expand / Collapse  Quote originally posted by cwarn23 ...
Try and simplify the mysql query with the following on line 4:
php Syntax (Toggle Plain Text)
  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.?

php Syntax (Toggle Plain Text)
  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.
Reputation Points: 7
Solved Threads: 1
Posting Whiz in Training
blocker is offline Offline
231 posts
since Jan 2009
Mar 23rd, 2009
0

Re: mysql query error(need help)

Click to Expand / Collapse  Quote originally posted by blocker ...
Ive modified the code a little bit but still it returns $totalunvepayment to zero:Whats wrong with this code.?

php Syntax (Toggle Plain Text)
  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:
PHP Syntax (Toggle Plain Text)
  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:
php Syntax (Toggle Plain Text)
  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.
Reputation Points: 51
Solved Threads: 35
Posting Whiz in Training
Fest3er is offline Offline
238 posts
since Aug 2007
Mar 24th, 2009
0

Re: mysql query error(need help)

Click to Expand / Collapse  Quote originally posted by Fest3er ...
Are you missing the connect statement, like:
PHP Syntax (Toggle Plain Text)
  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:
php Syntax (Toggle Plain Text)
  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:

php Syntax (Toggle Plain Text)
  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.
Reputation Points: 7
Solved Threads: 1
Posting Whiz in Training
blocker is offline Offline
231 posts
since Jan 2009
Mar 24th, 2009
1

Re: mysql query error(need help)

Click to Expand / Collapse  Quote originally posted by blocker ...
php Syntax (Toggle Plain Text)
  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.
Reputation Points: 51
Solved Threads: 35
Posting Whiz in Training
Fest3er is offline Offline
238 posts
since Aug 2007
Mar 24th, 2009
0

Re: mysql query error(need help)

Click to Expand / Collapse  Quote originally posted by Fest3er ...
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.
Reputation Points: 7
Solved Threads: 1
Posting Whiz in Training
blocker is offline Offline
231 posts
since Jan 2009
Mar 24th, 2009
0

Re: mysql query error(need help)

Click to Expand / Collapse  Quote originally posted by blocker ...
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!
Reputation Points: 51
Solved Threads: 35
Posting Whiz in Training
Fest3er is offline Offline
238 posts
since Aug 2007
Mar 24th, 2009
0

Re: mysql query error(need help)

Click to Expand / Collapse  Quote originally posted by Fest3er ...
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.
Reputation Points: 7
Solved Threads: 1
Posting Whiz in Training
blocker is offline Offline
231 posts
since Jan 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Plz,help me to create a table!?
Next Thread in PHP Forum Timeline: (urgent) help to display error message





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC