Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Variable for array element in SELECT query help
Old 04-11-2009, 06:11 PM Variable for array element in SELECT query help
Average Talker

Posts: 17
Name: Gary
Trades: 0
I'm just beginning in PHP. I have a problem and hope someone can help. In the code below, if the SELECT query does not contain && assn ='$value', then everything works. I am trying to run the query for every $value in the array and each time determine the number of $rows for each query result. $rows is added to $total each time to keep a running count of number of rows.

When I run the code below, the ECHO runs for as many times as the FOREACH runs, and $total always is zero. I suspect I have an error in the SELECT statement. It may be the syntax for including a variable that contains an element from an array.

I've checked to make sure the array is valid by echoing and printing the $key and $value and the data is correctly displayed. The database has records that meet the query criteria so the query should produce results. The query does work as well as the rest of the code if the query is run without && assn ='$value'.

Thanks in advance.


Code:
<?php

$dbc = mysql_connect('xxxx', 'yyyy','xxxx')
or die('Error connecting to MySQL server.');

mysql_select_db('mydb');

// Number of closed condo sales for 7-12 months prior to effective date

$total = 0;

foreach ($_POST['condo'] as $key => $value){

$results = mysql_query("select * from compsin where cldate > '$effdate365' && cldate < '$effdate182' && assn ='$value' ");

$rows = mysql_num_rows($results);

if ($rows < 1)

{

echo "No Matching records.<br>";

}

else

{

$total = ($rows + $total);

}
}

?>
gdaniels is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 04-11-2009, 07:27 PM Re: Variable for array element in SELECT query help
Junior Talker

Posts: 3
Name: Larry Poppins
Trades: 0
Code:
select * from compsin where cldate > '$effdate365' && cldate < '$effdate182' && assn ='$value'
"AND" rather than "&&" will make a correct SQL statement:

Code:
SELECT * FROM compsin WHERE cldate > '$effdate365' AND cldate < '$effdate182' AND assn = '$value'
ProjectPoppins is offline
Reply With Quote
View Public Profile
 
Old 04-11-2009, 08:30 PM Re: Variable for array element in SELECT query help
Average Talker

Posts: 17
Name: Gary
Trades: 0
Thanks for the suggestion, however it made no difference. The same problem exits. If I do "print $value;" I get all the elements in the array (which consists of names of condo associations like The Ponds, Willow Woods, Coachlight, etc.) I am wondering if there may be any special characters that need to be stripped from the array elements before $value is placed in the query? Any suggestions?
gdaniels is offline
Reply With Quote
View Public Profile
 
Old 04-11-2009, 09:04 PM Re: Variable for array element in SELECT query help
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
Can you post the code for the form that is submitting to this page?

Also, you shouldn't pass values directly from POST to your database. Validate your data.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 04-11-2009, 09:11 PM Re: Variable for array element in SELECT query help
Novice Talker

Posts: 5
Trades: 0
Try this query:

Code:
$results = mysql_query("SELECT * FROM `compsin` WHERE (cldate > '$effdate365') AND (cldate <  '$effdate182') AND (assn = '$value')");
Sarvar is offline
Reply With Quote
View Public Profile
 
Old 04-11-2009, 10:25 PM Re: Variable for array element in SELECT query help - Partial Solution
Average Talker

Posts: 17
Name: Gary
Trades: 0
OK. I found the problem, but I don't know why it occurred. It seems the data in $value was not matching the data in the "assn" field, so no results and no rows were returned. I used a simple test code. Here's a snippet:

Instead of:

Code:
 ...WHERE assn = '$value' ");
I used:

Code:
...WHERE assn LIKE '$value%' ");
What I don't understand is why the data in $value is not identical to the data in the "assn" field, because it should be the same. My (faulty) reasoning is that the data in the "assn" field populates the options of a multiple select drop down box. When options are selected, they go into an array, from the array they go into $value and used in the FOREACH loop in the code I first provided in this thread. I can go with this workaround, but I'd like to make it cleaner. Anyone know where the error lies? Thanks again for your time.
gdaniels is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Variable for array element in SELECT query help
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.17362 seconds with 12 queries