|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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. |
|
#2
|
|||
|
|||
|
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... |
|
#3
|
|||
|
|||
|
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. |
|
#4
|
|||
|
|||
|
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.... |
|
#5
|
|||
|
|||
|
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? |
|
#6
|
|||
|
|||
|
RE: DB Query SelectBox Function
Yes, using a variable as your array index is defnitely possible.
$name[$i] should be ok. Dave |
|
#7
|
|||
|
|||
|
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++. |
![]() |
| Viewing: Codewalkers Forums > Other Technologies > Database Help > DB Query SelectBox Function |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|