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




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


34 responses to “PHP Tutorial 27 – MySQL Creating A Table (PHP For Beginners)”

  1. // 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. / 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. 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

  4. 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)

  5. 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

  6. 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);

  7. 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!

  8. 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

  9. 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);

    ?>

  10. <?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 

  11. 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 Reply