{"id":305,"date":"2016-08-07T03:48:05","date_gmt":"2016-08-07T00:48:05","guid":{"rendered":"https:\/\/community.virtono.com\/?p=305"},"modified":"2023-06-21T10:04:22","modified_gmt":"2023-06-21T07:04:22","slug":"how-mysql-and-php-work-together","status":"publish","type":"post","link":"https:\/\/www.virtono.com\/community\/knowledgebase\/how-mysql-and-php-work-together\/","title":{"rendered":"How MySQL and PHP Work Together"},"content":{"rendered":"<p style=\"text-align: center;\">This article is a fraction of a Number of Articles on MySQL, to access them click <a href=\"https:\/\/virtono.com\/community\/knowledgebase\/articles-on-mysql\/\" target=\"_blank\" rel=\"noopener\">here<\/a>.<\/p>\n<p>You interact with the database by passing messages to the MySQL server.<\/p>\n<p>The messages are composed in the SQL language, a standard computer language understood by most database management systems.<\/p>\n<p>PHP doesn\u2019t understand SQL, but it doesn\u2019t need to: PHP just establishes a connection with the MySQL server and sends the SQL message over the connection.<\/p>\n<p>The MySQL server interprets the SQL message, follows the instructions, and sends a return message that states its status and what it did (or reports an error if it couldn\u2019t understand or follow the instructions).<\/p>\n<p>The PHP language provides functions that make communicating with MySQL extremely simple. You use PHP functions to send SQL queries to the database.<\/p>\n<p>You don\u2019t need to know the details of communicating with MySQL; PHP handles the details. You only need to know the SQL queries and how to use the PHP functions.<\/p>\n<p><strong>PHP Functions That Communicate with MySQL<\/strong><\/p>\n<p>PHP provides two sets of functions for communicating with MySQL \u2014 the mysql functions and the mysqli (MySQL Improved) functions. Which functions you use depends on the version of MySQL and PHP you\u2019re using.<\/p>\n<p>The mysqli functions were added in PHP 5 for use with MySQL versions 4.1 and later. If you\u2019re using a web hosting company, you need to know whether it offers PHP 5, which version of MySQL it provides, and whether it makes the mysqli functions available. I assume that you\u2019re using PHP 5 or later, MySQL 5.0, and the mysqli functions. If your web host doesn\u2019t offer the mysqli functions, you need to convert the mysqli \u00a0to mysql functions.<\/p>\n<p>If you installed PHP and MySQL yourself on your own computer planning to develop your PHP scripts locally and upload the finished scripts to your web hosting company, you need to install the same versions and activate the same MySQL support functions that your web host provides. Otherwise, if you install different versions, even newer ones, the scripts may not behave in the<\/p>\n<p>same way on your web host\u2019s computer as they do on your local computer.<\/p>\n<p><strong>Communicating with MySQL<\/strong><\/p>\n<p><strong>Communicating with MySQL involves the following steps:<\/strong><\/p>\n<ol>\n<li>Connect to the MySQL server.<\/li>\n<li>Send the SQL query.<\/li>\n<\/ol>\n<p><strong>Connecting to the MySQL server<\/strong><\/p>\n<p>Before you can store or get any data, you need to connect to the database, which might be on the same computer as your PHP scripts or on a different computer. You don\u2019t need to know the details of connecting to the database because PHP handles the details. All you need to know is the name and location of the database, along with a username and password to access it. Think of a database connection in the same way that you think of a telephone connection. You don\u2019t need to know the details about how the connection is made \u2014 that is, how your words move from your telephone to another telephone \u2014 you need to know only the area code and phone number. The phone company handles the details.<\/p>\n<p>To connect to the MySQL server, you need to know the name of the computer on which the database is located and your MySQL account\u2019s user ID and password. For most queries, you also need to know the name of the database with which you want to interact.<\/p>\n<p>To open the connection, use the mysqli_connect function:<\/p>\n<p>$cxn = mysqli_connect(\u201c<em>host<\/em>\u201d,\u201d<em>acct<\/em>\u201d,\u201d<em>password<\/em>\u201d,\u201d<em>dbname<\/em>\u201d)<\/p>\n<p>or die (\u201c<em>message<\/em>\u201d);<\/p>\n<p>Fill in the following information:<\/p>\n<p>\u2726 <strong><em>host<\/em><\/strong><strong><em>: <\/em><\/strong>The name of the computer on which MySQL is installed \u2014 for example, databasehost.example.com. If the MySQL database is on the same computer as your website, you can use localhost as the computer name. If you leave this information blank (\u201c\u201d), PHP assumes<\/p>\n<p>localhost.<\/p>\n<p>\u2726 <strong><em>acct<\/em><\/strong><strong><em>: <\/em><\/strong>The name of any valid MySQL account.<\/p>\n<p>\u2726 <strong><em>password<\/em><\/strong><strong><em>: <\/em><\/strong>The password for the MySQL account specified by <em>acct<\/em><em>.<\/em><\/p>\n<p>If the MySQL account doesn\u2019t require a password, don\u2019t type anything between the quotes: \u201c\u201d.<\/p>\n<p>\u2726 <strong><em>dbname<\/em><\/strong><strong><em>: <\/em><\/strong>The name of the database with which you want to communicate.<\/p>\n<p>This parameter is optional \u2014 you can select the database later, with a separate command, if you prefer. You can select a different database at any point in your script.<\/p>\n<p>If you\u2019re using the mysql functions, you can\u2019t select the database in the connect function. You must use a separate function \u2014 mysql_<\/p>\n<p>select_db \u2014 to select the database.<\/p>\n<p>\u2726 <strong><em>message<\/em><\/strong><strong><em>: <\/em><\/strong>The message sent to the browser if the connection fails. The connection fails if the computer or network is down, or if the MySQL server isn\u2019t running. It also may fail if the information provided isn\u2019t correct \u2014 for example, if the password contains a typo.<\/p>\n<p>You might want to use a descriptive <em>message <\/em>during development, such as Couldn\u2019t connect to server, but a more general <em>message<\/em> suitable for customers after you put the application in use, such as<\/p>\n<p>The Catalog is not available at the moment. Please try again later.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This article is a fraction of a Number of Articles on MySQL, to access them click here. You interact with the database by passing messages to the MySQL server. The messages are composed in the SQL language, a standard computer language understood by most database management systems. PHP doesn\u2019t understand<\/p>\n","protected":false},"author":3,"featured_media":306,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[5],"tags":[],"class_list":["post-305","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-knowledgebase"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/www.virtono.com\/community\/wp-content\/uploads\/2016\/08\/php___mysql_wallpaper_by_milesandryprower-d9o6yat.png?fit=1024%2C576&ssl=1","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p7ISfL-4V","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":235,"url":"https:\/\/www.virtono.com\/community\/knowledgebase\/lamp-technology\/","url_meta":{"origin":305,"position":0},"title":"LAMP TECHNOLOGY","author":"Daniel Draga","date":"July 30, 2016","format":false,"excerpt":"LAMP STANDS FOR: L:Linux A:Apache M:MySQL P:PHP What is LAMP? LAMP is a shorthand term for a web application platform consisting of Linux, Apache, MySQL and one of Perl or PHP. Lamp is an\u00a0 OPENSOURCE Together, these open source tools provide a world-class platform for deploying web applications TECHNOLOGIES USED\u2026","rel":"","context":"In &quot;Knowledgebase&quot;","block_context":{"text":"Knowledgebase","link":"https:\/\/www.virtono.com\/community\/category\/knowledgebase\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.virtono.com\/community\/wp-content\/uploads\/2016\/07\/ic-1.png?fit=1200%2C750&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.virtono.com\/community\/wp-content\/uploads\/2016\/07\/ic-1.png?fit=1200%2C750&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.virtono.com\/community\/wp-content\/uploads\/2016\/07\/ic-1.png?fit=1200%2C750&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.virtono.com\/community\/wp-content\/uploads\/2016\/07\/ic-1.png?fit=1200%2C750&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/www.virtono.com\/community\/wp-content\/uploads\/2016\/07\/ic-1.png?fit=1200%2C750&ssl=1&resize=1050%2C600 3x"},"classes":[]},{"id":3791,"url":"https:\/\/www.virtono.com\/community\/tutorial-how-to\/how-to-install-zabbix-on-ubuntu\/","url_meta":{"origin":305,"position":1},"title":"How to Install Zabbix on Ubuntu","author":"George B.","date":"September 2, 2023","format":false,"excerpt":"We will walk you through installing Zabbix on Ubuntu 22.04 in this detailed guide, giving you access to a potent tool for monitoring your IT infrastructure. Monitoring is crucial in the world of system administration. One of the most important aspects of maintaining a strong IT infrastructure is making sure\u2026","rel":"","context":"In &quot;Tutorials&quot;","block_context":{"text":"Tutorials","link":"https:\/\/www.virtono.com\/community\/category\/tutorial-how-to\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.virtono.com\/community\/wp-content\/uploads\/2023\/09\/How-to-Install-Zabbix-on-Ubuntu.png?fit=360%2C240&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]},{"id":1036,"url":"https:\/\/www.virtono.com\/community\/tutorial-how-to\/installing-lamp-linux-apache-mysql-php-stack-on-debian-7-server\/","url_meta":{"origin":305,"position":2},"title":"INSTALLING LAMP (LINUX, APACHE, MYSQL &amp; PHP) STACK ON DEBIAN 7 SERVER","author":"Daniel Draga","date":"September 25, 2017","format":false,"excerpt":"LAMP STANDS FOR: L:Linux A:Apache M:MySQL P:PHP What is LAMP? LAMP is a shorthand term for a\u00a0web application platform\u00a0consisting of Linux, Apache, MySQL and one of Perl or PHP. Lamp is an\u00a0\u00a0OPENSOURCE Together, these open source tools provide a world-class platform for deploying web applications TECHNOLOGIES USED IN LAMP: For\u2026","rel":"","context":"In &quot;Tutorials&quot;","block_context":{"text":"Tutorials","link":"https:\/\/www.virtono.com\/community\/category\/tutorial-how-to\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.virtono.com\/community\/wp-content\/uploads\/2017\/09\/maxresdefault.jpg?fit=1200%2C750&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.virtono.com\/community\/wp-content\/uploads\/2017\/09\/maxresdefault.jpg?fit=1200%2C750&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.virtono.com\/community\/wp-content\/uploads\/2017\/09\/maxresdefault.jpg?fit=1200%2C750&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.virtono.com\/community\/wp-content\/uploads\/2017\/09\/maxresdefault.jpg?fit=1200%2C750&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/www.virtono.com\/community\/wp-content\/uploads\/2017\/09\/maxresdefault.jpg?fit=1200%2C750&ssl=1&resize=1050%2C600 3x"},"classes":[]},{"id":3507,"url":"https:\/\/www.virtono.com\/community\/tutorial-how-to\/how-to-install-lemp-stack-linux-nginx-mysql-and-php-on-ubuntu-22-04\/","url_meta":{"origin":305,"position":3},"title":"How to install LEMP stack (Linux, Nginx, MySQL, and PHP) on Ubuntu 22.04","author":"George B.","date":"June 20, 2023","format":false,"excerpt":"Introduction The LEMP stack is a popular software stack for web development and hosting. It includes four major components: Linux, Nginx, MySQL, and PHP. Each component serves a specific purpose in powering dynamic websites and web applications. Linux is the operating system that serves as the LEMP stack's foundation. In\u2026","rel":"","context":"In &quot;Tutorials&quot;","block_context":{"text":"Tutorials","link":"https:\/\/www.virtono.com\/community\/category\/tutorial-how-to\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.virtono.com\/community\/wp-content\/uploads\/2023\/06\/How-to-install-LEMP-stack-Linux-Nginx-MySQL-PHP-on-Ubuntu-22-04.png?fit=600%2C330&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.virtono.com\/community\/wp-content\/uploads\/2023\/06\/How-to-install-LEMP-stack-Linux-Nginx-MySQL-PHP-on-Ubuntu-22-04.png?fit=600%2C330&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.virtono.com\/community\/wp-content\/uploads\/2023\/06\/How-to-install-LEMP-stack-Linux-Nginx-MySQL-PHP-on-Ubuntu-22-04.png?fit=600%2C330&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":2961,"url":"https:\/\/www.virtono.com\/community\/tutorial-how-to\/how-to-install-wordpress-on-centos-8\/","url_meta":{"origin":305,"position":4},"title":"How to Install WordPress on CentOS 8","author":"George B.","date":"August 4, 2022","format":false,"excerpt":"What is WordPress? WordPress is a free and open-source platform for building websites. On a more technical level, WordPress is a PHP-based content management system (CMS) that uses a MySQL database. WordPress is the most user-friendly and powerful blogging and website builder available today. Important note: According to WPVALID the\u2026","rel":"","context":"In &quot;Tutorials&quot;","block_context":{"text":"Tutorials","link":"https:\/\/www.virtono.com\/community\/category\/tutorial-how-to\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.virtono.com\/community\/wp-content\/uploads\/2022\/08\/Looking-for-a-WordPress-Developer-1042x665-1.jpeg?fit=1042%2C665&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.virtono.com\/community\/wp-content\/uploads\/2022\/08\/Looking-for-a-WordPress-Developer-1042x665-1.jpeg?fit=1042%2C665&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.virtono.com\/community\/wp-content\/uploads\/2022\/08\/Looking-for-a-WordPress-Developer-1042x665-1.jpeg?fit=1042%2C665&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.virtono.com\/community\/wp-content\/uploads\/2022\/08\/Looking-for-a-WordPress-Developer-1042x665-1.jpeg?fit=1042%2C665&ssl=1&resize=700%2C400 2x"},"classes":[]},{"id":732,"url":"https:\/\/www.virtono.com\/community\/knowledgebase\/hardening-the-defense-of-database-server\/","url_meta":{"origin":305,"position":5},"title":"Hardening the Defense of Database Server","author":"Daniel Draga","date":"November 1, 2016","format":false,"excerpt":"Importance of Database Security: Databases often store sensitive data Incorrect data or loss of data could negatively affect business operations Databases can be used as bases to attack other systems from. \u00a0 Principles of Finding Holes Don't believe the documentation Implement your own client Debug the system to understand how\u2026","rel":"","context":"In &quot;Knowledgebase&quot;","block_context":{"text":"Knowledgebase","link":"https:\/\/www.virtono.com\/community\/category\/knowledgebase\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.virtono.com\/community\/wp-content\/uploads\/2016\/11\/network-security1.jpg?fit=1200%2C556&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.virtono.com\/community\/wp-content\/uploads\/2016\/11\/network-security1.jpg?fit=1200%2C556&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.virtono.com\/community\/wp-content\/uploads\/2016\/11\/network-security1.jpg?fit=1200%2C556&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.virtono.com\/community\/wp-content\/uploads\/2016\/11\/network-security1.jpg?fit=1200%2C556&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/www.virtono.com\/community\/wp-content\/uploads\/2016\/11\/network-security1.jpg?fit=1200%2C556&ssl=1&resize=1050%2C600 3x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.virtono.com\/community\/wp-json\/wp\/v2\/posts\/305","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.virtono.com\/community\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.virtono.com\/community\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.virtono.com\/community\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.virtono.com\/community\/wp-json\/wp\/v2\/comments?post=305"}],"version-history":[{"count":4,"href":"https:\/\/www.virtono.com\/community\/wp-json\/wp\/v2\/posts\/305\/revisions"}],"predecessor-version":[{"id":3562,"href":"https:\/\/www.virtono.com\/community\/wp-json\/wp\/v2\/posts\/305\/revisions\/3562"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.virtono.com\/community\/wp-json\/wp\/v2\/media\/306"}],"wp:attachment":[{"href":"https:\/\/www.virtono.com\/community\/wp-json\/wp\/v2\/media?parent=305"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.virtono.com\/community\/wp-json\/wp\/v2\/categories?post=305"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.virtono.com\/community\/wp-json\/wp\/v2\/tags?post=305"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}