Jump to content

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?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.