| | |
mysql query error(need help)
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
php Syntax (Toggle Plain Text)
$_SESSION['MM_paymentstatus']="to be verified"; $modeofpay="Credit Card"; mysql_select_db($database_enamysqldb, $enamysqldb); $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")); $recunvesummary = mysql_query($query_recunvesummary, $enamysqldb) or die(mysql_error()); $row_recunvesummary = mysql_fetch_assoc($recunvesummary); $totalRows_recunvesummary = mysql_num_rows($recunvesummary); $totalunvepayment=0; while($row = mysql_fetch_array($recunvesummary)){ $totalunvepayment = $totalunvepayment + $row['amountpaid']; } $_SESSION['MM_totalunvepayment']=$totalunvepayment;
Whats wrong with this.
Pls help
God bless on this mother Earth.
Try and simplify the mysql query with the following on line 4:
And also make sure the variables/arrays have valid values.
php Syntax (Toggle Plain Text)
$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'])."'");
Try not to bump 10 year old threads as it can be really annoying.
http://syntax.cwarn23.net/
My favourite PC. - MacGyver Fan
http://syntax.cwarn23.net/
Smilies: ^_* +_+ v_v -_- *~*` My favourite PC. - MacGyver Fan
•
•
•
•
Try and simplify the mysql query with the following on line 4:
And also make sure the variables/arrays have valid values.php Syntax (Toggle Plain Text)
$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'])."'");
php Syntax (Toggle Plain Text)
$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'])."'"; $recunvesummary = mysql_query($query_recunvesummary, $enamysqldb) or die(mysql_error()); $row_recunvesummary = mysql_fetch_assoc($recunvesummary); $totalRows_recunvesummary = mysql_num_rows($recunvesummary); $totalunvepayment=0; while($row = mysql_fetch_array($recunvesummary)){ $totalunvepayment = $totalunvepayment + $row['amountpaid']; } $_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.
•
•
Join Date: Aug 2007
Posts: 165
Reputation:
Solved Threads: 18
•
•
•
•
Ive modified the code a little bit but still it returns $totalunvepayment to zero:Whats wrong with this code.?
php Syntax (Toggle Plain Text)
$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'])."'"; $recunvesummary = mysql_query($query_recunvesummary, $enamysqldb) or die(mysql_error()); $row_recunvesummary = mysql_fetch_assoc($recunvesummary); $totalRows_recunvesummary = mysql_num_rows($recunvesummary); $totalunvepayment=0; while($row = mysql_fetch_array($recunvesummary)){ $totalunvepayment = $totalunvepayment + $row['amountpaid']; } $_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.
PHP Syntax (Toggle Plain Text)
$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)
// function do_sql executes the specified statements, handles error, // and returns the resource by reference. function do_sql($db, &$resource, $err_msg, $qstring) { //print "<p>$qstring</p>\n"; $resource = mysql_query($qstring, $db) or die ($err_msg.": ".mysql_error()); } // Database preparation $datasetobj = mysql_connect("localhost", "username", "password") or die ("Unable to connect to database"); mysql_select_db("db_name", $dataestobj) or die ("Unable to select database"); // Query operation do_sql($datasetobj, $result, "Couldn't fetch data", "SELECT * FROM my_table WHERE my_column='5'"); $my_row_count = mysql_num_rows($result); //print("<pre>Rows returned: $my_row_count</pre>\n"); // Result handling if ($my_row_count>0) { while ($my_row = mysql_fetch_array($result)) { //print("<pre>"); print_r($my_row); print("</pre>\n"); . . . } }
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.
•
•
•
•
Are you missing the connect statement, like:PHP Syntax (Toggle Plain Text)
$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:
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'.php Syntax (Toggle Plain Text)
// function do_sql executes the specified statements, handles error, // and returns the resource by reference. function do_sql($db, &$resource, $err_msg, $qstring) { //print "<p>$qstring</p>\n"; $resource = mysql_query($qstring, $db) or die ($err_msg.": ".mysql_error()); } // Database preparation $datasetobj = mysql_connect("localhost", "username", "password") or die ("Unable to connect to database"); mysql_select_db("db_name", $dataestobj) or die ("Unable to select database"); // Query operation do_sql($datasetobj, $result, "Couldn't fetch data", "SELECT * FROM my_table WHERE my_column='5'"); $my_row_count = mysql_num_rows($result); //print("<pre>Rows returned: $my_row_count</pre>\n"); // Result handling if ($my_row_count>0) { while ($my_row = mysql_fetch_array($result)) { //print("<pre>"); print_r($my_row); print("</pre>\n"); . . . } }
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.
php Syntax (Toggle Plain Text)
while($row = mysql_fetch_array($recunvesummary)){ $totalunvepayment = $totalunvepayment + $row['amountpaid']; }
I cant just understand whats wrong with this.! If someone can help find the error, Im so thank you to you and to everyone.
•
•
Join Date: Aug 2007
Posts: 165
Reputation:
Solved Threads: 18
•
•
•
•
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.!php Syntax (Toggle Plain Text)
. . . $row_recunvesummary = mysql_fetch_assoc($recunvesummary); $totalRows_recunvesummary = mysql_num_rows($recunvesummary); $totalunvepayment=0; while($row = mysql_fetch_array($recunvesummary)){ $totalunvepayment = $totalunvepayment + $row['amountpaid']; } . . .
Whats wrong with this.
Pls help
God bless on this mother Earth.
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.
•
•
•
•
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.
Pls help.Thank you very many for giving time to this.
•
•
Join Date: Aug 2007
Posts: 165
Reputation:
Solved Threads: 18
•
•
•
•
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.
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!
•
•
•
•
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!
Last edited by blocker; Mar 24th, 2009 at 2:30 am.
![]() |
Similar Threads
- please help really strange mysql/php error! (PHP)
- MySQL query error (Perl)
- MySQL query- Error (MySQL)
- PHP Variable in mySQL query (PHP)
- Double MySQL Query (PHP)
- There appears to be an error with the database. (MySQL)
- Invision Power Board mysql error when trying to view newly created forums (PHP)
Other Threads in the PHP Forum
- Previous Thread: Plz,help me to create a table!?
- Next Thread: (urgent) help to display error message
| Thread Tools | Search this Thread |
Tag cloud for PHP
.htaccess access ajax apache api array beginner binary broken cakephp checkbox class cms code cron curl database date development directory display download dynamic echo email error file files filter folder form forms function functions gc_maxlifetime google host href htaccess html image include insert integration ip java javascript joomla limit link login loop mail memmory memory menu mlm mod_rewrite multiple mysql navigation oop parse parsing paypal pdf php problem query radio random recursion regex remote script search server sessions sms snippet soap source space sql structure syntax system table thesishelp tutorial update upload url validation validator variable video web xml youtube






