Welcome to WebmasterWorld Guest from 54.198.183.217

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

sql returns nothing

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

Junior Member

10+ Year Member

joined:Oct 22, 2002
posts:196
votes: 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.

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

Full Member

10+ Year Member

joined:Oct 9, 2003
posts:314
votes: 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();
9:46 pm on Nov 23, 2004 (gmt 0)

Full Member

10+ Year Member

joined:Oct 9, 2003
posts:314
votes: 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)";
9:48 pm on Nov 23, 2004 (gmt 0)

Junior Member

10+ Year Member

joined:Oct 22, 2002
posts:196
votes: 0


Will try that first thing in the morning Salsa and let you know how I get on :)
7:51 am on Nov 24, 2004 (gmt 0)

Junior Member

10+ Year Member

joined:Oct 22, 2002
posts:196
votes: 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.

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

Administrator

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 31, 2003
posts:12541
votes: 1


You got "No result", but what error did you get from MySQL?
12:24 pm on Nov 24, 2004 (gmt 0)

Junior Member

10+ Year Member

joined:Oct 22, 2002
posts:196
votes: 0


No error at all.

Could be a php setting in the ini file maybe?

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

Full Member

10+ Year Member

joined:Oct 9, 2003
posts:314
votes: 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]

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

Junior Member

10+ Year Member

joined:Oct 22, 2002
posts:196
votes: 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.