Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Function to build drop down controlls 1

Status
Not open for further replies.

StuartBombay

Programmer
Feb 11, 2008
56
US
I'm trying to build a function to use for creating drop downs. PHP 5, MySQL 5, Apache2.
The configuration: configdb.inc
Code:
<?php
// Configure database
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'password';
$dbname = 'sitedata';
?>

The connection: opendb.inc
Code:
<?php
// Open the database
$conn = mysqli_connect($dbhost, $dbuser, $dbpass) or die 
	('Error connecting to mysql '.mysqli_error());
mysqli_select_db($conn, $dbname);
?>
I get $conn not defined when I try to run this, I don't understand because it is passing the database name?
Code:
<?php
require('../../includes/configdb.inc');
require('../../includes/opendb.inc');

echo "From opendb.inc: $dbname";
function drop_down($intID, $strName, $tableName, $strOrderField, $strMethod="asc") {

  $strQuery = "select $intID, $strName from $tableName order by $strOrderField $strMethod";
echo "<p> $strQuery </p>";
 
  $result = mysqli_query($conn, $strQuery);

  while($arrayRow = mysqli_fetch_array($result, MYSQL_ASSOC)) {
   $intIdField = $arrayRow["$intID"];
   $strNameField = $arrayRow["$strName"];
   echo "<option value=\"$intIdField\">$strNameField</option>\n";
  }
  echo "</select>\n\n";
}
drop_down('prognum', 'fullname', 'programs', 'lastname');
?>
This include method works when I run a query and display a table - but this does not work.
Can anyone explain to me what's wrong?
The code is incomplete, I've take a lot out in an attempt to debug.
Thanks.
 
Its a Variable scope problem. basically your $conn variable is not available inside your function.

You have several options, pass the variable as an additional parameter to your function, make your $conn variable global, or include your DB connection inside your function.



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Thanks for the answer, and it does make sense. But what confuses me is that the part-

require('../../includes/configdb.inc');
require('../../includes/opendb.inc');

echo "From opendb.inc: $dbname";

returns the correct value for $dbname. So, if the value of $dbName is being passed, then why not $conn?
Also, is it good practice to make a connection variable global, or should I only have the connection values in an include and do the connection in each function (as you suggested)?
I'm very new with PHP, the class I took was a waste of $$ so I'm relying on books and forums. Feel free to point me to articles too.
Thanks again.
 
Again Scope, your echo statement is outside your function therefore has a different scope than what is going on inside your function.

Here PHP's online manual is the best source of info there is for this type fo thing. Its also incredibly well organized, so its easy to find things you need.






----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Again Scope, your echo statement is outside your function therefore has a different scope than what is going on inside your function."

OH! I get it!

Thank you.
 
really you should not use mysqli_select_db at all. this is for changing databases. to instantiate a db connection using mysqli just add the dbname as the 4th param to the mysqli_connect() call.

personally i would use the OO form of mysqli if you want to use the improved extension. then make the object global when required. (using the global keyword)
 
Thank you jpadie, I've removed the mysqli_connect().
I like the idea of the Object Oriented method, but I don't understand the current one yet.
Why is the scope OK when used like this:

Code:
require('../../includes/configdb.inc');
require('../../includes/opendb.inc');

$SQL = 'SELECT v_program_details.FullName, programs.fullname as cluster, v_program_details.Organization, v_program_details.LowGrade, v_program_details.HighGrade, v_program_details.esisNum, v_program_details.Phone, v_program_details.Fax, v_program_details.LocAddress, v_program_details.LocZip
FROM (
v_program_details
LEFT JOIN v_cluster_program ON v_program_details.ProgNum = v_cluster_program.ProgNum
)
LEFT JOIN programs ON v_cluster_program.parent_prognum = programs.ProgNum;
';

$result = mysqli_query($conn, $SQL);

$count = 0;

echo "<table border='1'>
<tr>
<th>Location</th>
<th>Cluster</th>
</tr>";

while ($row = mysqli_fetch_array($result, MYSQL_ASSOC))
	{
	$count = $count + 1;
	echo "<tr>";
	echo "<td>" . $row['FullName'];
	echo "<td>" . $row['cluster'] ."<td>";
	echo "</tr>";
	}

echo "</table>";

But not in a function like this? What do I need to do to this function in order to have $conn work?

Code:
require('../../includes/configdb.inc');
require('../../includes/opendb.inc');

function drop_down($intID, $strName, $tableName, $strOrderField, $strMethod="asc") {

  $strQuery = "select $intID, $strName from $tableName order by $strOrderField $strMethod";
echo "<p> $strQuery </p>";
 
  $RSresult = mysqli_query($conn, $strQuery);

  while($arrayRow = mysqli_fetch_array($RSresult, MYSQL_ASSOC)) {
   $intIdField = $arrayRow["$intID"];
   $strNameField = $arrayRow["$strName"];
   echo "<option value=\"$intIdField\">$strNameField</option>\n";
  }
  echo "</select>\n\n";
}
drop_down('prognum', 'fullname', 'programs', 'fullname');

I'm probably being dense, I thought I had it but after changing the opendb.inc to the following, I'm still not keeping the connection. I read about Globals in the PHP manual... but, well, dunce hat for me.

Code:
<?php
// Open the database
Global $conn, $dbhost, $dbuser, $dbpass,$dbname;

$conn = mysqli_connect($dbhost, $dbuser, $dbpass,$dbname) or die 
	('Error connecting to mysql '.mysqli_error());

?>
 
in the function add the line (as the first line)
Code:
global $conn;

variables declared outside the function are not available inside a function or method unless they are made global or are already superglobal ($_POST, $_GET etc). functions and methods have their own 'scope'.

so to make a global variable (i.e. a variable that was declared in the global scope, outside a function or method or class), available to a function, you either need to pass the variable in as an argument or to tell the function that you want to be able to use a global variable inside it. this is what the global keyword does. you must use it within each function that you want to use the $conn variable.
 
Thank you, that works in my example. I couldn't get my head around declaring the global variable in form_function and not in the connection include. Making it global at the start made more sense to me.
I found a much better book than what I got in my useless class: 'Sams Teach Yourself PHP MySQL and Apache' by Julie Miloni. For anyone else who is struggling with this concept like I am, this may be a good read.
Thanks again for your patience.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top