homepage Welcome to WebmasterWorld Guest from 54.242.231.109
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
I have to use CURL to connect to my remote database
Never used CURL before. Is this even possible?
ntbgl




msg:3682558
 5:05 pm on Jun 24, 2008 (gmt 0)

I'm trying to insert some data into an MySQL database using PHP, however, my hosting company dosn't allow this.

I used a simple script:

$mysql_connect=mysql_connect("database.tld","username","password");
mysql_select_db("database_name");
mysql_query("INSERT INTO table (value1) VALUES ($value)");
mysql_close($mysql_connect);

I contacted my server and they said:

"If you you want to make any outbound remote connections you will need to use our proxy server. Below is a PHP script that uses CURL to connect securely to a remote system (http://www.paypal.com), obtain data from that system and then creates a Web page based on that data.

$URL="https://www.paypal.com";
if (isset($_GET["site"])) { $URL = $_GET["site"]; }
$ch = curl_init();
echo "URL = $URL <br>n";
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt ($ch, CURLOPT_HTTPPROXYTUNNEL, TRUE);
curl_setopt ($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
curl_setopt ($ch, CURLOPT_PROXY,"http://proxy.shr.secureserver.net:3128");
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($ch, CURLOPT_URL, $URL);
curl_setopt ($ch, CURLOPT_TIMEOUT, 120);
$result = curl_exec ($ch);
echo "<hr><br>n";
echo 'Errors: ' . curl_errno($ch) . ' ' . curl_error($ch) . '<br><br>';
echo "<hr><br>n";
curl_close ($ch);
print "result - $result";
echo "<hr><br>n";"

How do I change this so I can make a simple database connect and insert? Or is this even the right direction?

Thanks

 

NomikOS




msg:3683212
 4:06 pm on Jun 25, 2008 (gmt 0)

still alone? here are using curl [webmasterworld.com...]

ntbgl




msg:3683647
 2:52 am on Jun 26, 2008 (gmt 0)

Thank you for your reply.

Right now I'm just wondering if mysql connects or inserts can even take place through cURL.

It seems like cURL is just used for getting data from another page, but can mysql queries take place over it? Or did the help desk just send me searching for a red herring?

Romeo




msg:3684012
 1:01 pm on Jun 26, 2008 (gmt 0)

The CURL is for talking to remote HTTP services on port 80 and can work through a HTTP proxy.

Your mysql client is trying to talk to a mysql database running on a remote port 3306 (or 3333, or whatever port is configured over there, but you surely have checked this).

Different thing, different ports, different protocol, different understanding of what a proxy is and how it works, apples and oranges.

Kind regards,
R.

WesleyC




msg:3684055
 2:03 pm on Jun 26, 2008 (gmt 0)

I believe that mysql_connect() can accept a remote host as an argument, however--try looking into that.

ntbgl




msg:3684167
 3:40 pm on Jun 26, 2008 (gmt 0)

Thanks,

My understanding now is that CURL is the wrong way to go about this. Right?

Looking at the mysql_connect function on php.net, it states for the server parameter:

"The MySQL server. It can also include a port number. e.g. "hostname:port" or a path to a local socket e.g. ":/path/to/socket" for the localhost.

If the PHP directive mysql.default_host is undefined (default), then the default value is 'localhost:3306'. In SQL safe mode, this parameter is ignored and value 'localhost:3306' is always used."

I don't fully understand all of this right now, but is this the area I would want to focus on?

Is the mysql_connect the failing part of my simple inserting script? If I were able to get the mysql_connect function rigged to connect to my remote host, would I be able to insert and disconnect just find? Or would I most likely have to play around with those aspects too?

Thanks

Romeo




msg:3684204
 4:14 pm on Jun 26, 2008 (gmt 0)

Ntgbl, in your original posting you wrote about connecting to a REMOTE database service running on another distant host "database.tld".

To connect to a remote database (on another box than your webserver), try this:
$mysql_connect=mysql_connect('database.tld:3306','username','password');
if the remote database is confugured to run on port 3306.

To connect to a local database (on the same box as your webserver), try this:
$mysql_connect=mysql_connect('localhost','username','password');

Your original statement
my hosting company dosn't allow this
implied to me that you have already done all checks necessary to come to this conclusion.

Is the mysql_connect the failing part of my simple inserting script?
We don't know, unless you check for this error:
if (!$mysql_connect) {
die('no connect: ' . mysql_error());
} else {
echo 'db connect OK';
}

Here is the manual page:
[php.net...]

Kind regards,
R.

[edited by: Romeo at 4:16 pm (utc) on June 26, 2008]

WesleyC




msg:3684224
 4:25 pm on Jun 26, 2008 (gmt 0)

mysql_connect should do everything you need.

You can determine if it's the point of failure by calling mysql_error() after every mysql action you take. This will tell you where and what the error is.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved