0

I'm new to using xampp, until a week ago I used a hosted server for php development. I'm having what is probably a newbie problem and would appreciate any advice offered.

I've got phpmyadmin working and have created a database and a user with full permissions. All the things I would do on my hosted server. When I try to connect using mysqli, I get the following error:

Warning: mysqli::mysqli(): (HY000/1045): Access denied for user 'cromwell'@'localhost' (using password: YES) in C:\xampp\htdocs\cromwell\dbconnect.php on line 2 Connect failed: Access denied for user 'cromwell'@'localhost' (using password: YES)

The user account privileges were set up with the following SQL:

REVOKE ALL PRIVILEGES ON *.* FROM 'cromwell'@'localhost'; 
GRANT ALL PRIVILEGES ON *.* TO 'cromwell'@'localhost' REQUIRE NONE WITH GRANT OPTION MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0;

the connection script is as follows:

<?
$mysqli = new mysqli("localhost", "cromwell", 'password', "cromwell");

if ($mysqli->connect_errno) {
    printf("Connect failed: %s\n", $mysqli->connect_error);
    exit();
}

?>

both the user and database are named cromwell as per phpmyadmin's set-up when I created the user.

I assume I'm missing something simple in either the user setup or how I'm addressing the database from the script.

Thanks for any help or advice.

Isaac Bennetch
  • 11,830
  • 2
  • 32
  • 43
franki
  • 123
  • 2
  • 13
  • 1
    You did not add a password when you recreated the account, but you are using one in the PHP script connection – RiggsFolly Sep 19 '16 at 18:58
  • Thankyou! I'd set a password for the user in phpmyadmin but not for the user's connection to the database. Like I said, newbie error. – franki Sep 19 '16 at 19:27

2 Answers2

-1
Sql connection using PDO Methode:

<?php
/* Database config */
$db_host        = 'localhost';
$db_user        = 'root';
$db_pass        = '';
$db_database    = 'sales'; 

/* End config */

$db = new PDO('mysql:host='.$db_host.';dbname='.$db_database, $db_user, $db_pass);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

?>
Hasnain Mehmood
  • 407
  • 5
  • 4
