Discuss PHP & MySQL ”; Previous Next 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. Print Page Previous Next Advertisements ”;
Category: php Mysql
PHP & MySQL – Home
PHP & MySQL Tutorial PDF Version Quick Guide Resources Job Search Discussion 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. Audience This tutorial is designed for Java programmers who would like to understand the PHP functions to connect to MySQL in detail and actual usage. Prerequisites Before proceeding with this tutorial, you should have a good understanding of PHP programming language. As you are going to deal with MySQL database, you should have prior exposure to SQL and Database concepts. Print Page Previous Next Advertisements ”;
PHP & MySQL – Handling NULL
PHP & MySQL – Handling NULL Example ”; Previous Next 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. Print Page Previous Next Advertisements ”;
PHP & MySQL – Quick Guide
PHP & MySQL – Quick Guide ”; Previous Next 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. PHP & MySQL – Environment Setup In order to develop and run PHP Web pages, three vital components need to be installed on your computer system. Web Server − PHP works with virtually all Web Server software, including Microsoft”s Internet Information Server (IIS) but most often used is Apache Server. Download Apache for free here − https://httpd.apache.org/download.cgi Database − PHP works with virtually all database software, including Oracle and Sybase but most commonly used is MySQL database. Download MySQL for free here − https://www.mysql.com/downloads/ PHP Parser − In order to process PHP script instructions, a parser must be installed to generate HTML output that can be sent to the Web Browser. This tutorial will guide you how to install PHP parser on your computer. PHP Parser Installation Before you proceed, it is important to make sure that you have proper environment setup on your machine to develop your web programs using PHP. Store the following php file in Apache”s htdocs folder. phpinfo.php <?php phpinfo(); ?> Type the following address into your browser”s address box. http://127.0.0.1/phpinfo.php If this displays a page showing your PHP installation related information, then it means you have PHP and Webserver installed properly. Otherwise, you have to follow the given procedure to install PHP on your computer. This section will guide you to install and configure PHP over the following four platforms − PHP Installation on Linux or Unix with Apache PHP Installation on Mac OS X with Apache PHP Installation on Windows NT/2000/XP with IIS PHP Installation on Windows NT/2000/XP with Apache Apache Configuration If you are using Apache as a Web Server, then this section will guide you to edit Apache Configuration Files. Check here − PHP Configuration in Apache Server PHP.INI File Configuration The PHP configuration file, php.ini, is the final and immediate way to affect PHP”s functionality. Check here − PHP.INI File Configuration Windows IIS Configuration To configure IIS on your Windows machine, you can refer your IIS Reference Manual shipped along with IIS. Install MySQL Database The most important thing you will need, of course is an actual running database with a table that you can query and modify. MySQL DB − MySQL is an open source database. You can download it from MySQL Official Site. We recommend downloading the full Windows installation. In addition, download and install MySQL Administrator as well as MySQL Query Browser. These are GUI based tools that will make your development much easier. Finally, download and unzip MySQL Connector/J (the MySQL JDBC driver) in a convenient directory. For the purpose of this tutorial we will assume that you have installed the driver at C:Program FilesMySQLmysql-connector-java-5.1.8. Accordingly, set CLASSPATH variable to C:Program FilesMySQLmysql-connector-java-5.1.8mysql-connector-java-5.1.8-bin.jar. Your driver version may vary based on your installation. Set Database Credential When we install MySQL database, its administrator ID is set to root and it gives provision to set a password of your choice. Using root ID and password you can either create another user ID and password, or you can use root ID and password for your JDBC application. There are various database operations like database creation and deletion, which would need administrator ID and password. For rest of the JDBC tutorial, we would use MySQL Database with guest as ID and guest123 as password. If you do not have sufficient privilege to create new users, then you can ask your Database Administrator (DBA) to create a user ID and password for you. Create Database To create the TUTORIALSPOINT database, use the following steps − Step 1 Open a Command Prompt and change to the installation directory as follows − C:> C:>cd Program FilesMySQLbin C:Program FilesMySQLbin> Note − The path to mysqld.exe may vary depending on the install location of MySQL on your system. You can also check documentation on how to start and stop your database server. Step 2 Start the database server by executing the following command, if it is already not running. C:Program FilesMySQLbin>mysqld C:Program FilesMySQLbin> Step 3 Create the TUTORIALSPOINT database by executing the following command − C:Program FilesMySQLbin> mysqladmin create TUTORIALSPOINT -u guest -p Enter password: ******** C:Program FilesMySQLbin> Create Table To create the Employees table in TUTORIALSPOINT database, use the following steps − Step 1 Open a Command Prompt and change to the installation directory as follows − C:>
PHP & MySQL – Using Joins
PHP & MySQL – Using Joins Example ”; Previous Next PHP uses mysqli query() or mysql_query() function to get records from a MySQL tables using Joins. 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 get records from multiple tables using Join. 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. First create a table in MySQL using following script and insert two records. create table tcount_tbl( tutorial_author VARCHAR(40) NOT NULL, tutorial_count int ); insert into tcount_tbl values(”Mahesh”, 3); insert into tcount_tbl values(”Suresh”, 1); Example Try the following example to get records from a two tables using Join. − Copy and paste the following example as mysql_example.php − <html> <head> <title>Using joins on MySQL Tables</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 = ”SELECT a.tutorial_id, a.tutorial_author, b.tutorial_count FROM tutorials_tbl a, tcount_tbl b WHERE a.tutorial_author = b.tutorial_author”; $result = $mysqli->query($sql); if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) { printf(“Id: %s, Author: %s, Count: %d <br />”, $row[“tutorial_id”], $row[“tutorial_author”], $row[“tutorial_count”]); } } else { printf(”No record found.<br />”); } 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. Id: 1, Author: Mahesh, Count: 3 Id: 2, Author: Mahesh, Count: 3 Id: 3, Author: Mahesh, Count: 3 Id: 5, Author: Suresh, Count: 1 Print Page Previous Next Advertisements ”;
PHP & MySQL – Where Clause
PHP & MySQL – Where Clause Example ”; Previous Next PHP uses mysqli query() or mysql_query() function to select records in a MySQL table using where clause. 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 select records in a MySQL table using Where Clause. 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 select a record using where clause in a table − Copy and paste the following example as mysql_example.php − <html> <head> <title>Using Where Clause</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 = ”SELECT tutorial_id, tutorial_title, tutorial_author, submission_date FROM tutorials_tbl where tutorial_author = “Mahesh””; $result = $mysqli->query($sql); if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) { printf(“Id: %s, Title: %s, Author: %s, Date: %d <br />”, $row[“tutorial_id”], $row[“tutorial_title”], $row[“tutorial_author”], $row[“submission_date”]); } } else { printf(”No record found.<br />”); } mysqli_free_result($result); $mysqli->close(); ?> </body> </html> Output Access the mysql_example.php deployed on apache web server and verify the output. Here we”ve entered multiple records in the table before running the select script. Connected successfully. Id: 1, Title: MySQL Tutorial, Author: Mahesh, Date: 2021 Id: 2, Title: HTML Tutorial, Author: Mahesh, Date: 2021 Id: 3, Title: PHP Tutorial, Author: Mahesh, Date: 2021 Print Page Previous Next Advertisements ”;
PHP & MySQL – Select Database Example ”; Previous Next 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 Print Page Previous Next Advertisements ”;
PHP & MySQL – Database Info
PHP & MySQL – Database Info Example ”; Previous Next 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 Print Page Previous Next Advertisements ”;
PHP & MySQL – Update Records
PHP & MySQL – Update Records Example ”; Previous Next PHP uses mysqli query() or mysql_query() function to update records in 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 update records in 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 update a record in a table − Copy and paste the following example as mysql_example.php − <html> <head> <title>Updating 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(”UPDATE tutorials_tbl set tutorial_title = “Learning Java” where tutorial_id = 4”)) { printf(“Table tutorials_tbl updated successfully.<br />”); } if ($mysqli->errno) { printf(“Could not update table: %s<br />”, $mysqli->error); } $sql = “SELECT tutorial_id, tutorial_title, tutorial_author, submission_date FROM tutorials_tbl”; $result = $mysqli->query($sql); if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) { printf(“Id: %s, Title: %s, Author: %s, Date: %d <br />”, $row[“tutorial_id”], $row[“tutorial_title”], $row[“tutorial_author”], $row[“submission_date”]); } } else { printf(”No record found.<br />”); } mysqli_free_result($result); $mysqli->close(); ?> </body> </html> Output Access the mysql_example.php deployed on apache web server and verify the output. Here we”ve entered multiple records in the table before running the select script. Connected successfully. Table tutorials_tbl updated successfully. Id: 1, Title: MySQL Tutorial, Author: Mahesh, Date: 2021 Id: 2, Title: HTML Tutorial, Author: Mahesh, Date: 2021 Id: 3, Title: PHP Tutorial, Author: Mahesh, Date: 2021 Id: 4, Title: Learning Java, Author: Mahesh, Date: 2021 Id: 5, Title: Apache Tutorial, Author: Suresh, Date: 2021 Print Page Previous Next Advertisements ”;
PHP & MySQL – Overview
PHP & MySQL – Overview ”; 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. 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. Print Page Previous Next Advertisements ”;