views:

194

answers:

6

I have a working PHP server. Now I want to use databases (MySQL or something similar). Is it possible to create a database from PHP?

I would like to emphasize that in my case I do not have any user-name and password which I can use to connect to MySQL server. I also do not have a control-panel where I could create a database or a table in an existing database.

ADDED:

I think SQLite is what I need. But if I try to create a new database from PHP I see that PHP tries to create a file in a directory which is different from the directory where my files are supposed to be. Then it reports that it is unable to create the database and I think it is because it tries to create in the directory to which I have no permission to write. Can I force PHP to create SQLite in a specific directory?

+4  A: 

Mysql,no. SqlLite is a possibility, you only need write permissions on the filesystem. www.sqlite.org/

mhughes
Don't forget to `chown apache:apache` and `chmod 0600` the database file. Or `chmod 0666` it if you don't have root access. But that introduces the ability to download the database if it's kept within the htdocs directory. Best to keep it in a folder outside of that tree.
amphetamachine
A: 

Yes, just fire a create database sql query from PHP http://dev.mysql.com/doc/refman/5.0/en/create-database.html

nitroxn
Read the question properly.
Matti Virkkunen
Congrats on your potential "peer pressure" badge :-)
klausbyskov
+10  A: 

I'm assuming you want to create an SQL database without access to a stand-alone database server. You could use SQLite, which is a library that creates a lightweight database in a single file without separate processes.

It's not quite as efficient as a standalone database server, however, so if you need performance, use a proper database server. It's not an unreasonable requirement for a high-performance web app.

Matti Virkkunen
I imagine that some people will inflate their own performance needs, so I'd like to point out that SQLite most likely will do just fine for you until you get *really* huge - like, hundreds or thousands of users at the same time.
Matchu
I've had problems using SQLite from PHP 4, and can't remember what caused it. Make sure you're using at least PHP 5 for SQLite. Also, SQLite3 is much better than SQLite2.
amphetamachine
+1  A: 

I would like to emphasize that in my case I do not have any user-name and password to which I can use to connect to MySQL server. I also do not have a control-panel where I could create a database or a table in an existing database.

This question is confusing to me, because if you're using MySQL you should be able to create a database, with a username and password to connect, using their command line admin tool. That's also the way to create a database or a table as well.

Are you saying you don't have access to the admin tool? You don't have an account? If so, you'll need to ask for them from the person who does have such access.

duffymo
A: 

Creating a database through PHP is possible, but for that you need a connection to the database, which needs a username/password pair or some kind of authentication. Unless your database allows anonym logins or something similar, it's not possible.

WishCow
You can't connect to something that doesn't even exist yet.
Lotus Notes
+1  A: 

An option is to set up an SQLite database in a directory outside of your htdocs folder. This is so people can't type in the name of the database file and download the entire database, severely compromising security.

If you are so inclined, you can even set up a layer of abstraction between PHP and the DBMS using PDO. Then, in order to create the database object, you would specify a DSN specific to SQLite write something like this:

$pdo_obj = new PDO('sqlite:/path/to/my_database.sqlite3');

and then query it like a normal PDO object using the PDO functions.

This method would better allow for easier migration to using a client-server DBMS once you can get one set up; it would just be a matter of copying the table structure and data over to the new database, and changing the DSN to something appropriate.

amphetamachine