Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-10-2011, 03:41 AM   PM User | #1
sonny
Regular Coder

 
sonny's Avatar
 
Join Date: Apr 2008
Location: United States
Posts: 567
Thanks: 88
Thanked 0 Times in 0 Posts
sonny can only hope to improve
errors with connecting code

Hi folks

Everything works on the result page, but saving to database, for some reason I get these errors on the result page, yet I know, all the database login info is correct.

PHP Code:
Warningmysql_connect() [function.mysql-connect]: Access denied for user 'XXXXX@'localhost (using passwordYESin /home/.../results.php on line 16
Warning
mysql_select_db(): supplied argument is not a valid MySQL-Link resource in /home/.../results.php on line 17
Warning
mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/.../results.php on line 31 
Below is my complete results page
PHP Code:
// Complete results.php
require("common.php");
ReadConfig("speedtest.cfg");

## Save the results of this test to the database, if enabled
if($config->{'database'}->{'enable'}) {
    
$ip_matches $config->{'database'}->{'ip_matches'};
    if( (! 
$ip_matches) || ($ip_matches && preg_match("/$ip_matches/",$_SERVER['REMOTE_ADDR'])) ) {
        
Debug("Saving to database");
        
$dbh mysql_connect(
            
$config->{'database'}->{'host'},
            
$config->{'database'}->{'user'},
            
$config->{'database'}->{'password'}
        );
        
$dbs mysql_select_db$config->{'database'}->{'database'}, $dbh);
        
$table $config->{'database'}->{'table'};
        
$ip $_SERVER['REMOTE_ADDR'];
        
$upspeed addslashes($_GET['upspeed']);
        
$downspeed addslashes($_GET['downspeed']);
        
$sql "
            INSERT INTO `$table`
            SET
                `ip_string` =  '$ip',
                `ip` = INET_ATON('$ip'),
                `timestamp` = NOW(),
                `upspeed` = '$upspeed',
                `downspeed` = '$downspeed'
        "
;
        
mysql_query($sql,$dbh);
    }


PHP Code:
// this function located in common.php
function ReadConfig($config_file) {
    global 
$config;
    
$lines file($config_file);
    foreach (
$lines as $line_num => $line) {
        
$line rtrim(preg_replace("/#.*/","",$line));
        if(
preg_match("/\[.*\]/"$line$parts)) {
            
$section $parts[0];
            
$section preg_replace("/[\[\]]/","",$section);
        } elseif (
preg_match("/=/",$line)) {
            list(
$var,$value) = split('=',$line);
            
$var preg_replace('/ $/','',$var);
            
$value preg_replace('/^ +/','',$value);
            
$config->{$section}->{$var} = $value;
        }
    }

and below is my settings from speedtest.cfg
Code:
## Set to 1 to enable
enable = 1
host = localhost
database = testdb
user = testuser
password = testpass
table = testtable

## Regular expression to match to save results to the database
ip_matches =

Last edited by sonny; 09-10-2011 at 03:44 AM..
sonny is offline   Reply With Quote
Old 09-10-2011, 05:51 AM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Don't even need to go over the code; the error is very clear:
Code:
Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'XXXXX@'localhost (using password: YES) in /home/.../results.php on line 16
Now, you specify that the user is valid, BUT! Are you sure its valid for 'XXXXX'@localhost and not just 'XXXXX'@%? These are distinct user accounts depending on if the connection is local or remote.
Fou-Lu is offline   Reply With Quote
Old 09-10-2011, 06:15 AM   PM User | #3
sonny
Regular Coder

 
sonny's Avatar
 
Join Date: Apr 2008
Location: United States
Posts: 567
Thanks: 88
Thanked 0 Times in 0 Posts
sonny can only hope to improve
Quote:
Originally Posted by Fou-Lu View Post
Don't even need to go over the code; the error is very clear:
Code:
Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'XXXXX@'localhost (using password: YES) in /home/.../results.php on line 16
Now, you specify that the user is valid, BUT! Are you sure its valid for 'XXXXX'@localhost and not just 'XXXXX'@%? These are distinct user accounts depending on if the connection is local or remote.
Hi Fou

That user error was just redacted, before posting
I just double checked again, login info is correct

I thought the problem was in the code somewhere

Sonny
sonny is offline   Reply With Quote
Old 09-10-2011, 06:20 AM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
So, the problem is fixed then yes? The only error you have here is with a connection to the database (although I am relying on the error reporting to state the error).
Fou-Lu is offline   Reply With Quote
Old 09-10-2011, 06:43 AM   PM User | #5
sonny
Regular Coder

 
sonny's Avatar
 
Join Date: Apr 2008
Location: United States
Posts: 567
Thanks: 88
Thanked 0 Times in 0 Posts
sonny can only hope to improve
Quote:
Originally Posted by Fou-Lu View Post
So, the problem is fixed then yes? The only error you have here is with a connection to the database (although I am relying on the error reporting to state the error).
No, I didn't get this yet, this is unreal, I have been working with complex functions all night, and the one thing that stumps me, is connecting to a simple database. I am going to close this notebook, make some coffee. and take another fresh look later.

Thanks
Sonny
sonny is offline   Reply With Quote
Old 09-10-2011, 07:02 AM   PM User | #6
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Then we go back to post #2.
The problem is the user you have specified does NOT have privileges on this database server. I mentioned as well that there is a difference between user@localhost and user@%. You must ensure that the user has been provided correctly for localhost and not just for %.
Also, make sure you dump your $config to verify that the data in it is correct. It doesn't look like it should be to me; these are using object dereferencing, but I don't see anywhere that these have been created as objects.

Last edited by Fou-Lu; 09-10-2011 at 07:04 AM..
Fou-Lu is offline   Reply With Quote
Old 09-10-2011, 07:24 AM   PM User | #7
sonny
Regular Coder

 
sonny's Avatar
 
Join Date: Apr 2008
Location: United States
Posts: 567
Thanks: 88
Thanked 0 Times in 0 Posts
sonny can only hope to improve
This works with another test script using that same database
its the same login info

PHP Code:
$db['host'] = 'localhost';
$db['user'] = 'testuser';
$db['pass'] = 'testpass';
$db['db'] = 'testdb'
Thanks
Sonny
sonny is offline   Reply With Quote
Old 09-10-2011, 07:32 AM   PM User | #8
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
And the dump of $config is?
Fou-Lu is offline   Reply With Quote
Old 09-10-2011, 07:33 AM   PM User | #9
sonny
Regular Coder

 
sonny's Avatar
 
Join Date: Apr 2008
Location: United States
Posts: 567
Thanks: 88
Thanked 0 Times in 0 Posts
sonny can only hope to improve
Quote:
Originally Posted by Fou-Lu View Post
And the dump of $config is?
How do I do that? I don't understand

Sonny
sonny is offline   Reply With Quote
Old 09-10-2011, 08:14 AM   PM User | #10
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
var_dump($config);
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
sonny (09-14-2011)
Old 09-14-2011, 08:13 AM   PM User | #11
sonny
Regular Coder

 
sonny's Avatar
 
Join Date: Apr 2008
Location: United States
Posts: 567
Thanks: 88
Thanked 0 Times in 0 Posts
sonny can only hope to improve
Quote:
Originally Posted by Fou-Lu View Post
var_dump($config);
Fixed it, just forgot to post back, it was something I had going for security
reasons that was striping certain symbols from the password.

Thanks as always
Sonny
sonny is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

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

Forum Jump


All times are GMT +1. The time now is 06:47 AM.


Advertisement
Log in to turn off these ads.