You are not logged in.

#1 2008-03-22 20:57:27

Kobussie
Member
From: NL - Groningen
Registered: 2007-04-06
Posts: 43
Website

[SOLVED] php-mysql connection

Hi everyone!

I'm just starting out with php and mysql, and encountered the following problem:

I have installed:

Apache 2.2.8-1
PHP 5.2.5-7
MySQL 5.0.51-3

I can log in to mysql from the commandline; I made a small database to test.
When I try to connect to this database from a webpage with this php-code:

<?php
require_once('db_login.php');
// connect
$connection = mysql_connect($db_host, $db_username, $db_password);
if (!$connection){
        die ("Kan geen verbinding krijgen met de database: <br />".mysql_error());
};
// select database
$db_select=mysql_select($db_database);
if (!$db_select){
        die ("Database niet gevonden: <br />".mysql_error());
};
mysql_close($connection);
echo "succes !";
?>

file db_login.php:

<?php
$db_host='127.0.0.1';
$db_database='bandjes';
$db_username='ko';
$db_password='Kobus';
?>

I get this error:

Kan geen verbinding krijgen met de database:
Lost connection to MySQL server at 'reading initial communication packet', system error: 0

I have googled for this error, but can't find any answer for my situation. Any suggestions will be appreciated!

Last edited by Kobussie (2008-03-25 22:05:34)

Offline

#2 2008-03-23 02:34:11

Shaika-Dzari
Member
From: Québec, Canada
Registered: 2006-04-14
Posts: 436
Website

Re: [SOLVED] php-mysql connection

Hello!

Did you uncomment the mysql plugin line in php.ini to activate mysql support?
Look here: http://wiki.archlinux.org/index.php/LAM … QL_support

@+

Offline

#3 2008-03-23 13:09:12

Kobussie
Member
From: NL - Groningen
Registered: 2007-04-06
Posts: 43
Website

Re: [SOLVED] php-mysql connection

Thanks!

And yes, I did all that's described in this wiki-page. I Also added "extension=session.so" to the php.ini file to get MyAdmin working.

Offline

#4 2008-03-23 14:57:41

ralvez
Member
From: Canada
Registered: 2005-12-06
Posts: 1,718
Website

Re: [SOLVED] php-mysql connection

Take a look at /var/log/httpd/error_log and /var/log/php.log to find what's failing.

R.

Offline

#5 2008-03-23 16:00:27

Kobussie
Member
From: NL - Groningen
Registered: 2007-04-06
Posts: 43
Website

Re: [SOLVED] php-mysql connection

Hi, ralvez

I looked at httpd/error_log; this gives me the same error message:

[Sun Mar 23 17:27:26 2008] [warn] Init: Session Cache is not configured [hint: SSLSessionCache]
[Sun Mar 23 17:27:26 2008] [notice] suEXEC mechanism enabled (wrapper: /usr/bin/suexec)
[Sun Mar 23 17:27:26 2008] [notice] Digest: generating secret for digest authentication ...
[Sun Mar 23 17:27:26 2008] [notice] Digest: done
[Sun Mar 23 17:27:26 2008] [notice] Apache/2.2.8 (Unix) DAV/2 mod_ssl/2.2.8 OpenSSL/0.9.8g configured -- resuming normal operations
[Sun Mar 23 17:29:15 2008] [error] [client 10.0.0.2] File does not exist: /home/www/majako/favicon.ico
[Sun Mar 23 17:29:19 2008] [error] [client 10.0.0.2] PHP Warning:  mysql_connect() [<a href='function.mysql-connect'>function.mysql-connect</a>]: Lost connection to MySQL server at 'reading initial communication packet', system error: 0 in /home/www/majako/test_mysql.php on line 22, referer: http://10.0.0.2/

Maybe i should do something with the first line about SSlSesionCache?

php doesn't seem to log anything; I changed the log_error lines in php.ini so now it's supposed to log to /var/log/php/php_error_log, but the file doesn't exist after calling the script several times

Offline

#6 2008-03-23 16:09:53

ralvez
Member
From: Canada
Registered: 2005-12-06
Posts: 1,718
Website

Re: [SOLVED] php-mysql connection

To me this is the "key" line:
[Sun Mar 23 17:29:19 2008] [error] [client 10.0.0.2] PHP Warning:  mysql_connect() [<a href='function.mysql-connect'>function.mysql-connect</a>]: Lost connection to MySQL server at 'reading initial communication packet', system error: 0 in /home/www/majako/test_mysql.php on line 22, referer: http://10.0.0.2/

Take a look at what's it that line. What command? Is the command properly written syntax wise?

R.

Offline

