Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: passing values

  1. #1
    Regular Coder
    Join Date
    Nov 2002
    Posts
    180
    Thanks
    0
    Thanked 0 Times in 0 Posts

    passing values

    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:
    Code:
    <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?

  • #2
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #3
    New Coder
    Join Date
    Apr 2004
    Location
    Texas
    Posts
    60
    Thanks
    0
    Thanked 0 Times in 0 Posts

    hope this helps...

    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....
    Jason B


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •