Hi All - I am trying create classes for db connection and I am getting the following error

Thanks in advance

D

Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'XXXXXXX_david'@'localhost' (using password: YES) in /home/djenning/public_html/dbconnection/connection.php on line 20
Cannot connect to the database

page: connection.php

<?php

class createConnection //create a class for make connection
{
    var $host="localhost";
    var $username="xxxxxxx_david";    // specify the sever details for mysql
    var $password="XXXXXXXX";
    var $database="databaseclass";
    var $myconn;

    function connectToDatabase() // create a function for connect database
    {

        $conn= mysql_connect($this->host,$this->username,$this->password);

        if(!$conn)// testing the connection
        {
            die ("Cannot connect to the database");
        }

        else
        {

            $this->myconn = $conn;

            echo "Connection established";

        }

        return $this->myconn;

    }

    function selectDatabase() // selecting the database.
    {
        mysql_select_db($this->database);  //use php inbuild functions for select database

        if(mysql_error()) // if error occured display the error message
        {

            echo "Cannot find the database ".$this->database;

        }
         echo "Database selected..";       
    }

    function closeConnection() // close the connection
    {
        mysql_close($this->myconn);

        echo "Connection closed";
    }

}

?>

I am checking the connection here

Page: index.php

<?php

    include ('connection.php');

    $connection = new createConnection(); //i created a new object

    $connection->connectToDatabase(); // connected to the database

    echo "<br />"; // putting a html break

    $connection->selectDatabase();// closed connection

    echo "<br />";

    $connection->closeConnection();
?>

You just need this:

  1. <?php
  2. $connect = mysql_connect("localhost","user","password") or die(mysql_error());
  3. mysql_select_db("database");
  4. ?>

You don't need to put all the functions

Hi Nuno - Thanks, I am trying to write functions and some OOP. How would I do this using OOP.

Thanks

David

there's some sort of a problem in your mysql database password. can you check the settings in your phpMyAdmin for that certain database setting?

Hi masterjiraya - The password is hidden i.e xxxxxxx for security on the forum

Thanks

David

Hi all - still having problems with the below code, I have now changed from the previous code as it was just testing the connection.
I have checked the DB name and Password and they are correct.

Thanks in advance

David

<?php
$connect = mysql_connect("localhost","djenning_david","password") or die(mysql_error());
mysql_select_db("djenning_databaseclass");


                        $title = $_POST['title'];
                        $firstname = $_POST['firstname'];
                        $lastname = $_POST['lastname'];
                        $babysage = $_POST['babysage'];
                        $address1 = $_POST['address1'];
                        $address2 = $_POST['address2'];
                        $address3 = $_POST['address3'];                                             
                        $town = $_POST['town'];
                        $county = $_POST['county'];
                        $postcode = $_POST['postcode'];
                        $email = $_POST['email'];                                                                                                                                                                                               
                        $heard = $_POST['heard'];

$sql="INSERT INTO data (id,title,firstname,lastname,babysage,address1,address2,address3,town,county,postcode,email,heard) 
VALUES ('NULL', '$title','$firstname','$lastname','$babysage','$address1','$address2','$address3','$town','$county','$postcode','$email','$heard')";



mysql_close($con);
?>

You are not executing your query. Put the following on line 22:

$result = mysql_query($sql) or die(mysql_error());

I also suggest you remove the single quotes around NULL.

Hi Pritaeas - I am still getting the following error on submit any additional ideas?

<br /><b>Warning</b>: mysql_connect() [<a href='function.mysql-connect'>function.mysql-connect</a>]: Access denied for user 'user_david'@'localhost' (using password: YES) in <b>/home/djenning/public_html/hipp-test/insert.php</b> on line <b>2</b><br />Access denied for user 'user_david'@'localhost' (using password: YES)

Thanks in advance

David

<?php
$conn = mysql_connect("localhost","user_david","password","djenning_databaseclass");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  exit
  }


                        $title = $_POST['title'];
                        $firstname = $_POST['firstname'];
                        $lastname = $_POST['lastname'];
                        $babysage = $_POST['babysage'];
                        $address1 = $_POST['address1'];
                        $address2 = $_POST['address2'];
                        $address3 = $_POST['address3'];                                             
                        $town = $_POST['town'];
                        $county = $_POST['county'];
                        $postcode = $_POST['postcode'];
                        $email = $_POST['email'];  
                        $heard = $_POST['heard'];

                        $sql="INSERT INTO username (id,title,firstname,lastname,babysage,address1,address2,address3,town,county,postcode,email,heard) 
VALUES (NULL, '$title','$firstname','$lastname','$babysage','$address1','$address2','$address3','$town','$county','$postcode','$email','$heard')";

$result = mysql_query($sql) or die(mysql_error());

mysql_close($con);
?>

You're using mysqli_connect_error and mysqli_connect_errno, but you're using mysql_connect for the db connection. You really should be using all mysqli for this since mysql has been deprecated from PHP.

This problem is related to php.ini file settings.Nothing else

Member Avatar for diafol

Yep, the mix of mysql and mysqli is all wrong as noted by pixelsoul. However, the error seems to be coming from the first line - the mysql_connect itself.

Your parameters seem to be wrong, for

mysql_connect: host, username, password - should be sufficient (the next accepted parameter should be a boolean, but very rarely used)

mysqli_connect: host, username, password, dbname ...

So it seems it 'should' work if you change mysql_connect() to mysqli_connect()

Be aware that you'll also need to change these to mysqli too:

$result = mysql_query($sql) or die(mysql_error());
mysql_close($con);

In addition, you don't clean your POST variables - if you use mysql - you'll need to use mysql_real_escape_string() or if you're using mysqli, use a parameterized / bind query.

Thanks, I am trying to write functions and some OOP. How would I do this using OOP.

MySQL is not capable of OOP, either use MySQLi or MySQL with a PDO wrapper

I dont know why my post has been downvoted.YOU SIMPLY CHECK WEATHER YOUR SERVER IS UP AND RUNNING OR NOT.

if you are using

$conn = mysql_connect("localhost","user_david","password","djenning_databaseclass");

this line, you have to put the password in clear type, other wise the server will kick the connection as yours is doing.
otherwise use something like

<?php
$dbhost='localhost';
$dbuser='auser';
$dbpass='abc';
$dbname='website'
?> 

as config,php and

<?php
$link=mysqli_connect($dbhost, $dbuser, $dbpass,$dbname) or die ('Error connecting to mysql');
?>

as connect.php

and include them at the top of your script

<?php

include('config.php');
include('connect.php');

    /* your  login script here */

?>
Member Avatar for diafol

I dont know why my post has been downvoted.YOU SIMPLY CHECK WEATHER YOUR SERVER IS UP AND RUNNING OR NOT.

That's not what you said:

This problem is related to php.ini file settings.Nothing else

I'm assuming you were downvoted as it was not very helpful. If you elaborated, perhaps members would have understood your intention. We all know that ini settings are important, so would you care to expand on this? To which ones were you referring?

@diafol it is understood if problem is in mysql he shall check that part .I have soloved this problem once in php.ini.

Member Avatar for diafol

I have soloved this problem once in php.ini

How? I'm curious. Perhaps if you could show the relevant part of php.ini and which values should be set. Thanks.

imti321 is probably talking about the default settings for the mysql connection. Setting the credentials in the php.ini would most likely not solve anything, you would just be setting the same credentials somewhere else that you are already trying to use in your code to connect to the MySQL database.

Dear davidjennings

Your code is a miss mash of different coding technics.

You are using the MySQLi connection method but as MySQL.

For example:

MySQL

$con = mysql_connect('host','user','password'); <-- Notice no database selected

http://php.net/manual/en/function.mysql-connect.php

Where as your line 2 of code snippet 2 shows this:

$conn = mysql_connect("localhost","user_david","password","djenning_databaseclass"); **<-- You cannot select a database during MySQL connect**

MySQLi

$con = mysqli_connect('host','user','password','database');

http://www.php.net/manual/en/mysqli.construct.php

Sp you must decide which you are going to be using. As you have stated you want to use OOP.

If this is the case you can use MySQLi, or as I have already mentioned PDO with MySQL.

Member Avatar for diafol

imti321 is probably talking about the default settings for the mysql connection. Setting the credentials in the php.ini would most likely not solve anything, you would just be setting the same credentials somewhere else that you are already trying to use in your code to connect to the MySQL database.

My thoughts. However, imti was complaining about getting downvoted. I was simply curious as to where he was leading with the cryptic post.

I was simply curious as to where he was leading with the cryptic post.

Ah, okay. I get you now.

maybe you can put in line 14 as $conn=mysqli_connect("$host","$username","$Password","/database name/");

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.