...

View Full Version : passing values



Aymen++
04-15-2004, 07:16 AM
i wana do one page that displays the connection status or the databases on MySQL depending on the value of a variable (action). i did it like this:


<html>

<head>
<title>Welcome</title>
</head>

<body>
<?php
function disconnect() {
global $r;
mysql_close($r) or die("Could not disconnect: " . mysql_error());
echo "You have been disconnected successfully";
}

switch ($action) {
case "showdatabases":
$db_list = mysql_list_dbs($r);
while($row = mysql_fetch_object($db_list)) {
echo $row->Database . "\n";
}
break;
case "disconnect":
disconnect();
break;
default:
$conn = mysql_connect($dbhost, $txtUserName, $txtPassword) or die("Could not Connect: " . mysql_error());
echo "Connected successfully<BR>";
echo "<a href='index.php?action=showdatabases&r=$conn'>Show databases<BR></a>";
echo "<a href='index.php?action=disconnect&r=$conn'>Disconnect<BR></a>";
}
?>
</body>

</html>

and i had this error when i press show databases:
Warning: Supplied argument is not a valid MySQL-Link resource in c:\program files\apache group\apache\htdocs\test\index.php on line 17

Warning: Supplied argument is not a valid MySQL result resource in c:\program files\apache group\apache\htdocs\test\index.php on line 18

and this when i press disconnect:
Warning: Supplied argument is not a valid MySQL-Link resource in c:\program files\apache group\apache\htdocs\test\index.php on line 11
Could not disconnect:

so, how can i pass the value of $conn to another page?

raf
04-15-2004, 08:27 AM
You shouldn't try.

Try link ID ($conn) holds a handler for the connection that was openend. But when the script is finished, the connection is automatically closed, so the handler becomes invalid.
You just need to put your connectionfunction before your db-query

If you don't want' the connection to be closed, then use a persintent connection (mysql_pconnect() ) and then the parser will automatically look for an open available connection. But this has also some downsides (run a search here. I've posted some info about it yesterday)

Note : didn't you have another thread on how to list db's :confused:

black3842
04-15-2004, 08:28 AM
Well, normally you just reconnect on each page. Make sure you're using
mysql_connect and not mysql_pconnect(p is for Persistent).

most folks will put connection info in something like Connect.php
then
include ('Connect.php')
in each file that needs to access the db. The connection to the database will close automatically when your script finishes executing, unless you're using pconnect, but it's still not a bad to have that disconnect function like you do.....

something like so...for Connect.php

<?php
// This file connects to the MySQL server and selects the database we will work with.
define ('DB_USER','testuser');
define ('DB_PASSWORD','test');
define ('DB_HOST','localhost');
define ('DB_NAME','testdb');

$conn = mysql_connect(DB_HOST,DB_USER) OR die('Could not connect to MySQL: '.mysql_error());
mysql_select_db(DB_NAME) OR die('Could not select database: '.mysql_error());
?>

hope that helps....



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum