Simple PHP Issue - Functions and mysql

nick-harper

New member
Hi Guys,

I am having issues with this bit of code:

I keep getting this error on the line of code I have made bold. I can't understand why it works with the get_proxy function.

Notice: Undefined variable: hostname_Proxys
PHP:
    function get_proxy()
    {
        require_once('../Connections/Proxys.php');
        mysql_connect($hostname_Proxys, $username_Proxys, $password_Proxys) or die(mysql_error());
        mysql_select_db($database_Proxys) or die(mysql_error());

        $sql = "SELECT address
                FROM proxy
                ORDER BY rand()
                LIMIT 1";
        $q = mysql_query($sql);
        $r = mysql_fetch_array($q);
        if (empty($r) || empty($r["address"]))
        {
            echo 'You have run out of proxys. <a href="addproxys.php">Click Here</a> to add some more.';
            die;
        }
        return $r["address"];
    }    

    function delete_proxy($address)
    {
            require_once('../Connections/Proxys.php');
        mysql_connect($hostname_Proxys, $username_Proxys, $password_Proxys) or die(mysql_error());
        mysql_select_db($database_Proxys) or die(mysql_error());
        $sql = "DELETE FROM proxy WHERE address = '" . mysql_escape_string($address) . "'";
        mysql_query($sql);
    }
Can anybody help?

Thanks
 


So get_proxy() works and delete_proxy() doesn't, correct?

Where are all those variables being defined for the connection ($hostname_Proxys, $username_Proxys, $password_Proxys)? Inside ../Connections/Proxys.php ?
 
Yes, it can get the proxys but when they dont work it can't delete them.

../Connections/Proxys.php is as below:

Code:
$hostname_Proxys = "localhost";
$database_Proxys = "dbname";
$username_Proxys = "dbuser";
$password_Proxys = "dbpass";

So the variables are there.
 
Any error messages?

Substitue mysql_escape_string with mysql_real_escape_string () as it is deprecated (won´t probably solve your problem though).

what do the proxy-addresses look like - _user_:_passwd_@xxx.xxx.xxx.xxx:xx ?
 
..respectively what's the output for $sql when deleting proxies..

Add the following to your delete statement
....
$mysql_query($query);
$num = mysql_affected_rows();
echo $num;
 
change require_once('../Connections/Proxys.php');

to require('../Connections/Proxys.php');

in both functions.

delete_proxy() can't access the connection info "required" in get_proxy() and, since you used "require_once", it won't require it again.

You could also move the connection info out of the functions and make it global.
 
^^ was just about to post this. it's not smart to call the includes/mySQL connection every time the function is ran..