473,387 Members | 1,812 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,387 software developers and data experts.

Select statement with arrays

196 100+
Ok here is what im trying to achieve, i first retrieve from one part of my database names and email address, etc which i store in an array i.e.

Expand|Select|Wrap|Line Numbers
  1. while($row= mysql_fetch_array($result)){
  2. $name[$i] = $row['name']
  3. $suburb[$i] = $row['suburb']
  4. }
this seems to work fine but when i try to use this information in a select statement for another part of my database it doesn't seem to like it, heres a example of what im tring to do - (note i renamed a couple of things),

Also for some reason bytes does not want to display the code so im gonna try re-writing it here (line 15)-

$sql = "SELECT var1, var2, var3 FROM table_name WHERE suburb="$suburb[$counter]" ;


Expand|Select|Wrap|Line Numbers
  1. for ($counter=0; $counter < $numtotal; $counter++){
  2. $con = mysql_connect("localhost","user_name","pass");
  3.         if (!$con)
  4.           {
  5.  
  6.               die('Could not connect: ' . mysql_error());
  7.           }
  8.  
  9.         else {
  10.             echo "We have a connection" . "<br />";
  11.             mysql_select_db("name_database", $con);
  12.  
  13.  
  14. //this is where i seem to be getting the errors from            
  15. $sql = "SELECT var1, var2, var3 FROM table_name WHERE suburb="$suburb['$counter']" ;
  16.  
  17.  
  18.         $result = mysql_query($sql,$con);
  19.         if (!$result)
  20.           {
  21.  
  22.               die('Error: ' . mysql_error());
  23.           }
  24.  
  25. $NUMlistings = 0;
  26.         while($row = mysql_fetch_array($result))
  27.           {
  28.  
  29.          $var1[$NUMlistings] = $row['var1'];
  30.          $var2[$NUMlistings] = $row["var2"];
  31.         $var3[$NUMlistings] = $row["var3"];
  32.          $NUMlistings++;
  33.         }
  34.  
  35.         mysql_close($con);
  36.  
  37.         }
So if anyone can give us a hint or perhaps point me in the right direction, thanks for anyhelp :)
Jun 27 '09 #1
5 2707
Atli
5,058 Expert 4TB
Hi.

In your first code example, where is the $i variable coming from?
You are using a while loop, which doesn't increment anything automatically, and I don't see any code that would do it manually.

As it is, the $i variable is either set, and the while loop keeps overriding the same index of those arrays, or it isn't set and PHP evaluates it as 0 and overrides that index each iteration.
Jun 27 '09 #2
chazzy69
196 100+
Sorry bout that, i must of missed it when copying it should look like

Expand|Select|Wrap|Line Numbers
  1. $i = 0;
  2. while($row= mysql_fetch_array($result)){
  3.  $name[$i] = $row['name']
  4.  $suburb[$i] = $row['suburb']
  5.  $i++;  
  6. }
Anyway the line im having trouble with is -

$sql = "SELECT var1, var2, var3 FROM table_name WHERE suburb="$suburb[$counter]" ;

For some reason still can't put code tags around this without the tags deleting the line...

Anyway specifically can i use - WHERE suburb="$suburb[$counter]" ;
inside a loop.

Hope this clarifies my previous statement a bit
Jun 28 '09 #3
chazzy69
196 100+
I believe i found the problem but im not entirely sure how to over come it.

First this works-

Expand|Select|Wrap|Line Numbers
  1. $state[0] = "nsw";
  2. $state[1] = "qld";
  3. $pcode[0] = 2000;
  4. $pcode[1] = 3000;
  5. $suburb[0]= "sydney";
  6. $suburb[1]= "surfers paridise";
  7.  
  8. for ($i=0; $i < 2; $i++){
  9. $con = mysql_connect("localhost","user_name","pass");
  10.         if (!$con)
  11.           {
  12.  
  13.               die('Could not connect: ' . mysql_error());
  14.           }
  15.  
  16.         else {
  17.             echo "We have a connection" . "<br />";
  18.             mysql_select_db("name_table", $con);
  19.  
  20.  
  21.  
  22.             $sql = "SELECT var1, var2, var3 FROM latest_listings WHERE state='$state[$i]' AND suburb='$suburb[$i]'";
  23. .
  24. .
  25. .
  26. .

but this wont work -

Expand|Select|Wrap|Line Numbers
  1. $state[0] = "nsw";
  2. $state[1] = "qld";
  3. $pcode[0] = 2000;
  4. $pcode[1] = 3000;
  5. $suburb[0]= "sydney";
  6. $suburb[1]= "surfers paridise";
  7.  
  8. for ($i=0; $i < 2; $i++){
  9. $con = mysql_connect("localhost","user_name","pass");
  10.         if (!$con)
  11.           {
  12.  
  13.               die('Could not connect: ' . mysql_error());
  14.           }
  15.  
  16.         else {
  17.             echo "We have a connection" . "<br />";
  18.             mysql_select_db("name_table", $con);
  19.  
  20.  
  21.  
  22.             $sql = "SELECT var1, var2, var3 FROM latest_listings WHERE state='$state[$i]' AND suburb='$suburb[$i]' AND pcode=pcode[$i]";
  23. .
  24. .
  25. .
  26. .
i know it wont work because of this line -

Expand|Select|Wrap|Line Numbers
  1. AND pcode=pcode[$i]
but pcode[$i] = to a integer, so i cant place quotation marks around like the other varibles. So any hints on how to overcome this would be greatly appreciated thanks,
Jun 28 '09 #4
Atli
5,058 Expert 4TB
@chazzy69
The trouble here is that you are messing up the quotes.
If you were to view this in an editor that can highlight PHP, it should become very obvious, as the code following that line would be highlighted as a string.

When you use double quotes, there is no need to close the quotes to insert a variable, you can just put it right in there and PHP will parse it. To make absolutely sure there is not problem when you do this, it's best to enclose the variable in brackets. Like:
Expand|Select|Wrap|Line Numbers
  1. $sql = "SELECT var1, var2, var3 
  2.         FROM table_name 
  3.         WHERE suburb={$suburb[$counter]}";
And if "suburb" is a string that needs to be quoted in the SQL statement, you need to either use single-quotes (as putting a double quote would close the string, rather than add the quote to it) or escaped double quotes.
Expand|Select|Wrap|Line Numbers
  1. $sql = "... WHERE suburb='{$suburb[$counter]}'";
  2. $sql = "... WHERE suburb=\"{$suburb[$counter]}\"";
@chazzy69
This, in no way related to the previous error, is because the second "pcode" you are trying to fetch an element from is not designated as a variable.

All variables start with a dollar-sign ($). That is how PHP recognises variables. If you forget it, PHP will assume it is either a constant, or a string. (Always the latter if it happens to be inside a string.)
Jun 28 '09 #5
chazzy69
196 100+
Thanks heaps, i think that sorts out the problems i was having :)
Jun 29 '09 #6

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

Similar topics

7
by: Guy Hocking | last post by:
Hi there, I have a problem in my ASP/SQL Server application i am developing, i hope you guys can help. I have a ASP form with list boxes populated by SQL tables. When a user selects a value...
12
by: Kevin Lyons | last post by:
Hello, I am trying to get my select options (courses) passed correctly from the following URL: http://www.dslextreme.com/users/kevinlyons/selectBoxes.html I am having difficulty getting the...
6
by: Ian Davies | last post by:
Hello all I have the following PHP script to place records from my MySql db into a table. However I now wish users to beable to select the multiple records from the table and on clicking a button...
1
by: Grant McLean | last post by:
Hi First a simple question ... I have a table "access_log" that has foreign keys "app_id" and "app_user_id" that reference the "application_type" and "app_user" tables. When I insert into...
9
by: Stuart | last post by:
I am trying to execute a Socket.Select() statement on an arraylist of sockets. The problem is that I can only go up to 64 sockets at a time. I know that I have to manipulate the FD_SetSize to...
35
by: Thierry Loiseau | last post by:
Hello all, and Happy end year 2005 ! Well, I would like to obtain a list of all JavaScript var statement, With "for...in" perharps ? That is bellow my recent test here, but the problem is...
2
by: willwmagic | last post by:
Hi everyone, I feel as this is a very simple question, but I cannot find any information pertaining to it. I want to be able to update a set of records that were returned through a select...
12
by: LayneMitch via WebmasterKB.com | last post by:
Hello everyone. I'm currently learning Javascript and doing a few exercises. One problem I'm working on takes an array of names from an xml file using Ajax and writes it to...
2
by: vikramkumar | last post by:
I have two arrays,i want to take out common elements from those two arrays in a single statement, for example ; two arrays are my @a = (1, 3 ,4 ,7 ); my @b = (2 , 3 ,6 , 7); i want to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.