• Saturday , 30 May 2020

PHP Tutorial 27 – MySQL Creating A Table (PHP For Beginners)

Code Canyon



In the previous tutorial we took a first look at MySQL and Phpmyadmin. In this video we will run our first MySQL Query through PHP to create a table inside of our …

Original source

3d Ocean

Related Posts

34 Comments

  1. Zeeshan M. Raje
    September 2, 2017 at 05:15

    // php code to create table on mysql
    <?php
    $servername = "localhost";
    $username = "root"; //type your username
    $password = "**"; //type your password
    $dbname = "accounts"; /type your database name

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    //mysqli stands for my scripted query language improve.

    // Check connection

    if ($conn->connect_error) {

        die("Connection failed: " . $conn->connect_error);
    }

    // sql to create table

    $sql = "CREATE TABLE MyGuests (
    id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    firstname VARCHAR(30) NOT NULL,
    lastname VARCHAR(30) NOT NULL,
    email VARCHAR(50),
    reg_date TIMESTAMP
    )";

    if ($conn->query($sql) === TRUE) {
        echo "Table accounts created successfully";
    } else {
        echo "Error creating table: " . $conn->error;
    }

    $conn->close();

    ?>

  2. Zeeshan M. Raje
    September 2, 2017 at 05:15

    / to create database on mysql using php code
    <?php
    $servername="localhost";
    $username="root"; /type your username
    $password="***"; /type your password
    $conn=new mysqli("$servername","$username","$password");
    if($conn->connect_error)
    {
    die("not connected".$conn->connect_error);
    }
    $sql="CREATE DATABASE MYdb";
    if($conn->query($sql)=== true)
    {
    echo"database created";
    }
    else
    {
    echo"not created".$conn->error;
    }
    $conn->close();
    ?>

  3. Zeeshan M. Raje
    September 2, 2017 at 05:15

    I am not able to find table in my database and i dont find any errors in my php code

  4. Mihajlo Ilic
    September 2, 2017 at 05:15

    Warning: mysql_select_db() expects parameter 2 to be resource

    How to fix this?

  5. RustDude
    September 2, 2017 at 05:15

    useless nothing working even with mysqli

  6. Gabriel Ribas
    September 2, 2017 at 05:15

    mine only worked with mysqli_connect, it gave me this error message:
    ( ! )
    Deprecated: mysql_connect(): The mysql extension is deprecated and will
    be removed in the future: use mysqli or PDO instead in
    C:wampwwwtestmysql.php on line 8

  7. Ali Khan
    September 2, 2017 at 05:15

    very helping..thnx chris

  8. plezx29
    September 2, 2017 at 05:15

    para-meter? don't you mean pa-rameter?

  9. Mark Chew
    September 2, 2017 at 05:15

    First video to not start with "hello youtube!" you've changed Chris..

  10. Vidyut Gupta
    September 2, 2017 at 05:15

    It works.. thanks a lot

  11. Bican Guzeler
    September 2, 2017 at 05:15

    why did you quit making videos ? you were great

  12. sonia pais
    September 2, 2017 at 05:15

    i m gettin a syntax error on line 9 its bcuz of the bracket (

  13. Yaron Golfand
    September 2, 2017 at 05:15

    Doesn't work for me 🙁
    I had no errors…. and no table….

  14. Nedim Dugonjic
    September 2, 2017 at 05:15

    It works :

    <?php

    $con = mysqli_connect("localhost","root","","accounts1");
    // Check connection

    // Create table
    $sql = "CREATE TABLE users7
    (
    ID int NOT NULL AUTO_INCREMENT,
    PRIMARY KEY(ID),
    Username varchar(20),
    Password varchar(20),
    FirstName varchar(20),
    LastName varchar(20)
    )";

    // Execute query
    mysqli_query($con,$sql)

  15. Isaac Newoton
    September 2, 2017 at 05:15

    I have watched all his videos, and actually made a page that allows you to make an account then login in to view the information, but now it wont let me access the mysql database,  without changing any of the code, i am unable to  access it. I can still use php,  and XAMPP shows now errors with Mysql.  i used the code from when i first watched this video and it still does not allow me to access the database

  16. ChanceOf SteeZ
    September 2, 2017 at 05:15

    Mines didn't make a user2 table and I called $sql="CREATE TABLE user2"

  17. dv looper
    September 2, 2017 at 05:15

    i'd encountered this error, Parse error: syntax error, unexpected '$sql' (T_VARIABLE)

  18. AtlasTheDestroyer
    September 2, 2017 at 05:15

    It's actually sad that I've learned more in less than a day from this guy than half a year at a university in UK.

  19. Chris Bator
    September 2, 2017 at 05:15

    my man……here is my code…..gives no errors on the localhost, but does not table 'users 2' does not show up on sql
    <?php

    $accounts = mysql_connect("localhost" , "root" , "Baseball82")
    or die (mysql_error());

    mysql_select_db("accounts" , $accounts);

    $sql = "CREATE TABLE users2
    (
    ID int NOT NULL AUTO_INCREMENT,
    PRIMARY KEY(ID),
    Username varchar(20),
    Password varchar(20),
    First varchar(20),
    Last varchar(20),

    )
    ";

    mysql_query($sql, $accounts);

  20. RAGHAV KHANNA
    September 2, 2017 at 05:15

    thanks dude

  21. Nikola Pasic
    September 2, 2017 at 05:15

    Hi! I have a problem! I created a .php file and checked him and it's seems nothing wrong but, on MyAdmin there is no my table that I created. When I try to import table MySQL reports me an error, but i couldn't find it. Code seems me ok. Here is the code:
    <?php 

    $konekcija = mysql_connect ("localhost" , "root", "26081990") or die (mysql_error());

    mysql_select_db("nalog", $konekcija);

    $tabela = "CREATE TABLE ljudi
    (
    ID int NOT NULL AUTO_INCREMENT,
    PRIMARY KEY(ID),
    Korisnicko_ime varchar(20),
    Sifra varchar(20),
    Ime varchar(20),
    Prezime(20)
    )
    ";

    mysql_query($tabela, $konekcija);

    ?>
    an this is the error that MySQL reports me:]

    Error
    MySQL said: Documentation

    #1064 – You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '<?php 

    $konekcija = mysql_connect ("localhost" , "root", "26081990") or die (' at line 1 

    If anyone has a solution please answer me. THANK YOU!

  22. MrJonathanRB
    September 2, 2017 at 05:15

    He said ' ' with this ones you can concatenate with the . then you contacatenate the variables but " " you cannot do it.

  23. Petar Stankovic
    September 2, 2017 at 05:15

    Why use " " now and on previous 20 tutorials ' ' ? I've seen differences you've mentioned in one of the tutorials but I still don't understand. Thanks in advance!

  24. cler rico
    September 2, 2017 at 05:15

    hello, good day, this video is very great. My I ask if you have any latest update video tutorial? Hope you can create more videos about mysql. Thanks

  25. Paul Goux
    September 2, 2017 at 05:15

    I see a lot of people having problems with the code in the tut as did I. To solve this problem remove the comma from the last field.

  26. Chirag Gupta
    September 2, 2017 at 05:15

    Hey Chris, I would request you to upload some video tutorials of javascript as well.

  27. Frank Joseph
    September 2, 2017 at 05:15

    Chris, your tutorials are great. I'm trying to create the table.php page but I'm not working with a "localhost". I'm saving the files to my website on a shared server. Can you tell me what should replace "localhost"? –Cheers

  28. Garrey Xu
    September 2, 2017 at 05:15

    Why can't I create the new table with the same code in the video? My code is below:
    <?php

    $accounts = mysql_connect("localhost", "root", "zaqxsw") or die(mysql_error());

    mysql_select_db("accounts", $accounts);

    $sql = "CREATE TABLE users2
    (
    ID Int NOT NULL AUTO_INCREMENT,
    PRIMARY KEY(ID),
    Username varchar(20),
    Password varchar(20),
    First varchar(20),
    Last varchar(20)
    )
    ";
    mysql_query ($sql, $accounts);

    ?>

  29. JohnNody
    September 2, 2017 at 05:15

    Booot! xD

  30. Tapesh Lall
    September 2, 2017 at 05:15

    Congratulations!

  31. adrian reimer
    September 2, 2017 at 05:15

    if your having problems with it creating the second table, make sure you are not putting any spaces in the name of row properties, so "first name varchar(20)" would be "firstname varchar(20)"

  32. Mohammed salman
    September 2, 2017 at 05:15

    hey dud @suresh kumar, change the curly braces in CREATE TABLE instead use parenthesis..  surely u'll get 🙂
    have a nce day

  33. suresh kumar
    September 2, 2017 at 05:15

    <?php
    $accounts= mysql_connect("localhost","root","janaki") or die(mysql_error());
    mysql_select_db("database " , $accounts);
    $sql= "CREATE TABLE example3
    {
    ID Int AUTO_INCREMENT NOT NULL,
    PRIMARY KEY(ID),
    username varchar(20),
    password varchar(20),
    firstname varchar(20),
    lastname varchar(20)
    }";

    mysql_query($sql,$accounts);

    ?>

    could you pls tell me whats wrong on this code, no error throws, but table is not created 

  34. clod1977
    September 2, 2017 at 05:15

    If you are having trouble, try this:

    <?php

    $con = mysqli_connect("localhost","root","password","accounts");
    // Check connection
    if (mysqli_connect_errno())
      {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }

    // Create table
    $sql = "CREATE TABLE users2
    (
    ID int NOT NULL AUTO_INCREMENT,
    PRIMARY KEY(ID),
    Username varchar(20),
    Password varchar(20),
    FirstName varchar(20),
    LastName varchar(20)
    )";

    // Execute query
    if (mysqli_query($con,$sql))
      {
      echo "Table created successfully";
      }
    else
      {
      echo "Error creating table: " . mysqli_error($con);
      }
      
    ?>

Leave A Comment

You must be logged in to post a comment.