jumpenjuhosaphat Posted December 27, 2006 Share Posted December 27, 2006 I wrote this code and included it and it worked fine. When I decided to make it a function, it stopped working. The $password variable comes up empty, where the $user variable isn't. They are both in the same file, which is included in the script that calls this function.Also, when I insert the password, I get another error, stating that the argument passed to mysql_fetch_array isn't valid. I didn't change anything in the code when I moved it from the included file to the functions, I even double and triple checked. Is there a trick to using queries and certain variables within a function?[code]function category_list($childid) { $select=='<select name="category"><option value=""></option>'; $con=mysql_connect("localhost",$user,"$password") or die("category_list function connect error ".mysql_error()); mysql_select_db($database, $con); $query=sprintf("SELECT * FROM category WHERE childid=%d ORDER BY catname",$childid); $result=mysql_query($query); while($row=mysql_fetch_array($result)) { $select.=' <option value="'.$row['catname'].'">'.$row['catname'].'</option>'; } $select.='</select>'; return $select; }[/code] Link to comment https://forums.phpfreaks.com/topic/31953-script-works-fine-but-not-in-functions/ Share on other sites More sharing options...
kenrbnsn Posted December 27, 2006 Share Posted December 27, 2006 Variables defined outside of a function are not available inside a function unless they are either passed via the function arguments or declared global within the function.Ken Link to comment https://forums.phpfreaks.com/topic/31953-script-works-fine-but-not-in-functions/#findComment-148260 Share on other sites More sharing options...
jumpenjuhosaphat Posted December 27, 2006 Author Share Posted December 27, 2006 That's sort of what I figured. But my question then would be, how does the $user variable get passed, but yet the $password variable doesn't?Plus, did you have any thoughts as to why the query would react differently in the function than it does out side of the function? Link to comment https://forums.phpfreaks.com/topic/31953-script-works-fine-but-not-in-functions/#findComment-148262 Share on other sites More sharing options...
craygo Posted December 27, 2006 Share Posted December 27, 2006 I use this to create my dropdowns you are welcome to use modify[code]<?phpfunction create_dropdown($sname, $table, $id_field, $namefield, $where){$select = "<select name=$sname>";$select .= "<option value="">--Please Select--</option>";$sql = "SELECT $id_field, $namefield FROM $table WHERE $where ORDER BY $namefield ASC";$res = mysql_query($sql) or die (mysql_error()); while($r = mysql_fetch_assoc($res)){ $select .= "<option value=".$r[$id_field].">".$r[$namefield]."</option>"; }$select .= "</select>";return $select;}echo create_dropdown("selectname", "tablename", "id_field", "namefield", "field = 'something'")?>[/code]Add what you like but this will let you select data from the table with 1 where clause.Ray Link to comment https://forums.phpfreaks.com/topic/31953-script-works-fine-but-not-in-functions/#findComment-148265 Share on other sites More sharing options...
kenrbnsn Posted December 27, 2006 Share Posted December 27, 2006 [quote author=jumpenjuhosaphat link=topic=120030.msg492066#msg492066 date=1167229172]That's sort of what I figured. But my question then would be, how does the $user variable get passed, but yet the $password variable doesn't?Plus, did you have any thoughts as to why the query would react differently in the function than it does out side of the function?[/quote]Since you didn't show us the script that calls this routine, it's hard to guess the answers to your questions.Post the calling script.Ken Link to comment https://forums.phpfreaks.com/topic/31953-script-works-fine-but-not-in-functions/#findComment-148271 Share on other sites More sharing options...
Hypnos Posted December 27, 2006 Share Posted December 27, 2006 [quote author=jumpenjuhosaphat link=topic=120030.msg492066#msg492066 date=1167229172]That's sort of what I figured. But my question then would be, how does the $user variable get passed, but yet the $password variable doesn't?Plus, did you have any thoughts as to why the query would react differently in the function than it does out side of the function?[/quote]For the function to work correctly as you have it, you need to use something like this in the function:[code=php:0]$user = $GLOBALS['user'];$password = $GLOBALS['password'];[/code]MySQL doesn't know where your query is in your code, so it shouldn't matter if it's in a function or not. If it does, there's a problem with your code, and you should echo your querys to see what the difference is. Link to comment https://forums.phpfreaks.com/topic/31953-script-works-fine-but-not-in-functions/#findComment-148422 Share on other sites More sharing options...
alpine Posted December 27, 2006 Share Posted December 27, 2006 A simple example, test it - then remove the global part and see what happends:[code]<?php$hello = "Hello";$message = "function calling!";function Greetings($name){ global $hello,$message; echo $hello." ".$name." ,".$message;}Greetings("jumpenjuhosaphat");?>[/code] Link to comment https://forums.phpfreaks.com/topic/31953-script-works-fine-but-not-in-functions/#findComment-148432 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.