problems with functions using an include file connecting to mysql
| From: | First Name Last Name | Date: | Tue, 09 Jan 2001 13:08:02 +0000 |
| Subject: | problems with functions using an include file connecting to mysql | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-33452@lists.php.net to get a copy of this message | ||
hi,
I am having some very diffucly problems using global var's form functions espeically with mysql
connections. I have created a small include file containing a couple of functions, mysql connection
stuff. When I run my main script I can connect and change databse OK, calling the functions. But as
soon as I use other mysql functions from my main script I get errors. When I run the script I get...
logged in
changed into to DB
Warning: Supplied argument is not a valid MySQL result resource
....
On the following lines..
$num_rows=mysql_num_rows($mysql_result);
while ($row=mysql_fetch_array($mysql_result))
What am I doing wrong!. I have made the variables global.
Heres my small main script, followed by my functions file.
<?php
require ("inc.php");
conn();
echo "logged in";
change_db();
echo "changed into to DB";
$sql="SELECT name, address,ID FROM mydb";
$mysql_result=mysql_query($sql,$connect);
$num_rows=mysql_num_rows($mysql_result);
while ($row=mysql_fetch_array($mysql_result))
{
$name=$row["name"];
$address=$row["address"];
$ID=$row["ID"];
echo "$name:$address:$ID<BR>";
}
mysql_close($connection);
?>
<?php
# inc.php
function conn(){
global $connect;
$connect=mysql_pconnect("localhost","","");
if (!$connect) {
echo "could not connect";
exit;
}
}
function change_db(){
global $db;
global $connect;
$db=mysql_select_db("mydb",$connect);
if (!$db)
{
echo "Could not select database";
exit;
}
}
------------------------------------------------------------
--== Sent via Deja.com ==--
http://www.deja.com/