-2
                    Sql connection using MySql Methode:

                    ?php
                    $database="gzone";
                    $username="root";
                    $password="";
                    $conn = mysql_connect("","$username","$password");
                    if(!$conn)
                    {
                    die('<h2>connection can not be established properly :</h2>'. mysql_error());
                    }
                    $db=mysql_select_db($database);
                    if(!$db)
                    {
                    die('<h2>fatal error in database</h2>'.mysql_error());
                    }
                    ?>
                ///////////////////////////////////////////////////////////////////
                LOgin in PhP:

                    <?php 
                    session_start();
                    include('conn.php');
                    $email = $_POST['email'];
                    $password = $_POST['password'];
                    $sql = mysql_query("SELECT user_type,email FROM guser WHERE email= '$email' and password='$password'") or die(mysql_error());
                    if(mysql_num_rows($sql) ==1){
                        $result = mysql_fetch_assoc($sql);
                        $user_type= $result['user_type'];

                        session_start();  
                        if($user_type!='admin'){

                            $_SESSION['uname'] = $email;
                            $_SESSION['user_type']= $user_type;
                            header("location:index.php");
                            exit();

                        }
                        else{

                            $_SESSION['uname'] = $email;
                            $_SESSION['user_type'] = $user_type;
                            header("location: ./admin_data/admin.php");
                            exit();

                        }
                        }else{
                        header("location:index.php?message=invalid-user");
                                exit(); 
                    }



                    ?>


            ////////////////////////////////////////////////////
            SignUp in Php:

            <?php
            include_once('conn.php');
            $name=$_POST['name'];
            $password=$_POST['password'];
            $email=$_POST['email'];
            $cnic=$_POST['cnic'];
            $phoneno=$_POST['phon_no'];
            $visacard=$_POST['visa'];
            $DOB=$_POST['dob'];

            $q="insert into guser(username,password,email,cnic_no,phone_no,visa_card,dob,user_type)values('".$name."','".$password."','".$email."','".$cnic."','".$phoneno."','".$visacard."','".$DOB."','user')";
            if(!mysql_query($q,$conn))
            {
            die('fatal error'.mysql_error());
            }
            header('location: GAMMING ZONE.COM.php?signup=t');
            exit();

            ?>
        //////////////////////////////////////////////////
        Java Script Validation is:

        <script>

        function validlogin(){


            var email1_value = document.getElementById("email1").value;
            var p1_value = document.getElementById("password").value;
             if(!email1_value)
                {                                                             
                    alert("Invalid email!");               
                    return false;
                }
                if(p1_value.length==0)
                {
                    alert("please enter password!");
                    return false;
                }


            }

         function validation(){

             var name_value = document.getElementById("name").value;
               var p_value = document.getElementById("password2").value;
               var cp_value = document.getElementById("retpassword").value;
                var email_value = document.getElementById("email").value;
                var cnic_value = document.getElementById("nic").value;
                var phone_value = document.getElementById("phone").value;
                var visa_value = document.getElementById("visa").value;
                var DOB_value = document.getElementById("dob").value;

                var reg_nic = /^[1-9]{1}[0-9]{4}\-[0-9]{7}\-[0-9]{1}$/;
                var reg_ph = /^03[0-9]{2}\-[0-9]{7}$/;
                var reg_visa=/^[1-9]{1}[0-9]{15}$/;


                if(name_value.length==0){

                    alert("Name lenght is not okay!");
                    return false;
                }
                //alert(p_value);
                if(p_value.length==0)
                {
                    alert("please enter password!");
                    return false;
                }
                    if(!cp_value)
                {
                    alert("must retype password!");
                    return false;
                    }

                if(p_value!=cp_value){

                    alert("Password dose not match!");
                    return false;
                }
               if(!email_value)
                {                                                             
                    alert("Invalid email!");               
                    return false;
                }
                if(reg_nic.test(cnic_value)==false){
                    alert("Invalid Nic!");
                    return false;
                }
                if(reg_ph.test(phone_value)==false){
                     alert("Invalid Mobile no!");
                    return false;
                }
                 if(reg_visa.test(visa_value)==false){                                                             
                    alert("Invalid visa card number!");               
                    return false;

                }
                 if(!DOB_value){                                                             
                    alert("Invalid date of birth!");              
                    return false;
                }
            }




            function search(){


            var search = document.getElementById("sname").value;

             if(search.length==0)
                {                                                             
                    alert("Search for games...!");               
                    return false;
                }
                }
        </script>

    /////////////////////////////////////////////////////////
    Query string Back to action page through Get Function:

     <?php
         session_start();
         if(isset($_SESSION['uname']) && isset($_SESSION['user_type']) && $_SESSION['user_type']=='user')
         {
             echo'<h3 style="color:white">wellcome '.$_SESSION['uname'].' </h3>';
             }
             if(isset($_GET['signup'])){
        echo '<h3 style="color:white">you are successfully signup.</h3>';

    }

        ?>

 <div class="main-body-section-left-title"><h2>MANAGE USER'S</h2></div>
    <?php
    include_once('../conn.php');


    $q=mysql_query("select * from guser");
?>
<form action="manage_user.php" method="post" style="margin-left:-150px;">
<table class="table_decoration" border="7px" >
    <tr  class="row_decoration">
        <th style="color:#00ff00;">USER NAME</th>
        <th style="color:#00ff00;">EMAIL</th>
        <th style="color:#00ff00;">PASSWORD</th>
        <th style="color:#00ff00;">CNIC_NO</th>
        <th style="color:#00ff00;">PHONE_NO</th>
        <th style="color:#00ff00;">VISA CARD NO</th>
        <th style="color:#00ff00;">DOB</th>
        <th style="color:#00ff00;">USER TYPE</th>
        <th style="color:#00ff00;">DELETE</th>
        <th style="color:#00ff00;">UPDATE</th>
        <th><a href="add_user.php"><p style="color:#00ff00;">ADD</p></a></th>
    </tr>
    <?php

    while($row=mysql_fetch_array($q)){
    echo ' <tr class="row_dec">
                <td style="color:white">'.$row['username'].'</td>
                <td style="color:white">'.$row['email'].'</td>
                <td style="color:white">'.$row['password'].'</td>
                <td style="color:white">'.$row['cnic_no'].'</td>
                <td style="color:white">'.$row['phone_no'].'</td>
                <td style="color:white">'.$row['visa_card'].'</td>
                <td style="color:white">'.$row['dob'].'</td>
                <td style="color:white">'.$row['user_type'].'</td>
                <td>
                 <a href="#?email='.$row['email'].'" onClick="if(window.confirm(\'Are you want to delete this user? \'))
                { window.location =\'delete_user.php?email='.$row['email'].'\'; }">
                <p style="color:yellow;">Delete</p></a>
                </td>
                <td> <a href="update_user.php?email='.$row['email'].'"><p style="color:yellow">Update </p></a></td>
            </tr>';

    }

    ?>
</table>

      </form> 

         </div>
Hasnain Mehmood
  • 407
  • 5
  • 4