Access denied for user to database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sIncognito
    New Member
    • Apr 2010
    • 7

    #1

    Access denied for user to database

    I have built an SQL query and sent it to the MySQL Server using a PHP program/script. Everything was working fine and the connection was good until I attempted to CREATE DATABASE. Then I got this error:

    Error: Access denied for user ''@'localhost' to database 'petcatalog'

    Does anyone know why I am not connecting and how I fix this?

    Thanks

    P.S.
    I know I can use the command prompt to build a database but I would like to complete this project of building a database using php scripts to connect with mysql server.
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    What is the full error message? It'll include something like 'USING PASSWORD yes'.

    Comment

    • Dormilich
      Recognized Expert Expert
      • Aug 2008
      • 8694

      #3
      it also looks like there’s no login name passed.

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        Note that you need to have the CREATE privilege for the database before you create it. Meaning that, before you create the database, a user with the GRANT privilege (like root) needs to issue a GRANT command, giving the user you are using in your scripts permission to create the database.

        Because of this, you are generally just better of creating the database using the admin user. You are going to have to use it to grant the privileges anyways.

        Comment

        • Markus
          Recognized Expert Expert
          • Jun 2007
          • 6092

          #5
          Originally posted by Dormilich
          it also looks like there’s no login name passed.
          I read it as "'user'@'localh ost'" - woops :P

          Comment

          • sIncognito
            New Member
            • Apr 2010
            • 7

            #6
            FULL ERROR MESS.

            Database Selected:
            Query: CREATE DATABASE Pet_Catalog
            Results
            Error: Access denied for user ''@'localhost' to database 'pet_catalog'

            Comment

            • sIncognito
              New Member
              • Apr 2010
              • 7

              #7
              i am using the admin (root) account.

              Comment

              • Markus
                Recognized Expert Expert
                • Jun 2007
                • 6092

                #8
                Can we see the code-in-question, please?

                Comment

                • sIncognito
                  New Member
                  • Apr 2010
                  • 7

                  #9
                  Code:
                  <?php
                  /*Program:	mysql_send.php
                   *Desc:	PHP program that sends an SQL query to the
                   *		MySQL server and displays the results.
                   */
                  echo "<html>
                  	<head><title>SQL Query Sender</title></head>
                  	<body>";
                  if(ini_get("magic_quotes_gpc") == "1")
                  {
                     $_POST['query'] = stripslashes($_POST['query']);
                  }
                  $host="localhost";
                  $user=" root";
                  $password=" *******";
                  
                  /* Section that executes query and displays the results */
                  if(!empty($_POST['form']))
                  {
                    $cxn = mysqli_connect($localhost,$root,$******,
                  			    $_POST['database']);
                    $result = mysqli_query($cxn,$_POST['query']);
                    echo "Database Selected: <b>{$_POST['database']}</b><br>
                  	  Query: <b>{$_POST['query']}</b>
                  	  <h3>Results</h3><hr>";
                    if($result == false)
                    {
                       echo "<h4>Error: ".mysqli_error($cxn)."</h4>";
                    }
                    elseif(@mysqli_num_rows($result) == 0)
                    {
                       echo "<h4>Query completed.
                  		No results returned.</h4>";
                    }
                    else
                    {
                     /* Display results */
                  	echo "<table border='1'><thead><tr>";
                  	$finfo = mysqli_fetch_fields($result);
                  	foreach($finfo as $field)
                  	{
                  	   echo "<th>".$field->name."</th>";
                        }
                  	echo "</tr></thead>
                  		<tbody>";
                  	for ($i=0;$i < mysqli_num_rows($result);$i++)
                  	{
                  	   echo "<tr>";
                  	   $row = mysqli_fetch_row($result);
                  	   foreach($row as $value)
                  	   {
                  	      echo "<td>".$value."</td>";
                  	   }
                  	   echo "</tr>";
                  	}
                  	echo "</tbody></table>";
                    }
                   /* Display form  with only buttons after results */
                    $query = str_replace("'","%&%",$_POST['query']);
                    echo "<hr><br>
                  	<form action='{$_SERVER['PHP_SELF']}' method='POST'>
                  	<input type='hidden' name='query' value='$query'>
                  	<input type='hidden' name='database'
                  		 value={$_POST['database']}>
                  	<input type='submit' name='queryButton'
                  		 value='New Query'>
                  	<input type='submit' name='queryButton'
                  		 value='Edit Query'>
                  	</form>";
                    exit();
                  }
                  
                  /* Displays form for query input */
                  if (@$_POST['queryButton'] != "Edit Query")
                  {
                     $query = " ";
                  }
                  else
                  {
                     $query = str_replace("%&%","'",$_POST['query']);
                  }
                  ?>
                  <form action="<?php echo $_SERVER['PHP_SELF'] ?>"
                  	method="POST">
                  <table>
                   <tr><td style='text-align: right; font-weight: bold'>
                  	   Type in database name</td>
                  	<td><input type="text" name="database"
                  		 value=<?php echo @$_POST['database'] ?> ></td>
                   </tr>
                   <tr><td style='text-align: right; font-weight: bold'
                  	    valign="top">Type in SQL query</td>
                       <td><textarea name="query" cols="60"
                  		rows="10"><?php echo $query ?></textarea></td>
                   </tr>
                   <tr><td colspan="2" style='text-align: center'>
                  	   <input type="submit" value="Submit Query"></td>
                   </tr>
                  </table>
                  <input type="hidden" name="form" value="yes">
                  </form>
                  </body></html>
                  Last edited by Markus; Apr 7 '10, 03:47 AM. Reason: added [code] tags

                  Comment

                  • Markus
                    Recognized Expert Expert
                    • Jun 2007
                    • 6092

                    #10
                    You do not have the variable $root defined (line #20); I believe you mean to use the variable $user (line #14). You should remove that leading space from your $user variable, too.

                    Comment

                    • sIncognito
                      New Member
                      • Apr 2010
                      • 7

                      #11
                      I should mention that I am very new at this.

                      what do you mean by the variable $root not being defined, and
                      using the variable $user?

                      I am learning as I go...

                      Comment

                      • Dormilich
                        Recognized Expert Expert
                        • Aug 2008
                        • 8694

                        #12
                        Code:
                        $host="localhost";
                        [B]$user=" root";[/B]
                        $password=" *******";
                        
                        if(!empty($_POST['form']))
                        {
                          $cxn = mysqli_connect($localhost,[B]$root[/B],$******,

                        Comment

                        • sIncognito
                          New Member
                          • Apr 2010
                          • 7

                          #13
                          so how do I define this to allow access to the localhost?

                          Comment

                          • Dormilich
                            Recognized Expert Expert
                            • Aug 2008
                            • 8694

                            #14
                            I’d go for
                            Code:
                            $host="localhost";
                            $user="root";
                            $password=" *******";
                             
                            if(!empty($_POST['form']))
                            {
                              $cxn = mysqli_connect($localhost,$user,$******,

                            Comment

                            Working...