Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New to the CF scene
    Join Date
    Apr 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Help me in Creating a MySQL Database using PHP

    Hi Can some one help me on this...
    I want to create a DataBase from a PHP code.....
    I have Created a MYSQL User but my PHP code wont create me the database...
    this is my code... plz help me...

    PHP Code:

    $hostname 
    "localhost";
    $username "root";
    $password "123";

    $dbConnected mysql_connect($hostname,$username,$password);

    $dbSuccess=true;

    if(
    $dbConnected){
    }else{
    echo
    "mySQL Connected FAIL <br/><br/>";
    $dbSuccess false;
    }

    if(
    $dbSuccess){
    $dbName "MyDB";
    $create_SQL "CREATE DATABASE" $dbName;

    if(
    mysql_query($create_SQL)){
    echo 
    "CREATE DATABASE" $dbName " = Successfull.";
    }else{
    echo 
    "CREATE " $dbName " = Failed.";
    }




    What gone wrong it wont create me a database,,??

  • #2
    Regular Coder
    Join Date
    Dec 2011
    Posts
    107
    Thanks
    5
    Thanked 17 Times in 17 Posts
    First, you are using mysql which is depreciated, use either mysqli or PDO.

    Second, go here : PHP Create Database and Tables (W3Schools actually has a decent tutorial for a change. )
    True courage is about knowing not when to take a life, but when to spare one. PDO Tutorial

  • #3
    New to the CF scene
    Join Date
    Apr 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi bro.. i used this code from PHP Create Database and Tables but i still get this error


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

  • #4
    Regular Coder Linux_Sage's Avatar
    Join Date
    Mar 2014
    Location
    Sterling,VA
    Posts
    106
    Thanks
    0
    Thanked 10 Times in 10 Posts
    Did you even create this user, chris4u_test, within your database? If you did have you granted the correct permissions to the necessary actions on that database, my_db? This is good info on how to do that:

    https://www.digitalocean.com/communi...sions-in-mysql

  • #5
    New to the CF scene
    Join Date
    Apr 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yes i have created the user name chris4u_test this is he screenshot Help me in Creating a MySQL Database using PHP-mysql.png
    how do i set the correct permissions? from where?.....


    btw i am on a free host just for now.... is it because of the free host limitations? i m on 5gbfree.com

  • #6
    Regular Coder Linux_Sage's Avatar
    Join Date
    Mar 2014
    Location
    Sterling,VA
    Posts
    106
    Thanks
    0
    Thanked 10 Times in 10 Posts
    The link I provided has instructions on how to do that.

    Since you're using a free host you'll need to find out if you're able to access that server in some kind of way. If you can't SSL into the server or connect to the database in some kind of way you'll probably be forced to use some of their front-end tools to do these tasks. Otherwise you're probably out of luck and will have to contact your host to set these things up for you.

    Anyway, assuming you can, basically you'll need to run a command on your MySQL client along the lines of:

    Code:
    GRANT ALL PRIVILEGES ON * . * TO 'chris4u_test'.'localhost';
    That command basically grants all priviledges on all databases. Of course you'll need to login as root to perform it or an account with the necessary permissions to do so. You can change localhost to the appropriate hostname if you're not doing it locally. After you've run the command you'll need to execute

    Code:
    FLUSH PRIVILEGES
    Last edited by Linux_Sage; 04-20-2014 at 05:41 PM. Reason: clarification

  • #7
    New to the CF scene
    Join Date
    Apr 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    HOW DO I RUN THIS "GRANT ALL PRIVILEGES ON * . * TO 'chris4u_test'.'localhost'"; CAN I RUN THIS IN THIS CODE??



    PHP Code:
    $con=mysqli_connect("localhost","chris4u_test","chris123");
    // Check connection
    if (mysqli_connect_errno())
      {
      echo 
    "Failed to connect to MySQL: " mysqli_connect_error();
      }

    // Create database
    $sql"GRANT ALL PRIVILEGES ON * . * TO 'chris4u_test'.'localhost'";
    if (
    mysqli_query($con,$sql))
      {
       echo 
    "GRANTED ALL PRIVILEGES SUCESSFULLY";
      }
    else
      {
      echo 
    "ERROR GRANTING PRIVILEGES: " mysqli_error($con);
      } 

  • #8
    Regular Coder Linux_Sage's Avatar
    Join Date
    Mar 2014
    Location
    Sterling,VA
    Posts
    106
    Thanks
    0
    Thanked 10 Times in 10 Posts
    It'd be better to connect to that database directly via a client or SSH into the server and do it from there by running the mysql command to log into the database.

    But again it depends on if your host allows this. This is information you'll need from them. They may have already provided a means to accomplish this via tools they provide. Do they have a phpMyAdmin setup? Or anything else? You could do it from there.

  • #9
    New to the CF scene
    Join Date
    Apr 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    yes they have a phpmyadmin..

  • #10
    Regular Coder Linux_Sage's Avatar
    Join Date
    Mar 2014
    Location
    Sterling,VA
    Posts
    106
    Thanks
    0
    Thanked 10 Times in 10 Posts
    Well then there you go. You can use that to run that query. Or you could do basically the same thing through user management. Detailed here: user management - PmaWiki


  •  

    Posting Permissions

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