dbTalk Databases Forums  

Why if it works in PHP, dioes it not work in C?

comp.databases.mysql comp.databases.mysql


Discuss Why if it works in PHP, dioes it not work in C? in the comp.databases.mysql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
The Natural Philosopher
 
Posts: n/a

Default Why if it works in PHP, dioes it not work in C? - 05-27-2011 , 10:59 AM






In php I can use:-

mysql_connect("localhost", $user,$password);
mysql_select_db($database);

but using C API I have to use this:-


mysql_real_connect(&mysql,"127.0.0.1",USERNAME,PAS SWORD,DATABASE,0,"",0)

"localhost" or a null "" for the host, simply do not work..

anyone care to comment?

they are different databases, in each case, but the same user/password.

Reply With Quote
  #2  
Old   
Sherm Pendley
 
Posts: n/a

Default Re: Why if it works in PHP, dioes it not work in C? - 05-27-2011 , 11:19 AM






The Natural Philosopher <tnp (AT) invalid (DOT) invalid> writes:

Quote:
In php I can use:-

mysql_connect("localhost", $user,$password);
mysql_select_db($database);

but using C API I have to use this:-


mysql_real_connect(&mysql,"127.0.0.1",USERNAME,PAS SWORD,DATABASE,0,"",0)

"localhost" or a null "" for the host, simply do not work..

anyone care to comment?
The Mysql client library takes a different view of "localhost" than the
rest of the world does. Instead of resolving it like it would any other
host name, it takes it as a request to connect to the local domain
socket.

I suspect that PHP may be resolving the host name itself, and passing
the IP address to the C function.

sherm--

Reply With Quote
  #3  
Old   
jimp@specsol.spam.sux.com
 
Posts: n/a

Default Re: Why if it works in PHP, dioes it not work in C? - 05-27-2011 , 11:23 AM



The Natural Philosopher <tnp (AT) invalid (DOT) invalid> wrote:
Quote:

In php I can use:-

mysql_connect("localhost", $user,$password);
mysql_select_db($database);

but using C API I have to use this:-


mysql_real_connect(&mysql,"127.0.0.1",USERNAME,PAS SWORD,DATABASE,0,"",0)

"localhost" or a null "" for the host, simply do not work..

anyone care to comment?

they are different databases, in each case, but the same user/password.
To mysql, localhost is not 127.0.0.1, it is a connection to a Unix socket
file on a local server.

http://dev.mysql.com/doc/refman/5.0/en/connecting.html

--
Jim Pennino

Remove .spam.sux to reply.

Reply With Quote
  #4  
Old   
The Natural Philosopher
 
Posts: n/a

Default Re: Why if it works in PHP, dioes it not work in C? - 05-27-2011 , 11:24 AM



Sherm Pendley wrote:
Quote:
The Natural Philosopher <tnp (AT) invalid (DOT) invalid> writes:

In php I can use:-

mysql_connect("localhost", $user,$password);
mysql_select_db($database);

but using C API I have to use this:-


mysql_real_connect(&mysql,"127.0.0.1",USERNAME,PAS SWORD,DATABASE,0,"",0)

"localhost" or a null "" for the host, simply do not work..

anyone care to comment?

The Mysql client library takes a different view of "localhost" than the
rest of the world does. Instead of resolving it like it would any other
host name, it takes it as a request to connect to the local domain
socket.

I suspect that PHP may be resolving the host name itself, and passing
the IP address to the C function.

So that suggests that my myqsld is not actually using a unix socket, but
an IP socket only?

How would I tell?

> sherm--

Reply With Quote
  #5  
Old   
Sherm Pendley
 
Posts: n/a

Default Re: Why if it works in PHP, dioes it not work in C? - 05-27-2011 , 11:32 AM



The Natural Philosopher <tnp (AT) invalid (DOT) invalid> writes:

Quote:
Sherm Pendley wrote:
The Natural Philosopher <tnp (AT) invalid (DOT) invalid> writes:

In php I can use:-

mysql_connect("localhost", $user,$password);
mysql_select_db($database);

but using C API I have to use this:-


mysql_real_connect(&mysql,"127.0.0.1",USERNAME,PAS SWORD,DATABASE,0,"",0)

"localhost" or a null "" for the host, simply do not work..

anyone care to comment?

