0

My goal is to populate a html select box from mysql using a php function. I started by putting the code directly on the html page, and I got it working.

<label for="product_Category">Product Category</label>
<?
$field_Name = "category_Name";
$table_Name = "Product_Category";

$sql = "Select " . $field_Name . " From " . $table_Name;
$results = mysqli_query($link, $sql);
echo "<select>";
echo "<option value = \" \" >Select Category"</option>;
while($row = mysqli_fetch_array($results, MYSQLI_ASSOC)){
echo "<option value = ' " .$row[$field_Name] . "'>" . $row[$field_Name]. "</option>";
}
echo "</select>";
?>

I have multiple select boxes, so I thought it would be easier to create a function and pass the table name, and field name as arguments

I moved the php function to its own file, and use an include statement in my html. But once I tried to call the PHP function, the select box wont populate. The select box shows up on the form like it should, but its empty.

PHP

<? 
function Populate_Select($table_Name, $field_Name){ 

$sql = "Select " . $field_Name . " From " . $table_Name;
$results = mysqli_query($link, $sql);
echo "<select>";
echo "<option value = \" \" >Select Category </option>";
while($row = mysqli_fetch_array($results, MYSQLI_ASSOC)){
    echo "<option value = ' " .$row[$field_Name] . "'>" . $row[$field_Name]. "    </option>";
}
echo "</select>";
}
?>

DB Config

<?
$host = "localhost";
$db_userName = "root";
$db_Password = "root";
$db_Name = "mydb";

$link = mysqli_connect($host, $db_userName, $db_Password, $db_Name);
     if (!$link){
        die("Database Connection failed " . mysqli_connect_error);
     }

?>

HTML Code

<? include 'PopulateSelect.php' ?>
<? include 'DB_Config.php ?>
<!--Rest of HTML CODE-->
<label for="product_Category">Product Category</label>              
<? Populate_Select('Product_Category', 'category_Name'); ?>

Where did I go wrong when I called the function?

Is it better practice to use a function or am I better off just writing separate code for each select box?

*This is my first time posting, so I apologize if my post is not correct or formatted poorly.

  • What is the actual output? In the above example there's at least `` missing. – hchr Feb 16 '15 at 18:40
  • i see youre calling the function `Populate_Select()` , where is this being defined? and where is `$link` defined? – CodeGodie Feb 16 '15 at 18:41
  • 1
    P{lease add some [error checking](http://stackoverflow.com/questions/17053466/how-to-display-errors-for-my-mysqli-query). – Jay Blanchard Feb 16 '15 at 18:43
  • You are not closing the option tag, in your last example – Amit Verma Feb 16 '15 at 18:46
  • so after your edi, you still have not specified where `$link` is being declared.. im assuming it is declared outside the function? – CodeGodie Feb 16 '15 at 18:54
  • Yes .. $link is declared in another php file that holds my database connection .. It is also included in the html file. I edited the file to show those parts of the code – Binary_Complex Feb 16 '15 at 19:01

3 Answers3

0

It looks like your $link object is not available in the function you declared. Try passing the $link object to the function.

It could look something like this:

function Populate_Select($link, $field_Name, $table_Name){ ... }
  • … and properly configured error_reporting would have told that straight away, so @user4572663 see to it that you enable it. – CBroe Feb 16 '15 at 18:53
  • How would I go about enabling error_reporting to see that my object is not available in the function? – Binary_Complex Feb 16 '15 at 19:37
  • Just add the following line once in your application: `error_reporting(E_ALL)` This shows every error and every warning your code generates. But make sure you remove it in your production application. – Aaron Schmied Feb 17 '15 at 13:04
0

The problem is that your $link variable is not defined. You need to create a class, OOP makes life easier, like this:

//myclass.php

class MyClass {

    private $link;

    function __construct() {
        $host = "localhost";
        $db_userName = "root";
        $db_Password = "root";
        $db_Name = "mydb";
        $this->link = mysqli_connect($host, $db_userName, $db_Password, $db_Name);
        if (!$this->link) {
            die("Database Connection failed " . mysqli_connect_error);
        }
    }

    function __destruct() {
        mysqli_close($this->link);
    }

    function Populate_Select($table_Name, $field_Name) {
        $sql = "Select $field_Name From $table_Name";
        $results = mysqli_query($this->link, $sql);
        $select = "<select>";
        $select .= "<option value=''>Select Category</option>";
        while ($row = mysqli_fetch_array($results, MYSQLI_ASSOC)) {
            $select .= "<option value='$row[$field_Name]'>$row[$field_Name]</option>";
        }
        $select .= "</select>";
        return $select;
    }
}

Then you can call this on your other pages by including that .php file and accessing the class, like this:

//your some_html.php

include 'myclass.php';
$obj = new MyClass();
$select = $obj->Populate_Select("your_table_name", "your_field_name");
echo $select;
CodeGodie
  • 11,914
  • 5
  • 35
  • 64
  • I created the class above in a separate file, and then included it in my html. Then I put the code in my html file to create the new class, and replaced the arguments with the correct values surrounded by single quotes. But select box disappeared off the form. Before it was empty, but now the select box is gone completely. – Binary_Complex Feb 16 '15 at 19:47
  • I made some edits, try it again and let me know if it works, the function will return the variable, but then you have to echo it out. – CodeGodie Feb 16 '15 at 19:50
  • I had a syntax error as well.. i double checked it, it should now work. – CodeGodie Feb 16 '15 at 20:01
0

You could also pass the connection as a reference to the function.

function Populate_Select($field_Name, $table_Name, &$link){ ... }

And then when you call it, add $link as the third argument. Otherwise, you're looking for a global variable inside of a function.

Shane Lessard
  • 625
  • 1
  • 8
  • 17