Skip to site navigation

Web Hosting - Linux: Managing Databases (MySQL)

Expand / Collapse
 

Web Hosting - Linux: Managing Databases (MySQL)


Aim of this article

This article explains how you can manage the MySQL database(s) for your Linux cPanel account, and how to code a connection to them in PHP.

Creating a MySQL Database

First log on to the cPanel Control Panel by opening a Web browser and browsing to http://myaccount.myzen.co.uk/cpanel/ or http://mydomain/cpanel/.  Once logged in scroll to the Databases section.

Click MySQL Databases.

Enter the following information:

  • New Database: Enter a name for your database.

    Click Create Database.

    NOTE:  You may only create as many databases as defined by the cPanel service you have purchased from Zen.

     

     

     

     

    Once the database is created click Go Back to return to the MySQL management page.

    Scroll to the section labeled MySQL Users.  If you do not have any existing database users you can create one here.  If you existing users you may use these instead.

    To create a new user enter the following information:

    • Username: A username for this database user.
    • Password: A password for this database user.  You will need to enter this in the 'Password (Again)' box to verify it is correct.

    Once done click Create User.

    You will now need to assign the user to a database.  To do this select the user and database they should access from the drop-down boxes and click Add.

    Your database should now be ready to use.

    Managing your MySQL databases:

    In order to add tables and data to your database(s) you can run SQL statements from scripts on your Web page, or alternatively use phpMyAdmin.  This can be found on the front page of your cPanel Control Panel under the heading Databases.

    To log in to phpMyAdmin click phpMyAdmin.

    This will log you on to the phpMyAdmin utility which allows you to manage your databases. Documentation for phpMyAdmin can be found by selecting the link phpMyAdmin documentation on this page.

    Connecting to a MySQL database using PHP:

    To connect to your MySQL database you will first need to code a PHP page which includes a connection to your database. Sample code is printed below:

    <?

    $dbConn = @mysql_pconnect ("localhost", "myUserID", "myPassword");

    if (!$dbConn)
    {
      echo "<p>Error: Could not connect to Database.</p>";
      exit;
    }

    echo "<p>Connect to database successful.</p>";

    ?>


    Replace myUserID and myPassword in the above statement with the database username and password you have set earlier.

    To test a "recordset" create a test table, and then add the following before the closing ?> php tag.

    // Tell mySQL what DB to connect to
    mysql_select_db("myDatabase");

    // Create a query string
    $stSQL = "select * from tTemp;";

    // Execute the query
    $rs = mysql_query($stSQL);

    // get the number of rows returned, then disply it
    $rowCount = mysql_num_rows($rs);

    echo "<p>Number of rows in table is " .$rowCount." </p>";
    echo "<p>Rows.. if any:<br>";

    // loop and display 1st col of table contents
    for ($i=0;$i < $rowCount; $i )
    {
      $rsRow = mysql_fetch_array($rs);
      echo "<br>".$rsRow[0];
    }


    Replace the name myDatabase in the first code above with the name of the database you created earlier.

    The result of this code should be the first column of the table being displayed - assuming it is populated with data of course.

    * NOTE: Each script you install via the cPanel script library will use up one of your available mySQL databases.



  • Rate this Article:
         
    Tags:

    User Comments

    No Member Photo posted 16th July 2010
    This comment was helpful. 0 This comment is not helpful.
    Info clear, but requires very careful reading.

    Feedback on this Article


    Name: *
    Email Address:
    Web Address:
       
      
     
     
       
    CAPTCHA
    Speak the CAPTCHA code Change the CAPTCHA code
     
    Type the characters:Required
     

    Details
    Type: GETTING STARTED
    Rated 2 stars based on 5 votes.
    Article has been viewed 2,506 times.
    Options

    Skip to site navigation