473,406 Members | 2,705 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Access denied for user to database

sIncognito
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.
Apr 6 '10 #1
13 4259
Markus
6,050 Expert 4TB
What is the full error message? It'll include something like 'USING PASSWORD yes'.
Apr 6 '10 #2
Dormilich
8,658 Expert Mod 8TB
it also looks like there’s no login name passed.
Apr 6 '10 #3
Atli
5,058 Expert 4TB
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.
Apr 6 '10 #4
Markus
6,050 Expert 4TB
@Dormilich
I read it as "'user'@'localhost'" - woops :P
Apr 6 '10 #5
FULL ERROR MESS.

Database Selected:
Query: CREATE DATABASE Pet_Catalog
Results
Error: Access denied for user ''@'localhost' to database 'pet_catalog'
Apr 7 '10 #6
i am using the admin (root) account.
Apr 7 '10 #7
Markus
6,050 Expert 4TB
Can we see the code-in-question, please?
Apr 7 '10 #8
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. /*Program:    mysql_send.php
  3.  *Desc:    PHP program that sends an SQL query to the
  4.  *        MySQL server and displays the results.
  5.  */
  6. echo "<html>
  7.     <head><title>SQL Query Sender</title></head>
  8.     <body>";
  9. if(ini_get("magic_quotes_gpc") == "1")
  10. {
  11.    $_POST['query'] = stripslashes($_POST['query']);
  12. }
  13. $host="localhost";
  14. $user=" root";
  15. $password=" *******";
  16.  
  17. /* Section that executes query and displays the results */
  18. if(!empty($_POST['form']))
  19. {
  20.   $cxn = mysqli_connect($localhost,$root,$******,
  21.                 $_POST['database']);
  22.   $result = mysqli_query($cxn,$_POST['query']);
  23.   echo "Database Selected: <b>{$_POST['database']}</b><br>
  24.       Query: <b>{$_POST['query']}</b>
  25.       <h3>Results</h3><hr>";
  26.   if($result == false)
  27.   {
  28.      echo "<h4>Error: ".mysqli_error($cxn)."</h4>";
  29.   }
  30.   elseif(@mysqli_num_rows($result) == 0)
  31.   {
  32.      echo "<h4>Query completed.
  33.         No results returned.</h4>";
  34.   }
  35.   else
  36.   {
  37.    /* Display results */
  38.     echo "<table border='1'><thead><tr>";
  39.     $finfo = mysqli_fetch_fields($result);
  40.     foreach($finfo as $field)
  41.     {
  42.        echo "<th>".$field->name."</th>";
  43.       }
  44.     echo "</tr></thead>
  45.         <tbody>";
  46.     for ($i=0;$i < mysqli_num_rows($result);$i++)
  47.     {
  48.        echo "<tr>";
  49.        $row = mysqli_fetch_row($result);
  50.        foreach($row as $value)
  51.        {
  52.           echo "<td>".$value."</td>";
  53.        }
  54.        echo "</tr>";
  55.     }
  56.     echo "</tbody></table>";
  57.   }
  58.  /* Display form  with only buttons after results */
  59.   $query = str_replace("'","%&%",$_POST['query']);
  60.   echo "<hr><br>
  61.     <form action='{$_SERVER['PHP_SELF']}' method='POST'>
  62.     <input type='hidden' name='query' value='$query'>
  63.     <input type='hidden' name='database'
  64.          value={$_POST['database']}>
  65.     <input type='submit' name='queryButton'
  66.          value='New Query'>
  67.     <input type='submit' name='queryButton'
  68.          value='Edit Query'>
  69.     </form>";
  70.   exit();
  71. }
  72.  
  73. /* Displays form for query input */
  74. if (@$_POST['queryButton'] != "Edit Query")
  75. {
  76.    $query = " ";
  77. }
  78. else
  79. {
  80.    $query = str_replace("%&%","'",$_POST['query']);
  81. }
  82. ?>
  83. <form action="<?php echo $_SERVER['PHP_SELF'] ?>"
  84.     method="POST">
  85. <table>
  86.  <tr><td style='text-align: right; font-weight: bold'>
  87.        Type in database name</td>
  88.     <td><input type="text" name="database"
  89.          value=<?php echo @$_POST['database'] ?> ></td>
  90.  </tr>
  91.  <tr><td style='text-align: right; font-weight: bold'
  92.         valign="top">Type in SQL query</td>
  93.      <td><textarea name="query" cols="60"
  94.         rows="10"><?php echo $query ?></textarea></td>
  95.  </tr>
  96.  <tr><td colspan="2" style='text-align: center'>
  97.        <input type="submit" value="Submit Query"></td>
  98.  </tr>
  99. </table>
  100. <input type="hidden" name="form" value="yes">
  101. </form>
  102. </body></html>
Apr 7 '10 #9
Markus
6,050 Expert 4TB
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.
Apr 7 '10 #10
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...
Apr 7 '10 #11
Dormilich
8,658 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. $host="localhost";
  2. $user=" root";
  3. $password=" *******";
  4.  
  5. if(!empty($_POST['form']))
  6. {
  7.   $cxn = mysqli_connect($localhost,$root,$******,
  8.  
Apr 7 '10 #12
so how do I define this to allow access to the localhost?
Apr 8 '10 #13
Dormilich
8,658 Expert Mod 8TB
I’d go for
Expand|Select|Wrap|Line Numbers
  1. $host="localhost";
  2. $user="root";
  3. $password=" *******";
  4.  
  5. if(!empty($_POST['form']))
  6. {
  7.   $cxn = mysqli_connect($localhost,$user,$******,
  8.  
Apr 8 '10 #14

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Randell D. | last post by:
Folks, I use PHP to write my form data to MySQL. I have a database with about ten tables. I'm trying to fill one table with some dummy data (its a contact manager table holding names of...
0
by: Andreas Reuleaux | last post by:
What works for me with mysql 3.23.49 of Debian GNU/Linux (Woody), does not any more with mysql 3.23.56 of fink (current-stable, for fink cf. fink.sf.net) - I am not sure, if this problem is caused...
8
by: John | last post by:
Hello. I am currently working through a book on Dreamweaver and using PHP. I am having a little trouble with setting up the database though. I have php 4.2.3 and MySQL 4.0.20a. I am running...
5
by: Jim Andersen | last post by:
Hi, I made a DTS-package and it works when I execute it manually, but when it is run by the SQL Server Agent, it fails. I have use the guide to create a maintenance plan. That doesn't work so...
3
by: vincentw56 | last post by:
This is the first time I have tried this. What we have is a com object the generates keys. A developer here wrote a .Net wrapper in C# for this com object. I am trying to call her dll wrapper...
4
by: RichB | last post by:
Hello, The .NET application I have just installed at a client web site is throwing a strange error. At first the applications works without any problems, but after 10 mins or so Access denied...
4
by: geodev | last post by:
I have developed a small application using ASP.NET and VB.NET on my development machine it works great. When I copy across the files manually to my test machine and create a Virtual Directory all...
2
by: Johan Johansson (Sweden) | last post by:
I can't connect to our database (seperate database server) from within my webservice. I cannot understand why.!!! * I have configured IIS not to allow anonymous access and to use "windows...
3
by: sammyloo | last post by:
Hi all, I'm experiencing a problem using ASP.NET web services to access a different server with SQL Server 2000 database. And I get the error of the following Exception Details:...
8
by: ajos | last post by:
hi frnds, im trying to convert my servlets database configuration from ms access to mysql database.however im getting some error like no driver found exception. to verify this error ive...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.