Database Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsOther TechnologiesDatabase Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
  #1  
Old September 15th, 2002, 07:36 AM
justravis justravis is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: San Diego, CA
Posts: 65 justravis User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 21 m 12 sec
Reputation Power: 2
Send a message via AIM to justravis Send a message via Yahoo to justravis
DB Query SelectBox Function

I tried to write a function that generates selectboxes.

I would greatly appreciate any sugestions.

Keep getting these weird errors:
Warning: Supplied argument is not a valid MySQL-Link resource on line 18.

Warning: Supplied argument is not a valid MySQL result resource on line 20.

Code:

5 <?php
6
7 $db = mysql_connect('localhost', 'username', 'password');
8
9 mysql_select_db('reference');
10
11 function selectbox($name, $query, $value_field, $first_field_shown)
12 {
13 //$value_field & $first_field_shown should be numeric values.
14
15 echo "ttt<select name=$name>n";
16
17 $query="SELECT * FROM tablename";
18 $result=mysql_query($query, $db);
19
20 while($name=mysql_fetch_array($result))
21 {
22 echo "tttt<option value=" . $name[$value_field] . ">n";
23 echo "ttttt";
24
25 //Cycles through all the fields in query.
26 for($i=$first_field_shown; $i++; $i==mysql_num_fields($result))
27 {
28 echo " " . $name[$i];
29 }
30
31 echo "n";
32 echo "tttt</option>n";
33 }
34
35 echo "ttt</select>n";
36 }
37
38 ?>
39
40 Select Box That Includes Primary Key:<br>
41 <?php
42 $query="SELECT * FROM tablename";
43
44 selectbox("reference", $query, 0, 0);
45 ?>
46 <br>

THANK YOU.

Reply With Quote
  #2  
Old September 16th, 2002, 10:48 AM
Anonymous Anonymous is offline
Registered User
Codewalkers God 35th Plane (22000 - 22499 posts)
 
Join Date: Apr 2007
Posts: 22,309 Anonymous User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 24
RE: DB Query SelectBox Function

It sounds like the database connection is not being made successfully..try to place:

echo mysql_error();

after the mysql_connect line...


Reply With Quote
  #3  
Old September 16th, 2002, 04:36 PM
justravis justravis is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: San Diego, CA
Posts: 65 justravis User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 21 m 12 sec
Reputation Power: 2
Send a message via AIM to justravis Send a message via Yahoo to justravis
RE: DB Query SelectBox Function

I have thought the same thing. I have compared the db connection portion to numerous other working scripts and they all look the same.

I added that echo mysql_error(). It echos nothing.

Reply With Quote
  #4  
Old September 16th, 2002, 06:40 PM
Matt Matt is offline
Moderator
Codewalkers Specialist (4000 - 4499 posts)
 
Join Date: Apr 2007
Location: Florida
Posts: 4,158 Matt User rank is Private First Class (20 - 50 Reputation Level)Matt User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 h 10 m 20 sec
Reputation Power: 6
RE: DB Query SelectBox Function

You need to place the line:

global $db;

just after

function selectbox($name, $query, $value_field, $first_field_shown)
{
//$value_field & $first_field_shown should be numeric values.

what is currently happening is that $db is out of scope. You can bring it in from the global scope by declaring it as a global inside the function....

Reply With Quote
  #5  
Old September 16th, 2002, 06:56 PM
justravis justravis is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: San Diego, CA
Posts: 65 justravis User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 21 m 12 sec
Reputation Power: 2
Send a message via AIM to justravis Send a message via Yahoo to justravis
RE: DB Query SelectBox Function

Somone in another forum realized my function was getting $db. I fixed it by passing it to the function, but I think I like your solution better. I won't have to pass it every time.

Now there seems to be another problem. The data is not printing out. I was trying to reference the fields numericly. Example: $name[0] Actually, I'm nesting a variable as the number. Example $name[$i]. Is that possible?

Reply With Quote
  #6  
Old September 16th, 2002, 07:27 PM
blazin blazin is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Aurora, CO, USA
Posts: 24 blazin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to blazin Send a message via AIM to blazin
RE: DB Query SelectBox Function

Yes, using a variable as your array index is defnitely possible.

$name[$i] should be ok.

Dave

Reply With Quote
  #7  
Old September 17th, 2002, 07:14 AM
justravis justravis is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: San Diego, CA
Posts: 65 justravis User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 21 m 12 sec
Reputation Power: 2
Send a message via AIM to justravis Send a message via Yahoo to justravis
RE: DB Query SelectBox Function

ALL PROBLEMS SOLVED

Data was not outputing because I had the expressions in the for loop in the wrong order. Expression2 is condition & expression3 should have been i++.

Reply With Quote
Reply

Viewing: Codewalkers ForumsOther TechnologiesDatabase Help > DB Query SelectBox Function


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway