MySQLi – PHP Syntax ”; Previous Next 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. This tutorial focuses heavily on using MySQL in a PHP environment. If you are interested in MySQL with PERL, then you can consider reading the PERL Tutorial. 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 − mysqli 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 = new mysqli($dbhost, $dbuser, $dbpass, $dbname); mysqli→query(,”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 = mysqli – > <i>function</i>(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. Print Page Previous Next Advertisements ”;
Category: mysqli
MySQLi – Drop Database
MySQLi – Drop Database ”; Previous Next Drop a Database using mysqladmin You would need special privileges to create or to delete a MySQL database. So, assuming you have access to the root user, you can create any database using the mysql mysqladmin binary. Be careful while deleting any database because you will lose your all the data available in your database. Here is an example to delete a database(TUTORIALS) created in the previous chapter − [root@host]# mysqladmin -u root -p drop TUTORIALS Enter password:****** This will give you a warning and it will confirm if you really want to delete this database or not. Dropping the database is potentially a very bad thing to do. Any data stored in the database will be destroyed. Do you really want to drop the ”TUTORIALS” database [y/N] y Database “TUTORIALS” dropped Drop Database using PHP Script 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. Print Page Previous Next Advertisements ”;
MySQLi – Introduction
MySQLi – Introduction ”; Previous Next MySQLi is an extension to MySQL API available in PHP and is introduced from PHP 5.0 onwards. It is also known as MySQL improved extension. Motivation behind MySQLi was to take advantage of new features available in MySQL 4.1.3 onwards. It provides numerous benefits over MySQL extension. MySQL provides an object oriented interface. It provides both object oriented and procedural approach to handle database operations. Object Oriented Interface <?php $mysqli = mysqli_connect(“localhost”, “user”, “password”, “database-name”); $result = mysqli_query($mysqli, “SELECT ”Welcome to MySQLi” AS _msg FROM DUAL”); $row = mysqli_fetch_assoc($result); echo $row[”_msg”]; ?> Procedural Approach <?php $mysqli = new mysqli(“localhost”, “user”, “password”, “database-name”); $result = $mysqli→query(“SELECT ”Welcome to MySQLi” AS _msg FROM DUAL”); $row = $result→fetch_assoc(); echo $row[”_msg”]; ?> MySQLi supports prepared statments. MySQLi supports multiple statments. MySQLi supports transactions. MySQLi provides enhanced debugging capabilities. Print Page Previous Next Advertisements ”;
MySQLi – Drop Tables
MySQLi – Drop Table ”; Previous Next It is very easy to drop an existing MySQL table, but you need to be very careful while deleting any existing table because the data lost will not be recovered after deleting a table. Syntax Here is a generic SQL syntax to drop a MySQL table − DROP TABLE table_name ; Dropping Tables from the Command Prompt To drop tables from the command prompt, we need to execute the DROP TABLE SQL command at the mysql> prompt. Example The following program is an example which deletes the tutorials_tbl − root@host# mysql -u root -p Enter password:******* mysql> use TUTORIALS; Database changed mysql> DROP TABLE tutorials_tbl Query OK, 0 rows affected (0.8 sec) mysql> Dropping Tables Using PHP Script 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. Print Page Previous Next Advertisements ”;