homepage Welcome to WebmasterWorld Guest from 54.196.24.103
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe to WebmasterWorld
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
sql returns nothing
TravelMan




msg:1295497
 8:53 pm on Nov 23, 2004 (gmt 0)

For those who might not be aware, the following takes the $_POST vars from a submitted form and does a db lookup based upon the chosen user variables.


$advquery = "SELECT SUM(pc_count) as amount FROM $tablename WHERE pc_place IN('" . implode("','", $_POST['chosenoption']) . "')";
$advcresult=mysql_query($advquery);
$adv = mysql_fetch_array($advcresult);
echo" $adv[amount] ";

The above returned a result when I echoed $advquery to the browser, and ran the contents through phpmyadmin's sql interpreter. However, the same query when run through the script itself ( as above), outputs nothing to the browser, yet I know for certain it should ( based upon the chosen data at least).

Would anyone know why?

It doesn't use reserved words either.

Heres the table structure, just in case this is the prob.


CREATE TABLE placecount (
pc_count varchar(20) NOT NULL default '',
pc_place varchar(60) NOT NULL default '',
PRIMARY KEY (pc_place),
KEY pc_place (pc_place)
) TYPE=MyISAM;

Thanks for any pointers.

 

Salsa




msg:1295498
 9:37 pm on Nov 23, 2004 (gmt 0)

I've looked at this pretty closely, and it looks okay to me.

Have you tried calling mysql_error() to see what that says?

if(!$advcresult) mysql_error();

Salsa




msg:1295499
 9:46 pm on Nov 23, 2004 (gmt 0)

Ah-Ha!

I think it's the quote implode...

Try:

$imploded_chosenoption = "'".implode("','", $_POST['chosenoption'])."'";

$advquery = "SELECT SUM(pc_count) as amount FROM $tablename WHERE pc_place IN($imploded_chosenoption)";

TravelMan




msg:1295500
 9:48 pm on Nov 23, 2004 (gmt 0)

Will try that first thing in the morning Salsa and let you know how I get on :)

TravelMan




msg:1295501
 7:51 am on Nov 24, 2004 (gmt 0)


if(!$advcresult) {
mysql_error();
echo"No result";
}
if($advcresult) {
echo"$advchrow[amount] here ";
}

:( Oh well, tried your suggestion but no joy.

I get "No result".

This is so weird, coz like I said, when I cut and past the echoed $advquery from the submitted page directly via phpmyadmin, the very same query works.

I think I might need to rethink my plan.

coopster




msg:1295502
 12:13 pm on Nov 24, 2004 (gmt 0)

You got "No result", but what error did you get from MySQL?

TravelMan




msg:1295503
 12:24 pm on Nov 24, 2004 (gmt 0)

No error at all.

Could be a php setting in the ini file maybe?

Salsa




msg:1295504
 12:31 pm on Nov 24, 2004 (gmt 0)

Yes, I tested your original $advquery construction, and it seemed to echo out a fine query.

One thing that could be making a difference between venues is, where does $_POST['chosenoption'] come from? Have you tried a,

print_r($_POST);

to see if the array is really there.

Also, I was moving hastily when I gave you the mysql_error() line. You need to echo it or put it in die(), etc.

if(!$advcresult) echo mysql_error(); //or 
if(!$advcresult) die(mysql_error());

I wish you well.

[edited]
another thought. If you haven't included method="post" in your form tag, the array may be in $_GET.
...and just echo out $advcresult to see that in isn't a typo when setting $tablename, or something.
Let me know what you find out.
[/edit]

TravelMan




msg:1295505
 1:13 pm on Nov 24, 2004 (gmt 0)

> Try print_r($_POST);

Yes I'd checked to see if the array existed, and checked the form action of the sending page was set to POST.
> if(!$advcresult) die(mysql_error());

This unlocked it. No connectin to Database (hangs head in shame) How did I miss this? Sigh...

I'd failed to include the require "../dbconn.php"; line in an if condition of the script.

Feel like a right plonker now!

Thanks very much for all your help, :) its very much appreciated.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved