Page 1 of 1

Connecting to mySQL problem in PHP

PostPosted: 28. December 2007 08:05
by Angel Dust
Hi I've downloaded XAMPP because I was looking at doing a bit of PHP and mySQL at home to complement my current university studies. The problem is while I can code a bit I don't know how to set anything up! I have managed to figure out how to get normal php stuff working but connecting to mySQL has become a bit of a problem.
I know that the php code involves this

mysql_connect(servername, username, password)

however I have no idea what those three variable are. I've tried opening up the WinMySQLAdmin control panel and looking around there but I'm unsure of what I'm seeing!

Thanks in advance for any help!

PostPosted: 28. December 2007 09:17
by Izzy
WinMySQLAdmin is now deprecated.

After starting Apache and MySQL go to:
http://localhost/
in your browser and check out all the menu items including phpMyAdmin which will give you a GUI for MySQL - it has much documentation and links to explore.

You can experiment by creating a database on the localhost server, giving that database a user with a password and permissions for that database - which might help with your understanding of that code snippet you posted:
mysql_connect(servername, username, password)

There are several posts about this subject and one that may interest you at this stage relates to creating a database for WordPress but may be used for any database creation.
http://community.apachefriends.org/f/viewtopi ... 258#112258
HTH


===========================================
1. The new DeskTopXampp Launch Control for XAMPP / XAMPPlite
Posted by Ridgewood available from Ridgewood'sDTX web site

2. Build Rich AJAX Applications - Faster
TIBCO General Interface Pro Edition but FREE and Open Source
Fully working with NO donations required to get a user/password
===========================================

PostPosted: 28. December 2007 10:50
by KallistaAEnvarou
To make a new user, all you need to do is go to http://localhost/phpmyadmin and select the users table from the mysql database, click "insert" and insert the information. Then, add the user to the db database like you did to the users table. Lastly, you'll need to click "sql" and then type FLUSH PRIVILEGES. You can code an automatic adder if you want, but you'd have to connect to the database with your root user.

It's wise for you to research minimal permissions as a method of sql-injection prevention.

PostPosted: 28. December 2007 12:22
by sari42
KallistaAEnvarou wrote:To make a new user, all you need to do is go to http://localhost/phpmyadmin and select the users table from the mysql database, click "insert" and insert the information. Then, add the user to the db database like you did to the users table. Lastly, you'll need to click "sql" and then type FLUSH PRIVILEGES.
there is an easier way in phpMyAdmin:
login as superuser(root)->main->Privileges->Add a new user ...

Advantage: no flush privs needed, optional adding of
host, password, grants on special db ....

@Angel Dust:
there are tons of tutorials out there, amongst the first hits from google, e.g.,
http://www.freewebmasterhelp.com/tutorials/phpmysql

PostPosted: 28. December 2007 12:26
by KallistaAEnvarou
That way's too confusing for me.

btw If you use my method, when you make the password, make sure to go to the left and choose PASSWORD as the function, or else you won't be able to connect. Sorry about leaving that out. The above method doesn't require this step.

PostPosted: 28. December 2007 12:30
by Wiedmann
That way's too confusing for me.

Maybe, but your way is still deprecated!
If you (or someone other) want use another easy way: Just use the GRANT command in the SQL window (or use this command in the mysql shell client, without using phpMyAdmin).

PostPosted: 28. December 2007 12:39
by KallistaAEnvarou
That's still too confusing for minimal permissions, at least on my part. I need to revert back to my code-based, automatic method, but I'm too lazy to remake my form. Now, OP, if you want, you can just run a php page like this:

mysql_connect(localhost,root,root_password);

mysql_select_db('mysql');

mysql_query("INSERT INTO users (host,user,password,[privileges desired]) VALUES('localhost', $username, PASSWORD('$password'), [2 for each privilege desired])");

mysql_query("INSERT INTO db (host, db, user, [privileges desired] VALUES('localhost', $dbname, $username, [2 for each privilege desired])");

mysql_query("FLUSH PRIVILEGES");

mysql_close();

So, for example, if you wanted just insert and select for me_is, you could do...

mysql_query("INSERT INTO users (host, user, password, select_priv, insert_priv) VALUES('localhost', $username, PASSWORD('$password'), 2, 2"));

mysql_query("INSERT INTO db (host, db, user, select_priv, insert_priv) VALUES('localhost', $dbname, $username, 2, 2"));

for the queries.

Then you could maybe put this in localhost and then do a form for it like I do, where you input the information and choose the privileges and passwords, like they do on a live server.

PostPosted: 28. December 2007 12:41
by sari42
Wiedmann wrote:Just use the GRANT command in the SQL window (or use this command in the mysql shell client, without using phpMyAdmin).
OK, using phpMyadmin is great for learning the mysql syntax,
as the used query is displayed on the resulting page.

PostPosted: 28. December 2007 12:42
by KallistaAEnvarou
If you can get past the forms, yeah.

PostPosted: 29. December 2007 13:05
by Angel Dust
Thanks for the prompt replies! I'll put all this into practice tomorrow and let you know how it goes.