dcsimg
www.webdeveloper.com
Results 1 to 7 of 7

Thread: I'm not able to create table in database using PHP, PLEASE HELP

  1. #1
    Join Date
    Mar 2017
    Posts
    1

    Lightbulb I'm not able to create table in database using PHP, PLEASE HELP

    I have been trying to make a table in my database which is already connected. But still, it brings out to me a promblematic error that "Could Not Make Table" and now I am really fed up. Please anyone can get me out of this?

    Here's my code:
    PHP Code:
    <?php session_start(); ?>
    <?php



    require_once('functions.php');



    // Database connection
    $dbname 'developing_house'// Write the name of the Database you gonna connect, for eg. developing_house
    $dbhost 'localhost'// Write the Host name, for eg. localhost.
    $dbuser 'kumar'// Write the username having all previlages of a Database, for eg. root
    $dbpass '****'// Write the corresponding User's Password. (Protected by 'sha256' encryption)

    $db_details = array(
        
    'host' => $dbhost,
        
    'user' => $dbuser,
        
    'password' => $dbpass,
        
    'dbname' => $dbname,
    );
    $IQcon mysqli_connect($db_details['host'],$db_details['user'],$db_details['password'],$db_details['dbname']);



    // Check connection
    if (mysqli_connect_errno())
      {
      echo 
    "Looks like there's an Error connecting IQubex Database. :-(";
      }
    else{
      echo 
    null;
    }




    // MySQL queries
    $sql 'CREATE TABLE employee( '.
          
    'emp_id INT NOT NULL AUTO_INCREMENT, '.
          
    'emp_name VARCHAR(20) NOT NULL, '.
          
    'emp_address  VARCHAR(20) NOT NULL, '.
          
    'emp_salary   INT NOT NULL, '.
          
    'join_date    timestamp(14) NOT NULL, '.
          
    'primary key ( emp_id ))';
       
    mysql_select_db($dbname);
       
    $retval mysql_query$sql$IQcon );
       
       if(! 
    $retval ) {
          die(
    'Could not create table: ' mysql_error());
       }
       
       echo 
    "Table employee created successfully";
       
    mysql_close($IQcon);



    ?>
    Thanks in advance!

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    21,978
    What does the mysql_error() part of the error output say?

    Does the DB user you are connecting with have the necessary privileges to create a table in that database?

    Also, the mysql_*() functions have been deprecated for years and are no longer available in PHP 7, so I strongly recommend you move to using either the newer MySQLi extension or the PDO extension (my personal preference).
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    21,978
    PS: Oh, wait, you connect with mysqli_connect, but then call mysql_query(). That's probably your problem.
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  4. #4
    Join Date
    Jan 2017
    Location
    West Midlands, UK
    Posts
    39
    It might be worth using

    Code:
    CREATE TABLE IF NOT EXISTS `table_name`( ... )
    to avoid trying to create a table that is already there.

    Regards,

    Shaun.

  5. #5
    Join Date
    Oct 2016
    Posts
    109
    Shaun,

    Where would you add the CREATE TABLE IF NOT EXIST in this code ?
    And can you substitute the OOP code below to procedural so we can learn ?
    I'm afraid OOP code got into my code which is procedural the most. This happend because I mix and match codes taken from different youtube tut vids.

    Thanks in advance!


    <?php

    $servername = "localhost";
    $username = "blah";
    $password = "blah";
    $dbname = "blah";

    $conn = new mysqli($servername,$username,$password,$dbname);

    if($conn->connect_error)
    {
    die($conn->connect_error);
    }

    $sql = "CREATE TABLE $myuser(
    id INT(6) UNSIGNED AUTO_INCREMENT, PRIMARY KEY
    name varchar(30) NOT NULL,
    email varchar(50) NOT NULL)";

    if($conn->query($sql)===TRUE){
    echo "table created!";
    }else{
    echo "table failed!";
    }

    $conn->close();

    ?>

  6. #6
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    1,765
    Where would you add the CREATE TABLE IF NOT EXIST in this code ?
    Really? This is your first question from someone's attempt to help you? Really?

    So you don't know how to code php (one doesn't have to open and close PHP mode from line to line!) and you don't know sql syntax and you don't even (apparently) know where a manual is for either tool. If all you are going to do is cut and paste code from others, why are you even bothering to try and learn to code?

    Do Some Reading!! Try To Learn instead of 'Learning How To Copy'. Is this the state of learning in the world now?

    Yeah - this is tough criticism. I admit it. But someone has to give the old 'kick in the pants'.
    JG
    PS - If you're posting here you should be using:

    error_reporting(E_ALL);
    ini_set('display_errors', '1');


    at the top of ALL php code while you develop it!

  7. #7
    Join Date
    Jan 2017
    Location
    West Midlands, UK
    Posts
    39
    Quote Originally Posted by uniqueideaman View Post
    Shaun,

    Where would you add the CREATE TABLE IF NOT EXIST in this code ?
    Add it to your MySQL query, instead of
    Code:
    CREATE TABLE `table_name`;
    add in the
    Code:
    IF NOT EXISTS
    in the appropriate place.
    And can you substitute the OOP code below to procedural so we can learn ?
    Don't worry about whether or not you're using OOP. In any case, where you are making a new instance of something, you are using an object even if the rest of your code is not Object Orientated
    I'm afraid OOP code got into my code which is procedural the most. This happend because I mix and match codes taken from different youtube tut vids.
    Again this is something that you shouldn't worry about. Break your problem down like this:

    1) What am I trying to achieve?
    2) How am I going to achieve this?
    3) Does my PHP script work?
    4) If it doesn't work, how do I debug it?
    5) If it does work then what do I need to do next?

    When you reach step 5, worry about how you've solved your initial problem at a later date. Remember that one can always refactor at some point in the future, when one has a better understanding of what is going on.

    OOP is just another way of moving and manipulating data, which is what PHP and all problem-orientated programming languages are all about.

    Regards,

    Shaun.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles