{"id":651,"date":"2021-06-05T16:42:51","date_gmt":"2021-06-05T11:12:51","guid":{"rendered":"https:\/\/mediastroke.com\/blog\/?p=651"},"modified":"2022-07-01T13:17:25","modified_gmt":"2022-07-01T07:47:25","slug":"php-mysql-connection","status":"publish","type":"post","link":"https:\/\/mediastroke.com\/blog\/php-mysql-connection\/","title":{"rendered":"PHP MySQL Connection Using cPanel Hosting"},"content":{"rendered":"\n
PHP MySQL connection setting up on a cPanel hosting is generally challenging for beginners. To make things simple, we have created this guide, which offers a detailed overview of how to set up a MySQL database and connect it to a PHP application using cPanel hosting. This article also explains about how to deploy a local project (running on XAMPP, LAMP, WAMP, etc.) in cPanel Hosting.<\/p>\n\n\n\n
To make your PHP project with MySQL live, just follow below simple steps.<\/p>\n\n\n\n
To setup MySQL database in cPanel, basically you need to create MySQL Database, create MySQL User, assign User to Database & manage User Privileges and import database using .sql file. Steps for the same are explained in below sections:<\/p>\n\n\n\n
You have to create a MySQL User and give the necessary privileges, before they may access and control the database.<\/p>\n\n\n\n
Note:<\/strong> Remember to save the database name, username, and password to use in PHP programs to connect to the database.<\/p>\n\n\n\n Now as we have both a MySQL Database and a MySQL User, we must assign the MySQL User to the Database in order to set the appropriate privileges. To do this, follow the below steps:<\/p>\n\n\n\n NOTE:<\/strong> You can assign multiple MySQL Users with different sets of privileges to the same MySQL database.<\/p>\n\n\n\n Now we can perform the data insert operation in the table. Create another file with .php extension. Write the following code with the table name, columns and values.<\/p>\n\n\n\n By following steps in this guide we successfully performed PHP MySQL connection and uploaded the PHP MySQL project on cPanel. Let us know in the comments below if you face any difficulties in following this guide.<\/p>\n\n\n\nAssign User to MySQL Database:<\/h3>\n\n\n\n
<\/figure>\n\n\n\n
Import Database to cPanel:<\/h3>\n\n\n\n
<\/figure>\n\n\n\n
How to connect to MySQL database using PHP in cPanel?<\/h2>\n\n\n\n
Connect MySQL database with PHP Script in cPanel<\/h3>\n\n\n\n
<?php\n$servername<\/span> = \"localhost\";\n$database<\/span> = \"databasename\";\n$username<\/span> = \"username\";\n$password<\/span> = \"password\";\n\/\/ Create connection using musqli_connect function\n$conn<\/span> = mysqli_connect($servername, $username, $password, $database<\/span>);\n\/\/ Connection Check\nif (!$conn)<\/span> {\n die(\"Connection failed: \" . $conn<\/span>->connect_error);\n}\n\nelse{\n echo \"Connected Successfully!\";\n $conn<\/span>->close();\n}\n\n?><\/code><\/pre>\n\n\n\n
MySQL Insert operation with PHP Script in cPanel<\/h3>\n\n\n\n
<?php\n$servername<\/span> = \"localhost\";\n$database<\/span> = \"databasename\";\n$username<\/span> = \"username\";\n$password<\/span> = \"password\";\n\/\/ Create connection using musqli_connect function\n$conn<\/span> = mysqli_connect($servername, $username, $password, $database<\/span>);\n\/\/ Connection Check\nif (!$conn<\/span>) {\n die(\"Connection failed: \" . $conn<\/span>->connect_error);\n}\n\nelse{\n \/\/insert query\n$qu<\/span>=\"INSERT INTO table_name<\/strong>(column1, column2, column3<\/em>) VALUES ('values1', 'values2', 'values3<\/em>')\";\n\/\/query execution\nif($conn<\/span>->query($qu<\/span>)===TRUE){\n\necho \"Inserted Successfully\";\n $conn<\/span>->close();\n}else{\necho \"Insert Failed \".$conn<\/span>->error;\n$conn<\/span>->close();\n}\n}\n\n?><\/code><\/pre>\n\n\n\n
In Summary:<\/h2>\n\n\n\n