Jump to content

creating database


khalen

Recommended Posts

Sorry to be asking for help again but you were all so helpful with my last problem I thought you wouldn't mind helping again.

 

I am trying to connect to localhost and create a simple database.  This code is returning;

 

Error creating database: Access denied for user ''@'localhost' to database 'student_members'

 

It is connecting to database ok but will not create database called student_members

 

can anyone see what I have done wrong??

 

Thanks

 

 

 

<html>
<body>

<?php

//open connection to database
$con = mysql_connect("","","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
else
{
echo "Connection to localhost made<br/><br/>";
}

//create database for student members to login 
if (mysql_query("CREATE DATABASE student_members",$con))
{
echo "Database created<br/><br/>";
}
else
{
echo "Error creating database:  " . mysql_error();
}  

//close connection before ending program 
mysql_close($con); 

  ?>
  
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/99062-creating-database/
Share on other sites

http://ca.php.net/mysql_connect

 

Your connection parameters are empty:

 

$con = mysql_connect("","","");

 

 

Hi Helmet ... it connects fine.  I don't believe these paramaters have to be named.  without then they will automatically go to localhost.  The problem is it won't create database student_members.

 

 

 

mysql_connect(servername,username,password);

 

Parameter Description

servername Optional. Specifies the server to connect to. Default value is "localhost:3306"

username Optional. Specifies the username to log in with. Default value is the name of the user that owns the server process

password Optional. Specifies the password to log in with. Default is ""

 

got this from http://www.w3schools.com/php/php_mysql_connect.asp

 

or am I missunderstanding this??

Link to comment
https://forums.phpfreaks.com/topic/99062-creating-database/#findComment-506904
Share on other sites

There must be something I'm not understanding here .... the link I posted in my last post says to me that I do not have to include that info to connect.  In fact if I comment out the create student_member database code I get no error messages at all.  I assume therefore that it has connected. 

 

I have no reason for not including a username or passwork just thought it would be easier by not complicating things with usernames and passwords at this stage.

 

If you hadn't already gathered I am VERY new to this php and slq stuff.

Link to comment
https://forums.phpfreaks.com/topic/99062-creating-database/#findComment-506917
Share on other sites

CREATE DATABASE requires a user with CREATE privileges. An anonymous user won't have such privileges.

 

Start by giving your database server at least a password for the root user, then only use the root user to manage the database (create databases and create users/passwords.)

 

If you are creating an "installation" script, most hosts will only allow databases and users to be created through the hosting control panel anyway, so you should not get in the habit of expecting a php script to be able to do this.

Link to comment
https://forums.phpfreaks.com/topic/99062-creating-database/#findComment-506920
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.