Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2006
    Location
    Buffalo, NY
    Posts
    1,501

    [PHP] Manipulating MySQL (an introduction)

    this article is meant for users that have little/no knowledge of how to manipulate MySQL databases via PHP code. Intermediate users might find this useful brush up reading.

    the first thing you need to learn is how to open a connection with the MySQL server. this is achieved by PHP's built in function, mysql_connect();

    Code:
    resource mysql_connect ( [string server [, string username [, string password]]] )
    as you can see, the mysql_connect(); function processes 5 parameters when it is accessed. the string server is the MySQL server, on most webhosts and home rigs, this will be localhost. string username is the username required to access your MySQL database. string password is the password used to identify your username. boolean new_link & integer client_flags are the last two but are not necessary to establish a MySQL connection.

    following mysql_connect(); usually comes die(); which tells your server what to do incase something goes wrong with the connection. any parameters passed through die(); will output on the .php page.

    mysql_error(); returns the text of the error message from previous MySQL operation.

    mysql_close(); is used to close a MySQL connection.

    here is an example code snippet, opening a MySQL connection.
    Code:
    <?
        mysql_connect('localhost', 'mysql_user', 'mysql_password');
            or die( mysql_error() );
        mysql_close();
    ?>
    .... more coming, check back for updates
    &copy; nuri hodges ( irunbackwards @ phpmydonut.com )
    Last edited by CodyRo; 02-27-2006 at 04:30 AM.

  2. #2
    Nice start so far, how about going another step and telling people how to set up mysql accounts/db's with cpanel/whm? I think that's where a lot of people get tripped up initially.

  3. #3
    Join Date
    Feb 2006
    Location
    Buffalo, NY
    Posts
    1,501
    as suggested by brian-as, a simple guide to setup a MySQL db using (cPanel 10.8.1-RELEASE-113)

    login to your websites cPanel (usually @ domain.com/cpanel)

    navigate to "Site Management Tools" & select the option "MySQL Databases"

    this will take you to a new page, where cPanel lists your current databases ( and users assigned to these db's ) and a little lower, three forms are available. one to create a MySQL database, one to create a MySQL user, and one to assign a user to a specific database.

    go ahead and type in a database name in the form next to "MySQL Database:" and click "Create MySQL Database", this will take you to a confirmation page.

    now, return to the MySQL Database manager in cPanel, and scroll down to the form that asks for a username/password, enter in a username with the password, and then click "Create MySQL User", again this will take you to another confirmation page, after you've completed this hit back or re-navigate to the MySQL section of cPanel.

    now, to give the user access to a specific database, scroll down to "Grant permissions on a MySQL database to a MySQL user" make sure the checkbox "ALL" is checked to grant uninhibited access to all MySQL commands (you can limit it if you wish by selecting other boxes), and click the button "grant permissions"

    congratulations, you have succesfully created a mysql db & user! you can now use them to connect!

    &copy; nuri hodges ( irunbackwards @ phpmydonut.com )

    more to come....
    Last edited by CodyRo; 03-07-2006 at 09:56 PM.
    Cody R.
    Hawk Host Inc. Proudly Serving websites since 2004.
    Official Let's Encrypt Sponsor

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •