seran128 Posted October 24, 2006 Share Posted October 24, 2006 I have a function nameddblistvalues values in my connection.php file[code]<?function dblistvalues($inTable){ $db_name="db_name";// Database name $db_host="localhost"; $db_user="db_user"; $db_password="db_password"; $table="$inTable"; // Table name $connection=mysql_connect($db_host,$db_user,$db_password) or die("I Couldn't connect"); $db=mysql_select_db($db_name,$connection) or die("I Couldn't select your database");}?>[/code]I then call this function in another file called get_list.phpmy code snippet is[code]<? dblistvalues(my_table); $sql="select * from $table order by ID"; $result=mysql_query($sql,$connection) or die(mysql_error()); while($row=mysql_fetch_array($result)) { ?> [/code] The error I get isWarning: mysql_query(): supplied argument is not a valid MySQL-Link resource if I do the same code in the function but remove the function part it works fine. But I want to reuse the code for seperate tables that is why I put it into a function so I can pass table names to it. Quote Link to comment https://forums.phpfreaks.com/topic/24896-simple-function-problem/ Share on other sites More sharing options...
.josh Posted October 24, 2006 Share Posted October 24, 2006 all the variables inside your function are local. you need to make them global or else return $connection. Quote Link to comment https://forums.phpfreaks.com/topic/24896-simple-function-problem/#findComment-113469 Share on other sites More sharing options...
doni49 Posted October 24, 2006 Share Posted October 24, 2006 There are a couple of issues.1) your function doesn't use the table name that you're passing to it--so you don't really need to pass it.2) all variables created in the function are LOCAL to the function--meaning they can't be accessed from outside of the function. You need to return any functions that you want to use.I haven't tested this--I've only taken your code and modified it to what I'm pretty sure it needs to be. At the very least it'll get you started.[code]<?phpfunction dblistvalues(){// $inTable){ $db_name="db_name";// Database name $db_host="localhost"; $db_user="db_user"; $db_password="db_password"; //$table="$inTable"; // Table name <==table isn't used in the function so I'm hiding it. $connection=mysql_connect($db_host,$db_user,$db_password) or die("I Couldn't connect"); $db=mysql_select_db($db_name,$connection) or die("I Couldn't select your database"); return $db;}?>[/code]Now we'll use the function.[code]<?php $table = "orders"; $connection = dblistvalues();//set "connection" variable to the value returned by the function. if($connection){ $sql="select * from $table order by ID"; $result=mysql_query($sql,$connection) or die(mysql_error()); while($row=mysql_fetch_array($result)) { } } ?> [/code] Quote Link to comment https://forums.phpfreaks.com/topic/24896-simple-function-problem/#findComment-113470 Share on other sites More sharing options...
seran128 Posted October 24, 2006 Author Share Posted October 24, 2006 I was thinking would it not be better to dosomething like<?phpfunction dblistvalues($inTable){ $db_name="db_name";// Database name $db_host="localhost"; $db_user="db_user"; $db_password="db_password"; $table=$inTable; $connection=mysql_connect($db_host,$db_user,$db_password) or die("I Couldn't connect"); $db=mysql_select_db($db_name,$connection) or die("I Couldn't select your database"); $sql="select * from $table order by ID"; $result=mysql_query($sql,$connection) or die(mysql_error()); }?>Then on the other page call the function dblistvalues("my_table"); while($row=mysql_fetch_array($result)) {..........but how would I return the value back to this page using this example? Quote Link to comment https://forums.phpfreaks.com/topic/24896-simple-function-problem/#findComment-113475 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.