Jump to content


Photo

SImple Function Problem


  • Please log in to reply
3 replies to this topic

#1 seran128

seran128
  • Members
  • PipPipPip
  • Advanced Member
  • 38 posts

Posted 24 October 2006 - 02:28 AM

I have a function named

dblistvalues values in my connection.php file

<?

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");
}
?>

I then call this function in another file called get_list.php

my code snippet is

<? 
          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)) { 
?>


The error I get is

Warning: 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.






#2 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 24 October 2006 - 02:43 AM

all the variables inside your function are local. you need to make them global or else return $connection.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#3 doni49

doni49
  • Members
  • PipPipPip
  • Advanced Member
  • 515 posts
  • LocationCentral MO

Posted 24 October 2006 - 02:46 AM

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.

<?php

function 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;

}
?>

Now we'll use the function.

<?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)) {
                    }
          } 
?> 

Don

#4 seran128

seran128
  • Members
  • PipPipPip
  • Advanced Member
  • 38 posts

Posted 24 October 2006 - 03:04 AM

I was thinking would it not be better to do
something like

<?php

function 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?






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users