#7 2008-03-23 16:43:28

Kobussie
Member
From: NL - Groningen
Registered: 2007-04-06
Posts: 43
Website

Re: [SOLVED] php-mysql connection

My php code (see original post) is:

$connection = mysql_connect($db_host, $db_username, $db_password);

$db_host, $db_username and $db_password are defined in db_login.php:

$db_host='127.0.0.1';
$db_database='bandjes';
$db_username='ko';
$db_password='Kobus';

As far as I can see this is correct; I typed it over from my book 'learning PHP & MySQL'.

The httpd_error says: <a href='function.mysql-connect'>function.mysql-connect</a>;
does this mean that the function is not found?
It also says: "Lost connection to MySQL server". Does this mean that there was a connection?

This is all new to me, and I don't understand it!

Offline

#8 2008-03-23 16:51:52

ralvez
Member
From: Canada
Registered: 2005-12-06
Posts: 1,718
Website

Re: [SOLVED] php-mysql connection

OK.
I'm going out now but when I return I'll put your code in my server and give it a test ... see what I find.

R.

edit: I did a quick test in my server and get no errors and no output messages either ... something is not wright.

Last edited by ralvez (2008-03-23 17:00:48)

Offline

#9 2008-03-23 20:11:20

Shaika-Dzari
Member
From: Québec, Canada
Registered: 2006-04-14
Posts: 436
Website

Re: [SOLVED] php-mysql connection

Kobussie wrote:

$db_host='127.0.0.1';
$db_database='bandjes';
$db_username='ko';
$db_password='Kobus';

Try to replace $db_host='127.0.0.1'; by $db_host='localhost';
Be sure mysqld is running.:P

Verify php and mysql configuration with this simple php script:

<?php
    php_info();
?>

it must have a mysql section with your current configuration.

@+

Offline

#10 2008-03-23 20:43:06

Kobussie
Member
From: NL - Groningen
Registered: 2007-04-06
Posts: 43
Website

Re: [SOLVED] php-mysql connection

Hi Shaika-Dzari!

Thanks for the reply.

1. I replaced 127.0.0.1 with localhost; the server returns an empty page. The HTML code stops at the <body> tag. So that's not the solution.

2. Here is my text from php_info() :

...
...
the MySQL part:

mysql

MySQL Support    enabled
Active Persistent Links     0
Active Links     0
Client API version     5.0.51a
MYSQL_MODULE_TYPE     external
MYSQL_SOCKET     /tmp/mysql.sock
MYSQL_INCLUDE     -I/usr/include/mysql
MYSQL_LIBS     -L/usr/lib -lmysqlclient

Directive    Local Value    Master Value
mysql.allow_persistent    On    On
mysql.connect_timeout    60    60
mysql.default_host    no value    no value
mysql.default_password    no value    no value
mysql.default_port    no value    no value
mysql.default_socket    no value    no value
mysql.default_user    no value    no value
mysql.max_links    Unlimited    Unlimited
mysql.max_persistent    Unlimited    Unlimited
mysql.trace_mode    Off    Off
...
...
I can see nothing wrong here, but I do not completely understand all...

Hope someone does...

Offline

#11 2008-03-23 21:33:29

ralvez
Member
From: Canada
Registered: 2005-12-06
Posts: 1,718
Website

Re: [SOLVED] php-mysql connection

I have thrown together a piece of code to test your scenario. Here it is:

<html>
       <head><title>test</title></head>
       <body>
<?php
require_once('db_login.php');
// connect

$link = mysql_connect ($db_host, $db_username, $db_password);

if (!$link){
    echo "Could not connect to database".mysql_error();
}

echo "<div align=\"center\">";
echo "<p>Connected</p>";
echo "</div>";

mysql_select_db($link, $db_database);
// enter a querry for mysql
 $result = mysql_query($link, "select * from users;");
if (!$result){
    echo "Could not perform the specified querry".mysql_error();
}

mysql_close($link);
echo "<p>All done</p>"

?>

</body>
</html>

As it goes it will report if you are connected, if your querry was taken and when it disconnects.
BTW, I'm reusing your db_login.php so the only thing you have to do is drop this file in and test.

Hope this helps.

edit: with regards to the blank page and stopping at the <body> tag: are you sure you un-commented the line extension=mysql.so in /etc/php/php.ini?

Last edited by ralvez (2008-03-23 21:49:58)

Offline

#12 2008-03-23 22:28:08

Kobussie
Member
From: NL - Groningen
Registered: 2007-04-06
Posts: 43
Website

Re: [SOLVED] php-mysql connection

Thanks.

I'll try this tomorrow....late at night over here (Europe!)

