This is a discussion on database within the PHP General forums, part of the PHP Programming Forums category; Hi, I'm very new with php, so perhaps it's a silly question. I want a connection with a ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Hi,
I'm very new with php, so perhaps it's a silly question. I want a connection with a mysql database. This works. I can open, retrieve data, and close the database. But this happens in the same page. Now I want one page that has a function who's opening and close the connection. In another page I want include this page. But I don't know the syntax. e.g page connectie.php Function openDB() { $link = mysql_connect("localhost", "host", "pas") or die("geen connectie mogelijk : " . mysql_error()); print "connectie ok"; mysql_select_db("database") or die("geen database gevonden"); } Function closeDB() { mysql_close($link); } End Function ?> <?php page input.php include ('connectie.php'); //open connectie $connectie = openDB(); //code //close connectie $connectie = closeDB(); This gives an error when I want to close the db. In the function I said close($link) but of course he cann't see this variabel. How can I fix that? Alain D'Haene Or is it possible the set the connection in an Object, and use this object during the Session? |
|
|||
|
Alain,
You can pass it in the function as a variable. E.g... Function closeDB($ref_link) { mysql_close($ref_link); } closeDB($connectie); Hope this Helps, Jordan S. Jones -- I am nothing but a poor boy. Please Donate.. https://www.paypal.com/xclick/busine...rency_code=USD |
|
|||
|
I have try
but I get the following error: Warning: mysql_close(): supplied argument is not a valid MySQL-Link resource in /home/schoolre/public_html/Hitek/Online/connectie.php on line 16 Alain "Jordan S. Jones" <list@racistnames.com> schreef in bericht news:3F9D61F5.7040105@racistnames.com... > Alain, > > You can pass it in the function as a variable. > > E.g... > > Function closeDB($ref_link) > { > mysql_close($ref_link); > } > > > closeDB($connectie); > > Hope this Helps, > > Jordan S. Jones > > -- > > I am nothing but a poor boy. Please Donate.. > https://www.paypal.com/xclick/busine...item_name=Jord an+S.+Jones&no_note=1&tax=0¤cy_code=USD |
|
|||
|
Alain,
Ok, I aplogize, for I neglected to notice that you were not passing back the db connection $link variable from your openDB function, so here is what you are going to want to do. page connectie.php Function openDB() { $link = mysql_connect("localhost", "host", "pas") or die("geen connectie mogelijk : " . mysql_error()); print "connectie ok"; mysql_select_db("database") or die("geen database gevonden"); return $link; } Function closeDB($ref_link) { mysql_close($ref_link); } include ('connectie.php'); //open connectie $connectie = openDB(); //code //close connectie closeDB($connectie); Jordan S. Jones alain dhaene wrote: >I have try >but I get the following error: > > >Warning: mysql_close(): supplied argument is not a valid MySQL-Link resource >in /home/schoolre/public_html/Hitek/Online/connectie.php on line 16 > >Alain > > >"Jordan S. Jones" <list@racistnames.com> schreef in bericht >news:3F9D61F5.7040105@racistnames.com... > > >>Alain, >> >>You can pass it in the function as a variable. >> >>E.g... >> >>Function closeDB($ref_link) >>{ >> mysql_close($ref_link); >>} >> >> >>closeDB($connectie); >> >>Hope this Helps, >> >>Jordan S. Jones >> >>-- >> >>I am nothing but a poor boy. Please Donate.. >> >> >> >https://www.paypal.com/xclick/busine...item_name=Jord >an+S.+Jones&no_note=1&tax=0¤cy_code=USD > > > -- I am nothing but a poor boy. Please Donate.. https://www.paypal.com/xclick/busine...rency_code=USD |
|
|||
|
alain dhaene wrote:
> I have try > but I get the following error: > > > Warning: mysql_close(): supplied argument is not a valid MySQL-Link resource > in /home/schoolre/public_html/Hitek/Online/connectie.php on line 16 > > Alain <snip> Look here.... http://us4.php.net/manual/en/languag...bles.scope.php Your $link only has the scope of that function. You can either pass it to the function, or put this line inside of your function.... global $link; -- By-Tor.com It's all about the Rush http://www.by-tor.com |
|
|||
|
It works,
Thx "John Nichel" <jnichel@by-tor.com> schreef in bericht news:3F9DBBDE.9020502@by-tor.com... > alain dhaene wrote: > > I have try > > but I get the following error: > > > > > > Warning: mysql_close(): supplied argument is not a valid MySQL-Link resource > > in /home/schoolre/public_html/Hitek/Online/connectie.php on line 16 > > > > Alain > <snip> > > Look here.... > > http://us4.php.net/manual/en/languag...bles.scope.php > > Your $link only has the scope of that function. You can either pass it > to the function, or put this line inside of your function.... > > global $link; > > -- > By-Tor.com > It's all about the Rush > http://www.by-tor.com |