473,320 Members | 1,794 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Trying to use a array to query a database

I am trying to use an array to query my db.
The following code displays one provinces data.
Expand|Select|Wrap|Line Numbers
  1.  
  2. $sql = 'SELECT * FROM `person` WHERE `Province` = CONVERT(_utf8 \'ON \'  USING latin1) COLLATE latin1_swedish_ci AND `IsJudge` =1' . ' ORDER BY  LastName, Firstname';
  3. $result = $link->query($sql)or die( $link->error );
  4.  
If I make an array like so;
Expand|Select|Wrap|Line Numbers
  1. $provinces = array("BC","AB","SK","MB","ON","QC","NB","NS");
  2.  
  3.  foreach( $provinces as $province )
  4.  {
  5. # then change query like so
  6. $sql = 'SELECT * FROM `person` WHERE `Province` = CONVERT(_utf8 \' "$province" \'  USING latin1) COLLATE latin1_swedish_ci AND `IsJudge` =1' . ' ORDER BY  LastName, Firstname';
  7. $result = $link->query($sql)or die( $link->error );
  8. }
  9.  
I get no data back. Any ideas?

I got the answer ... changed code to;
Expand|Select|Wrap|Line Numbers
  1.  $sql = "SELECT * FROM person WHERE Province='" . $province . "' AND IsJudge=1 ORDER BY Lastname, FirstName";
  2.  
and BobsURuncle, works just fine.

TNX
Bob
Oct 24 '13 #1
1 1164
Luuk
1,047 Expert 1GB
This wil eliminate the need for 'foreach()'

Expand|Select|Wrap|Line Numbers
  1. $p = "";
  2. $provinces = array("BC","AB","SK","MB","ON","QC","NB","NS");
  3. while ( count($provinces)>0 ){ $p.=array_shift($provinces).","; }
  4. $p = substr($p,0,strlen($p)-1);
  5. $sql = 'SELECT * FROM `person` WHERE `Province` IN ('.$p.') AND `IsJudge` =1' . ' ORDER BY  LastName, Firstname';
  6.  
Oct 24 '13 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: madsgormlarsen | last post by:
Hi I need to test a field(colum) in a SQL database for for NULL values, and have done so in this way. $query = "SELECT j FROM Andersen"; $result = mysql_query($query, $link_id); $query_data...
0
by: Dan Baker | last post by:
I'm trying to write a stand-alone application which will query the contact database used in Outlook (or maybe Exchange Server). I have no idea where to start looking. Any advice? Dan Baker
0
by: JC Voon | last post by:
Hi All: I'm new in Threading and Web Services, can someone please verify my code, i'm not sure whether this is the correct way, althought it is partially work, but some time it will raise...
1
by: Learner | last post by:
Hi there, I have installed Sql server 2005 developer on my machine which already has a Sql server 2000 installed on. Now i am trying to query the Sqlserver 2005 data(Ex: from Person.Address...
2
by: Allen Anderson | last post by:
Hi, I'm trying to design contact (names and addresses) tables in an Access database. Some of the contacts represent vendors, some are board members of the organization, some are donors, some...
25
by: gwb | last post by:
I am new to database queries. I am using access, trying to query my database where the field memberdate is less than 31 days old. I thought maybe the DateDiff function would work but I'm not sure how...
7
by: Francis S | last post by:
$record= array( "product"=>array( array("name"=>"apple","price"=>30.5), array("name"=>"orange","price"=>20) ) ); echo array_read($record, "") //out 30.5 print_r (array_read($record, ""))...
4
shelzmike
by: shelzmike | last post by:
To start with, let me lay out the situation. I am a student at the very end of my last assignment of a beginner - intermediate VB class (we have been using VB 2005 Express + MSSQL Express 2005)....
5
by: DavidPr | last post by:
I'm trying to do this: $query = "SELECT * FROM response WHERE recipe_id='$file' AND allow='1'"; $result = mysql_query($query) or trigger_error("Query: $query\n<br>MySQL Error: " . mysql_error());...
5
rahulephp
by: rahulephp | last post by:
Hi there, can you please help me? i have data in array and i want to insert it in a single table. structure of table and array to be store is shown below: Please let me know how to store...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.