Jump to content

SImple Function Problem


seran128

Recommended Posts

I have a function named

dblistvalues 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.php

my 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 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.




Link to comment
https://forums.phpfreaks.com/topic/24896-simple-function-problem/
Share on other sites

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]
<?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;

}
?>
[/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]
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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.