Category: php Mysql

  • Khóa học miễn phí PHP & MySQL – Database Info nhận dự án làm có lương

    PHP & MySQL – Database Info Example



    There are a few important commands in MySQL which can be executed either at the MySQL prompt or by using any script like PHP to get various important information about the database server.

    Sr.No. Command & Description
    1

    SELECT VERSION( )

    Server version string

    2

    SELECT DATABASE( )

    Current database name (empty if none)

    3

    SELECT USER( )

    Current username

    4

    SHOW STATUS

    Server status indicators

    5

    SHOW VARIABLES

    Server configuration variables

    Example

    Try the following example to get database info −

    Copy and paste the following example as mysql_example.php −

    <html>
       <head>
          <title>Getting MySQL Database Info</title>
       </head>
       <body>
          <?php
             $dbhost = ''localhost
             $dbuser = ''root
             $dbpass = ''root@123
             $dbname = ''TUTORIALS
             $mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
             $tutorial_count = null;
    
             if($mysqli->connect_errno ) {
                printf("Connect failed: %s<br />", $mysqli->connect_error);
                exit();
             }
             printf(''Connected successfully.<br />'');
    
             if ($result = mysqli_query($mysqli, "SELECT DATABASE()")) {
                $row = mysqli_fetch_row($result);
                printf("Default database is %s<br />", $row[0]);
                mysqli_free_result($result);
             }
             $mysqli->close();
          ?>
       </body>
    </html>
    

    Output

    Access the mysql_example.php deployed on apache web server and verify the output.

    Connected successfully.
    Default database is tutorials
    

    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí PHP & MySQL – Handling NULL nhận dự án làm có lương

    PHP & MySQL – Handling NULL Example



    You can use the if…else condition to prepare a query based on the NULL value.

    The following example takes the tutorial_count from outside and then compares it with the value available in the table.

    Example

    Copy and paste the following example as mysql_example.php −

    <html>
       <head>
          <title>Handling NULL</title>
       </head>
       <body>
          <?php
             $dbhost = ''localhost
             $dbuser = ''root
             $dbpass = ''root@123
             $dbname = ''TUTORIALS
             $mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
             $tutorial_count = null;
             if($mysqli->connect_errno ) {
                printf("Connect failed: %s<br />", $mysqli->connect_error);
                exit();
             }
             printf(''Connected successfully.<br />'');
    
             if( isset($tutorial_count )) {
                $sql = ''SELECT tutorial_author, tutorial_count
                   FROM  tcount_tbl
                   WHERE tutorial_count = '' + $tutorial_count;
             } else {
                $sql = ''SELECT tutorial_author, tutorial_count
                   FROM  tcount_tbl
                   WHERE tutorial_count IS NULL
             }
             $result = $mysqli->query($sql);
    
             if ($result->num_rows > 0) {
                while($row = $result->fetch_assoc()) {
                   printf("Author: %s, Count: %d <br />",
                      $row["tutorial_author"],
                      $row["tutorial_count"]);
                }
             } else {
                printf(''No record found.<br />'');
             }
             $mysqli->close();
          ?>
       </body>
    </html>
    

    Output

    Access the mysql_example.php deployed on apache web server and verify the output.

    Connected successfully.
    No record found.
    

    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí PHP & MySQL – Select Database nhận dự án làm có lương

    PHP & MySQL – Select Database Example



    PHP uses mysqli_select_db function to select the database on which queries are to be performed. This function takes two parameters and returns TRUE on success or FALSE on failure.

    Syntax

    mysqli_select_db ( mysqli $link , string $dbname ) : bool
    

    Sr.No. Parameter & Description
    1

    $link

    Required – A link identifier returned by mysqli_connect() or mysqli_init().

    2

    $dbname

    Required – Name of the database to be connected.

    Example

    Try the following example to select a database −

    Copy and paste the following example as mysql_example.php −

    <html>
       <head>
          <title>Selecting MySQL Database</title>
       </head>
       <body>
          <?php
             $dbhost = ''localhost
             $dbuser = ''root
             $dbpass = ''root@123
             $conn = mysqli_connect($dbhost, $dbuser, $dbpass);
    
             if(! $conn ) {
                die(''Could not connect: '' . mysqli_error($conn));
             }
             echo ''Connected successfully<br />
             $retval = mysqli_select_db( $conn, ''TUTORIALS'' );
    
             if(! $retval ) {
                die(''Could not select database: '' . mysqli_error($conn));
             }
             echo "Database TUTORIALS selected successfullyn";
             mysqli_close($conn);
          ?>
       </body>
    </html>
    

    Output

    Access the mysql_example.php deployed on apache web server and verify the output.

    Database TUTORIALS selected successfully
    

    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí PHP & MySQL – Drop Tables nhận dự án làm có lương

    PHP & MySQL – Drop Table Example



    PHP uses mysqli query() or mysql_query() function to drop a MySQL table. This function takes two parameters and returns TRUE on success or FALSE on failure.

    Syntax

    $mysqli->query($sql,$resultmode)
    

    Sr.No. Parameter & Description
    1

    $sql

    Required – SQL query to drop a table.

    2

    $resultmode

    Optional – Either the constant MYSQLI_USE_RESULT or MYSQLI_STORE_RESULT depending on the desired behavior. By default, MYSQLI_STORE_RESULT is used.

    Example

    Try the following example to drop a table −

    Copy and paste the following example as mysql_example.php −

    <html>
       <head><title>Dropping MySQL Table</title></head>
       <body>
          <?php
             $dbhost = ''localhost
             $dbuser = ''root
             $dbpass = ''root@123
             $dbname = ''TUTORIALS
             $mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
    
             if($mysqli->connect_errno ) {
                printf("Connect failed: %s<br />", $mysqli->connect_error);
                exit();
             }
             printf(''Connected successfully.<br />'');
    
             if ($mysqli->query("Drop Table tutorials_tbl")) {
                printf("Table tutorials_tbl dropped successfully.<br />");
             }
             if ($mysqli->errno) {
                printf("Could not drop table: %s<br />", $mysqli->error);
             }
             $mysqli->close();
          ?>
       </body>
    </html>
    

    Output

    Access the mysql_example.php deployed on apache web server and verify the output.

    Connected successfully.
    Table tutorials_tbl dropped successfully.
    

    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí PHP & MySQL – Connect Database nhận dự án làm có lương

    PHP & MySQL – Connect Database Example



    PHP provides mysqli contruct or mysqli_connect() function to open a database connection. This function takes six parameters and returns a MySQL link identifier on success or FALSE on failure.

    Syntax

    $mysqli = new mysqli($host, $username, $passwd, $dbName, $port, $socket);
    

    Sr.No. Parameter & Description
    1

    $host

    Optional − The host name running the database server. If not specified, then the default value will be localhost:3306.

    2

    $username

    Optional − The username accessing the database. If not specified, then the default will be the name of the user that owns the server process.

    3

    $passwd

    Optional − The password of the user accessing the database. If not specified, then the default will be an empty password.

    4

    $dbName

    Optional − database name on which query is to be performed.

    5

    $port

    Optional − the port number to attempt to connect to the MySQL server..

    6

    $socket

    Optional − socket or named pipe that should be used.

    You can disconnect from the MySQL database anytime using another PHP function close().

    Syntax

    $mysqli->close();
    

    Example

    Try the following example to connect to a MySQL server −

    Copy and paste the following example as mysql_example.php −

    <html>
       <head>
          <title>Connecting MySQL Server</title>
       </head>
       <body>
          <?php
             $dbhost = ''localhost
             $dbuser = ''root
             $dbpass = ''root@123
             $mysqli = new mysqli($dbhost, $dbuser, $dbpass);
    
             if($mysqli->connect_errno ) {
                printf("Connect failed: %s<br />", $mysqli->connect_error);
                exit();
             }
             printf(''Connected successfully.<br />'');
             $mysqli->close();
          ?>
       </body>
    </html>
    

    Output

    Access the mysql_example.php deployed on apache web server and verify the output.

    Connected successfully.
    

    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí PHP & MySQL – Create Database nhận dự án làm có lương

    PHP & MySQL – Create Database Example



    PHP uses mysqli query() or mysql_query() function to create or delete a MySQL database. This function takes two parameters and returns TRUE on success or FALSE on failure.

    Syntax

    $mysqli->query($sql,$resultmode)
    

    Sr.No. Parameter & Description
    1

    $sql

    Required – SQL query to create a MySQL database.

    2

    $resultmode

    Optional – Either the constant MYSQLI_USE_RESULT or MYSQLI_STORE_RESULT depending on the desired behavior. By default, MYSQLI_STORE_RESULT is used.

    Example

    Try the following example to create a database −

    Copy and paste the following example as mysql_example.php −

    <html>
       <head><title>Creating MySQL Database</title></head>
       <body>
          <?php
             $dbhost = ''localhost
             $dbuser = ''root
             $dbpass = ''root@123
             $mysqli = new mysqli($dbhost, $dbuser, $dbpass);
    
             if($mysqli->connect_errno ) {
                printf("Connect failed: %s<br />", $mysqli->connect_error);
                exit();
             }
             printf(''Connected successfully.<br />'');
    
             if ($mysqli->query("CREATE DATABASE TUTORIALS")) {
                printf("Database TUTORIALS created successfully.<br />");
             }
             if ($mysqli->errno) {
                printf("Could not create database: %s<br />", $mysqli->error);
             }
             $mysqli->close();
          ?>
       </body>
    </html>
    

    Output

    Access the mysql_example.php deployed on apache web server and verify the output.

    Connected successfully.
    Database TUTORIALS created successfully.
    

    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí PHP & MySQL – Insert Records nhận dự án làm có lương

    PHP & MySQL – Insert Records Example



    PHP uses mysqli_query function to insert records in table. This function takes three parameters and returns TRUE on success or FALSE on failure.

    Syntax

    mysqli_query ( mysqli $link, string $query, int $resultmode = MYSQLI_STORE_RESULT ) : mixed
    

    Sr.No. Parameter & Description
    1

    $link

    Required – A link identifier returned by mysqli_connect() or mysqli_init().

    2

    $query

    Required – SQL query to drop a table.

    2

    $resultmode

    Optional – Either the constant MYSQLI_USE_RESULT or MYSQLI_STORE_RESULT depending on the desired behavior. By default, MYSQLI_STORE_RESULT is used.

    Example

    This example will take three parameters from the user and will insert them into the MySQL table −

    Copy and paste the following example as mysql_example.php −

    <html>
       <head>
          <title>Add New Record in MySQL Database</title>
       </head>
       <body>
          <?php
             if(isset($_POST[''add''])) {
                $dbhost = ''localhost
                $dbuser = ''root
                $dbpass = ''root@123
                $conn = mysqli_connect($dbhost, $dbuser, $dbpass);
    
                if(! $conn ) {
                   die(''Could not connect: '' . mysqli_error($conn));
                }
                if(! get_magic_quotes_gpc() ) {
                   $tutorial_title = addslashes ($_POST[''tutorial_title'']);
                   $tutorial_author = addslashes ($_POST[''tutorial_author'']);
                } else {
                   $tutorial_title = $_POST[''tutorial_title''];
                   $tutorial_author = $_POST[''tutorial_author''];
                }
                $submission_date = $_POST[''submission_date''];
                $sql = "INSERT INTO tutorials_tbl ".
                   "(tutorial_title,tutorial_author, submission_date) "."VALUES ".
                   "(''$tutorial_title'',''$tutorial_author'',''$submission_date'')";
                mysqli_select_db( $conn, ''TUTORIALS'' );
                $retval = mysqli_query( $conn, $sql );
    
                if(! $retval ) {
                   die(''Could not enter data: '' . mysqli_error($conn));
                }
                echo "Entered data successfullyn";
                mysqli_close($conn);
             } else {
          ?>
          <form method = "post" action = "<?php $_PHP_SELF ?>">
             <table width = "600" border = "0" cellspacing = "1" cellpadding = "2">
                <tr>
                   <td width = "250">Tutorial Title</td>
                   <td><input name = "tutorial_title" type = "text" id = "tutorial_title"></td>
                </tr>
                <tr>
                   <td width = "250">Tutorial Author</td>
                   <td><input name = "tutorial_author" type = "text" id = "tutorial_author"></td>
                </tr>
                <tr>
                   <td width = "250">Submission Date [   yyyy-mm-dd ]</td>
                   <td><input name = "submission_date" type = "text" id = "submission_date"></td>
                </tr>
                <tr>
                   <td width = "250"> </td>
                   <td></td>
                </tr>
                <tr>
                   <td width = "250"> </td>
                   <td><input name = "add" type = "submit" id = "add"  value = "Add Tutorial"></td>
                </tr>
             </table>
          </form>
       <?php
          }
       ?>
       </body>
    </html>
    

    Output

    Access the mysql_example.php deployed on apache web server, enter details and verify the output on submitting the form.

    Entered data successfully
    

    While doing a data insert, it is best to use the function get_magic_quotes_gpc() to check if the current configuration for magic quote is set or not. If this function returns false, then use the function addslashes() to add slashes before the quotes.

    You can put many validations around to check if the entered data is correct or not and can take the appropriate action.


    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí PHP & MySQL – Drop Database nhận dự án làm có lương

    PHP & MySQL – Drop Database Example



    PHP uses mysqli query() or mysql_query() function to drop a MySQL database. This function takes two parameters and returns TRUE on success or FALSE on failure.

    Syntax

    $mysqli->query($sql,$resultmode)
    

    Sr.No. Parameter & Description
    1

    $sql

    Required – SQL query to drop a MySQL database.

    2

    $resultmode

    Optional – Either the constant MYSQLI_USE_RESULT or MYSQLI_STORE_RESULT depending on the desired behavior. By default, MYSQLI_STORE_RESULT is used.

    Example

    Try the following example to drop a database −

    Copy and paste the following example as mysql_example.php −

    <html>
       <head><title>Dropping MySQL Database</title></head>
       <body>
          <?php
             $dbhost = ''localhost
             $dbuser = ''root
             $dbpass = ''root@123
             $mysqli = new mysqli($dbhost, $dbuser, $dbpass);
    
             if($mysqli->connect_errno ) {
                printf("Connect failed: %s<br />", $mysqli->connect_error);
                exit();
             }
             printf(''Connected successfully.<br />'');
    
             if ($mysqli->query("Drop DATABASE TUTORIALS")) {
                printf("Database TUTORIALS dropped successfully.<br />");
             }
             if ($mysqli->errno) {
                printf("Could not drop database: %s<br />", $mysqli->error);
             }
             $mysqli->close();
          ?>
       </body>
    </html>
    

    Output

    Access the mysql_example.php deployed on apache web server and verify the output.

    Connected successfully.
    Database TUTORIALS dropped successfully.
    

    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí PHP & MySQL – Create Tables nhận dự án làm có lương

    PHP & MySQL – Create Table



    PHP uses mysqli query() or mysql_query() function to create a MySQL table. This function takes two parameters and returns TRUE on success or FALSE on failure.

    Syntax

    $mysqli->query($sql,$resultmode)
    

    Sr.No. Parameter & Description
    1

    $sql

    Required – SQL query to create a MySQL table.

    2

    $resultmode

    Optional – Either the constant MYSQLI_USE_RESULT or MYSQLI_STORE_RESULT depending on the desired behavior. By default, MYSQLI_STORE_RESULT is used.

    Example

    Try the following example to create a table −

    Copy and paste the following example as mysql_example.php −

    <html>
       <head>
          <title>Creating MySQL Table</title>
       </head>
       <body>
          <?php
             $dbhost = ''localhost
             $dbuser = ''root
             $dbpass = ''root@123
             $dbname = ''TUTORIALS
             $mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
    
             if($mysqli->connect_errno ) {
                printf("Connect failed: %s<br />", $mysqli->connect_error);
                exit();
             }
             printf(''Connected successfully.<br />'');
             $sql = "CREATE TABLE tutorials_tbl( ".
                "tutorial_id INT NOT NULL AUTO_INCREMENT, ".
                "tutorial_title VARCHAR(100) NOT NULL, ".
                "tutorial_author VARCHAR(40) NOT NULL, ".
                "submission_date DATE, ".
                "PRIMARY KEY ( tutorial_id )); ";
             if ($mysqli->query($sql)) {
                printf("Table tutorials_tbl created successfully.<br />");
             }
             if ($mysqli->errno) {
                printf("Could not create table: %s<br />", $mysqli->error);
             }
             $mysqli->close();
          ?>
       </body>
    </html>
    

    Output

    Access the mysql_example.php deployed on apache web server and verify the output.

    Connected successfully.
    Table tutorials_tbl created successfully.
    

    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí PHP & MySQL – Overview nhận dự án làm có lương

    PHP & MySQL – Overview



    MySQL works very well in combination of various programming languages like PERL, C, C++, JAVA and PHP. Out of these languages, PHP is the most popular one because of its web application development capabilities.

    PHP provides various functions to access the MySQL database and to manipulate the data records inside the MySQL database. You would require to call the PHP functions in the same way you call any other PHP function.

    The PHP functions for use with MySQL have the following general format −

    mysql_function(value,value,...);
    

    The second part of the function name is specific to the function, usually a word that describes what the function does. The following are two of the functions, which we will use in our tutorial −

    mysqli_connect($connect);
    mysqli_query($connect,"SQL statement");
    

    The following example shows a generic syntax of PHP to call any MySQL function.

    <html>
       <head>
          <title>PHP with MySQL</title>
       </head>
    
       <body>
          <?php
             $retval = mysql_function(value, [value,...]);
             if( !$retval ) {
                die ( "Error: a related error message" );
             }
             // Otherwise MySQL or PHP Statements
          ?>
       </body>
    </html>
    

    Starting from the next chapter, we will see all the important MySQL functionality along with PHP.


    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc