Re: problems with functions using an include file connecting to mysql
| From: | Cynic | Date: | Tue, 09 Jan 2001 13:20:03 +0000 |
| Subject: | Re: problems with functions using an include file connecting to mysql | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-33453@lists.php.net to get a copy of this message | ||
this one is very simple:
function foo()
{
global $bar ;
$bar = "something" ;
}
creates a reference to a global $bar, and then breaks the
reference when you set $bar to 'something'.
use $GLOBALS['bar'] instead.
At 14:08 9.1. 2001, First Name Last Name wrote the following:
--------------------------------------------------------------
>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/
>
>
>
>--
>PHP General Mailing List (http://www.php.net/)
>To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
>For additional commands, e-mail: php-general-help@lists.php.net
>To contact the list administrators, e-mail: php-list-admin@lists.php.net
------end of quote------
____________________________________________________________
Cynic:
A member of a group of ancient Greek philosophers who taught
that virtue constitutes happiness and that self control is
the essential part of virtue.
cynic@mail.cz