Offline

#13 2008-03-24 17:13:12

Kobussie
Member
From: NL - Groningen
Registered: 2007-04-06
Posts: 43
Website

Re: [SOLVED] php-mysql connection

Hi again!

I put your code into my document_root. This is wat my server returns:

"; echo "

Connected
"; echo ""; mysql_select_db($link, $db_database); // enter a querry for mysql $result = mysql_query($link, "select * from users;"); if (!$result){ echo "Could not perform the specified querry".mysql_error(); } mysql_close($link); echo "

All done
" ?>

I got the idea that: 1. the connection was succesfull
2. there's someting wrong with the code a little further on. I'll try to correct it...

Perhaps you can have a look at the result yourself, if you surf to 82.95.103.220. I put a link for you at the bottom of the index page (below all the the Dutch text that I don't think you understand....). Let me know if that works! I'll keep the server running.

Ko.

Offline

#14 2008-03-24 17:40:58

Spider.007
Member
Registered: 2004-06-20
Posts: 1,175

Re: [SOLVED] php-mysql connection

Did you remove the following from you my.cnf file?

# Don't listen on a TCP/IP port at all. This can be a security enhancement,
# if all processes that need to connect to mysqld run on the same host.
# All interaction with mysqld must be made via Unix sockets or named pipes.
# Note that using this option without enabling named pipes on Windows
# (via the "enable-named-pipe" option) will render mysqld useless!
# 
skip-networking

Offline

#15 2008-03-24 17:49:25

Kobussie
Member
From: NL - Groningen
Registered: 2007-04-06
Posts: 43
Website

Re: [SOLVED] php-mysql connection

Hallo Spider.007,

I just removed the # before skip-networking. (Tried that before).
Doesn't make any difference...

Offline

#16 2008-03-24 17:52:02

Spider.007
Member
Registered: 2004-06-20
Posts: 1,175

Re: [SOLVED] php-mysql connection

That's kind of strange since, if you uncomment it, it definitely shouldn't work anymore. The '#' should be there so put it back. You might try using $db_host='localhost' instead of an IP

Offline

#17 2008-03-24 17:52:31

ralvez
Member
From: Canada
Registered: 2005-12-06
Posts: 1,718
Website

Re: [SOLVED] php-mysql connection

Kobussie wrote:

Hi again!

I put your code into my document_root. This is wat my server returns:

"; echo "

Connected
"; echo ""; mysql_select_db($link, $db_database); // enter a querry for mysql $result = mysql_query($link, "select * from users;"); if (!$result){ echo "Could not perform the specified querry".mysql_error(); } mysql_close($link); echo "

All done
" ?>

I got the idea that: 1. the connection was succesfull
2. there's someting wrong with the code a little further on. I'll try to correct it...

Perhaps you can have a look at the result yourself, if you surf to 82.95.103.220. I put a link for you at the bottom of the index page (below all the the Dutch text that I don't think you understand....). Let me know if that works! I'll keep the server running.

Ko.

It looks like something got mangled hmm
It seems that the file is parsing "raw code" on the screen.
One thing is for sure, you are connecting and you are disconnecting.
Did you change the query? The query line I put was just an example and you should make sure it makes sense in the context of your DB.

p.s. I went to http://82.95.103.220 but it never loads.

R.

Offline

#18 2008-03-24 17:54:46

Kobussie
Member
From: NL - Groningen
Registered: 2007-04-06
Posts: 43
Website

Re: [SOLVED] php-mysql connection

Ralvez,

edit: with regards to the blank page and stopping at the <body> tag: are you sure you un-commented the line extension=mysql.so in /etc/php/php.ini?

yes, shure, I did!

Offline

#19 2008-03-24 18:06:55

Kobussie
Member
From: NL - Groningen
Registered: 2007-04-06
Posts: 43
Website

Re: [SOLVED] php-mysql connection

p.s. I went to http://82.95.103.220 but it never loads.

R.

Sorry; I had to change the router-configuration for that. I just did; now port 80 is open to the 'outside world' - do I need to open it for udp also, or is tcp enough?
Please try again.

Ko.

Offline

#20 2008-03-24 18:08:14

Spider.007
Member
Registered: 2004-06-20
Posts: 1,175

Re: [SOLVED] php-mysql connection

Dude, you uploaded the testscript as .html file which will never get it parsed. You really need to start learning some basics wink

Offline

#21 2008-03-24 18:18:19

Kobussie
Member
From: NL - Groningen
Registered: 2007-04-06
Posts: 43
Website

Re: [SOLVED] php-mysql connection

Oops..I see.
I'm just a beginner, as I said in the original post...

I've changed it right now; this is what I get:

Could not connect to databaseLost connection to MySQL server at 'reading initial communication packet', system error: 0

Connected
Could not perform the specified querryLost connection to MySQL server at 'reading initial communication packet', system error: 0

All done

Offline

#22 2008-03-24 19:52:37

Shaika-Dzari
Member
From: Québec, Canada
Registered: 2006-04-14
Posts: 436
Website

Re: [SOLVED] php-mysql connection

Hello!

Did you create your user and crypt your password with mysql password() function?

If you change the login and use your root account, does it work?

Are you sure mysql is running?

ps ax | grep mysqld

@+

Last edited by Shaika-Dzari (2008-03-24 20:01:25)

Offline

#23 2008-03-24 20:14:49

Kobussie
Member
From: NL - Groningen
Registered: 2007-04-06
Posts: 43
Website

Re: [SOLVED] php-mysql connection

Hello!

Glad you're back online!
On the commandline I can log in with username & password, and display my database with "select * from lijst".  So that works.
If I change username to "root" and password to roots password in db_login.php, no changes...

ps ax | grep mysqld gives:

6362 ?        S      0:00 /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql
6392 ?        Sl     0:01 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --user=mysql --pid-file=/var/lib/mysql/Kobus

So mysqld is running. What about mysqld_safe?? I'm not shure....

Last edited by Kobussie (2008-03-24 20:23:11)

Offline

#24 2008-03-24 21:05:19

Shaika-Dzari
Member
From: Québec, Canada
Registered: 2006-04-14
Posts: 436
Website

Re: [SOLVED] php-mysql connection

Re! wink
mysqld_safe is normal.

But this line is different from mine:

6392 ?        Sl     0:01 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --user=mysql --pid-file=/var/lib/mysql/Kobus

Do you skip arch configure script?
Can you give me the result of this command:

cat /etc/my.cnf

@+

Offline

#25 2008-03-24 21:20:41

Kobussie
Member
From: NL - Groningen
Registered: 2007-04-06
Posts: 43
Website

Re: [SOLVED] php-mysql connection

smile

Here's my.cnf: (sorry but it's quite long!! I delete the first part, and the part about "replication slave'; all commented out of course!)
So here you go:

[client]
#password    = your_password
port        = 3306
socket        = /tmp/mysql.sock

# Here follows entries for some specific programs

# The MySQL server
[mysqld]
port        = 3306
socket        = /tmp/mysql.sock
datadir        = /var/lib/mysql
skip-locking
key_buffer = 16M
max_allowed_packet = 1M
table_cache = 64
sort_buffer_size = 512K
net_buffer_length = 16K
myisam_sort_buffer_size = 8M

# Don't listen on a TCP/IP port at all. This can be a security enhancement,
# if all processes that need to connect to mysqld run on the same host.
# All interaction with mysqld must be made via Unix sockets or named pipes.
# Note that using this option without enabling named pipes on Windows
# (via the "enable-named-pipe" option) will render mysqld useless!
#
# skip-networking

# Replication Master Server (default)
# binary logging is required for replication
#log-bin

# required unique id between 1 and 2^32 - 1
# defaults to 1 if master-host is not set
# but will not function as a master if omitted
server-id    = 1


# Point the following paths to different dedicated disks
#tmpdir        = /tmp/       
#log-update     = /path-to-dedicated-directory/hostname

# Uncomment the following if you are using BDB tables
#bdb_cache_size = 4M
#bdb_max_lock = 10000

# Uncomment the following if you are using InnoDB tables
#innodb_data_home_dir = /var/lib/mysql
#innodb_data_file_path = ibdata1:10M:autoextend
#innodb_log_group_home_dir = /var/lib/mysql
#innodb_log_arch_dir = /var/lib/mysql
# You can set .._buffer_pool_size up to 50 - 80 %
# of RAM but beware of setting memory usage too high
#innodb_buffer_pool_size = 16M
#innodb_additional_mem_pool_size = 2M
# Set .._log_file_size to 25 % of buffer pool size
#innodb_log_file_size = 5M
#innodb_log_buffer_size = 8M
#innodb_flush_log_at_trx_commit = 1
#innodb_lock_wait_timeout = 50

[mysqldump]
quick
max_allowed_packet = 16M

[mysql]
#no-auto-rehash
# Remove the next comment character if you are not familiar with SQL
#safe-updates

[isamchk]
key_buffer = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M

[myisamchk]
key_buffer = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M

[mysqlhotcopy]
interactive-timeout

Hope that I didn't overlook essential lines...

Ko

Offline

Board footer

Powered by FluxBB