The Mysql client library takes a different view of "localhost" than the
rest of the world does. Instead of resolving it like it would any other
host name, it takes it as a request to connect to the local domain
socket.

I suspect that PHP may be resolving the host name itself, and passing
the IP address to the C function.


So that suggests that my myqsld is not actually using a unix socket,
but an IP socket only?

How would I tell?
You can tell because "127.0.0.1" works, and "localhost" does not. The
former uses an IP socket; the latter, a domain socket. PHP is apparently
obscuring the difference by resolving the host name itself, rather than
passing it as given to the C function.

sherm--

Reply With Quote
  #6  
Old   
Jerry Stuckle
 
Posts: n/a

Default Re: Why if it works in PHP, dioes it not work in C? - 05-27-2011 , 12:23 PM



On 5/27/2011 11:59 AM, The Natural Philosopher wrote:
Quote:

In php I can use:-

mysql_connect("localhost", $user,$password);
mysql_select_db($database);

but using C API I have to use this:-


mysql_real_connect(&mysql,"127.0.0.1",USERNAME,PAS SWORD,DATABASE,0,"",0)

"localhost" or a null "" for the host, simply do not work..

anyone care to comment?

they are different databases, in each case, but the same user/password.


Your MySQL configuration is probably screwed up. PHP obviously knows
where to look for the Unix socket, but C doesn't. The most obvious
answer would be that the MySQL configuration has the wrong information
for the socket but the php.ini file overrides it with the correct
information.

Another possibility would be a security problem connecting to the MySQL
socket from your C application, which may be running under a different
user id.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex (AT) attglobal (DOT) net
==================

Reply With Quote
  #7  
Old   
John Levine
 
Posts: n/a

Default Re: Why if it works in PHP, dioes it not work in C? - 05-27-2011 , 03:40 PM



Quote:
mysql_real_connect(&mysql,"127.0.0.1",USERNAME,PAS SWORD,DATABASE,0,"",0)

"localhost" or a null "" for the host, simply do not work..

anyone care to comment?
You have a configuration problem of some sort. Connecting to the Unix
socket from the C library should work.

The socket is normally named /tmp/mysql.sock -- any chance an overzealous
disk cleanup script deleted it?

R's,
John

Reply With Quote
  #8  
Old   
Tim Watts
 
Posts: n/a

Default Re: Why if it works in PHP, dioes it not work in C? - 05-27-2011 , 04:00 PM



John Levine wrote:

Quote:
mysql_real_connect(&mysql,"127.0.0.1",USERNAME,PAS SWORD,DATABASE,0,"",0)

"localhost" or a null "" for the host, simply do not work..

anyone care to comment?

You have a configuration problem of some sort. Connecting to the Unix
socket from the C library should work.

The socket is normally named /tmp/mysql.sock -- any chance an overzealous
disk cleanup script deleted it?

It could also be under /var/run or even /var/lib (either is betetr than /tmp
for the reasons given).

--
Tim Watts

Reply With Quote
  #9  
Old   
The Natural Philosopher
 
Posts: n/a

Default Re: Why if it works in PHP, dioes it not work in C? - 05-27-2011 , 04:18 PM



Tim Watts wrote:
Quote:
John Levine wrote:

mysql_real_connect(&mysql,"127.0.0.1",USERNAME,PAS SWORD,DATABASE,0,"",0)

"localhost" or a null "" for the host, simply do not work..

anyone care to comment?
You have a configuration problem of some sort. Connecting to the Unix
socket from the C library should work.

The socket is normally named /tmp/mysql.sock -- any chance an overzealous
disk cleanup script deleted it?


It could also be under /var/run or even /var/lib (either is betetr than /tmp
for the reasons given).

I think this is down to the standard debian configuration actually;-

from /etc/mysql/my.cnf


[client]
port = 3306
socket = /var/run/mysqld/mysqld.sock

# Here is entries for some specific programs
# The following values assume you have at least 32M ram

# This was formally known as [safe_mysqld]. Both versions are currently
parsed.
[mysqld_safe]
socket = /var/run/mysqld/mysqld.sock
nice = 0

[mysqld]
#
# * Basic Settings
#
user = mysql
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
port = 3306
basedir = /usr
datadir = /var/lib/mysql
tmpdir = /tmp
language = /usr/share/mysql/english
skip-external-locking
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address = 127.0.0.1
#

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.