BCA / B.Tech 11 min read

Use Of MYSQL in PHP in Hindi | MySQL का PHP में उपयोग हिंदी में

RK

लेखक Ratan Kumawat · संस्थापक, NotesMedia

प्रकाशित:

त्वरित सारांश और मुख्य बिंदु

  • • विषय फोकस: PHP Notes in Hindi | PHP नोट्स हिंदी में (BCA / B.Tech Syllabus)
  • • मुख्य विषय: Use Of MYSQL in PHP in Hindi | MySQL का PHP में उपयोग हिंदी में - General Topics
  • • अवलोकन: Quick Answer: PHP में MySQL का use dynamic websites और web applications में data को create, store और manage करने के लिए एक relational database manageme...
  • • मुख्य अवधारणाएं: Difference PHP HTML, Server client side dynamic static Hindi, PHP vs HTML comparison,Use Of MYSQL in PHP, MySQL PHP connection, CRUD operations in PHP, PHP database tutorial, XAMPP setup for PHP MySQL
Quick Answer: PHP में MySQL का use dynamic websites और web applications में data को create, store और manage करने के लिए एक relational database management system (RDBMS) की तरह किया जाता है। PHP script mysqli_connect() या PDO के ज़रिए MySQL database server से connect होती है, जहाँ SQL queries चलाकर tables में data को insert, select, update और delete (CRUD operations) किया जाता है। MySQL सारा data structured format (rows और columns) में save रखता है और PHP user request के अनुसार उसे HTML page पर dynamic रूप से display करती है।

PHP और MySQL क्या हैं? (Introduction)

Web development में dynamic web pages बनाने के लिए PHP और MySQL दुनिया का सबसे popular combination हैं। इसे अक्सर LAMP stack (Linux, Apache, MySQL, PHP) या XAMPP stack के रूप में जाना जाता है।

  • PHP (Hypertext Preprocessor): यह एक open-source server-side scripting language है। इसका मुख्य काम user से request लेना, business logic process करना, database से interact करना और final output browser को भेजना है। PHP और HTML के बीच अंतर को detail में यहाँ समझें: Difference Between PHP and HTML in Hindi.
  • MySQL: यह एक fast, multi-user और reliable Relational Database Management System (RDBMS) है। इसका मुख्य काम website के data (जैसे user credentials, product catalog, student records, comments) को tables में securely store और organize रखना है।

जब कोई user browser में web form submit करता है (जैसे registration या login form), तब PHP script उस input data को capture करती है और SQL query बनाकर MySQL server को भेज देती है। Web forms से data receive करने का तरीका यहाँ पढ़ें: PHP Form in Hindi.

Use Of MYSQL in PHP in Hindi | MySQL का PHP में उपयोग हिंदी में

PHP और MySQL client-server architecture: Browser से PHP और PHP से MySQL database communication.

Advertisement

Difference between PHP and MySQL

BCA और B.Tech exams में अक्सर यह सवाल पूछा जाता है कि PHP और MySQL में क्या अंतर है। दोनों अलग-अलग technologies हैं जो web architecture में अलग काम करती हैं:

Point PHP MySQL
Type Server-side scripting language Relational Database Management System (RDBMS)
Primary Role Logic process करना और dynamic content generate करना Data को store, organize, retrieve और manage करना
Language Used PHP syntax (variables, functions, loops) SQL (Structured Query Language)
Execution Web server (Apache, Nginx) के PHP engine पर run होता है MySQL database server (background service) पर run होता है
Output HTML, JSON, XML या plain text generate करता है Query results (rows / result set) देता है
Default Port Web server के port (80 या 8080) पर access होता है Default network port 3306 पर listen करता है

Advantages of Using MySQL in PHP

PHP के साथ MySQL का use करने के कई बड़े फायदे (advantages) हैं, जिस वजह से WordPress, Joomla और लाखों web applications इसी backend पर चलते हैं:

  • Open Source और Free: PHP और MySQL दोनों पूरी तरह free और open-source हैं, जिससे development cost बचती है।
  • High Performance और Speed: MySQL queries बहुत तेज़ी से execute होती हैं और बड़े datasets को efficiently handle करती हैं।
  • Platform Independent: यह combination Windows, Linux, macOS और Unix servers पर बिना किसी बदलाव के चलता है।
  • Built-in Extension Support: PHP core में MySQL के साथ काम करने के लिए MySQLi और PDO extensions direct मिलते हैं।
  • High Security: Password hashing, prepared statements और SSL encryption के ज़रिए data secure रहता है।
  • Easy CRUD Operations: PHP functions से database में data add करना, view करना, edit करना और remove करना बहुत आसान है। User login session maintain करने के लिए पढ़ें: Session and Cookies in Hindi.

PHP को MySQL से Connect करने के तरीके (MySQLi vs PDO)

PHP code से MySQL server से connect करने के मुख्य रूप से दो modern methods हैं: MySQLi (MySQL Improved) और PDO (PHP Data Objects)। पुराना mysql_* extension PHP 7.0 से पूरी तरह हटा दिया गया है।

Feature MySQLi Extension PDO (PHP Data Objects)
Database Support सिर्फ MySQL database को support करता है MySQL, PostgreSQL, Oracle, SQLite जैसे 12 से ज़्यादा databases
Programming Method Procedural और Object-Oriented दोनों तरह से सिर्फ Object-Oriented तरीका
Prepared Statements हाँ (SQL injection से protection) हाँ (SQL injection से protection)
Speed और Performance MySQL के लिए PDO से थोड़ी fast लगभग बराबर, flexibility ज़्यादा है
Named Parameters Support नहीं करता (सिर्फ ? positional markers) Named parameters (जैसे :email) support करता है

Connecting MySQL with PHP (Database Connection Code)

MySQL server से connect करने के लिए हमें 4 parameters की ज़रूरत होती है: server name (hostname), username, password, और database name।

<?php
$servername = "localhost";
$username   = "root";
$password   = "";
$database   = "college_db";

// MySQL server से connection create करना (MySQLi Procedural)
$conn = mysqli_connect($servername, $username, $password, $database);

// Connection check करना
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully to MySQL database!";
?>

Code Explanation: mysqli_connect() function connection object return करता है। अगर server बंद है या login details गलत हैं, तो !$conn true होगा और die() function script execution को रोककर error message print कर देगा। Errors को handle करने के rules detail में यहाँ देखें: Error Handling in PHP in Hindi.

Create and Manage Database File Using MySQL in PHP (CRUD Operations)

Database management का मतलब है CRUD operations perform करना: Create, Read, Update, Delete। नीचे सभी operations के complete, working code examples दिए गए हैं:

1. Create: Database और Table में New Data Insert करना

Data insert करने के लिए SQL INSERT INTO statement का use किया जाता है:

<?php
// Database table 'students' में data add करना
$sql = "INSERT INTO students (name, email, course) VALUES ('Amit Sharma', 'amit@example.com', 'BCA')";

if (mysqli_query($conn, $sql)) {
    echo "New record created successfully. Insert ID: " . mysqli_insert_id($conn);
} else {
    echo "Error inserting record: " . mysqli_error($conn);
}
?>

Real Output:

New record created successfully. Insert ID: 1

2. Read: Table से Data Fetch और Display करना

Table से records read करने के लिए SQL SELECT query और PHP function mysqli_fetch_assoc() का use किया जाता है:

<?php
$sql = "SELECT roll_number, name, email, course FROM students";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    while ($row = mysqli_fetch_assoc($result)) {
        echo "Roll No: " . $row["roll_number"] . " - Name: " . $row["name"] . " - Email: " . $row["email"] . " - Course: " . $row["course"] . "<br>";
    }
} else {
    echo "No records found.";
}
?>

Real Output:

Roll No: 101 - Name: Amit Sharma - Email: amit@example.com - Course: BCA

3. Update: Existing Record को Modify करना

Existing record बदलने के लिए SQL UPDATE statement और WHERE clause का use किया जाता है:

<?php
$sql = "UPDATE students SET course = 'MCA' WHERE roll_number = 101";

if (mysqli_query($conn, $sql)) {
    echo "Record updated successfully. Affected rows: " . mysqli_affected_rows($conn);
} else {
    echo "Error updating record: " . mysqli_error($conn);
}
?>

Real Output:

Record updated successfully. Affected rows: 1

4. Delete: Record को Remove करना

Record delete करने के लिए SQL DELETE FROM query use की जाती है:

<?php
$sql = "DELETE FROM students WHERE roll_number = 101";

if (mysqli_query($conn, $sql)) {
    echo "Record deleted successfully!";
} else {
    echo "Error deleting record: " . mysqli_error($conn);
}
?>

Real Output:

Record deleted successfully!

5. Database Connection Close करना

Script का काम खत्म होने पर database server resource free करने के लिए connection close करना जरूरी practice है:

<?php
mysqli_close($conn);
echo "Connection closed successfully.";
?>
Advertisement

PHP और MySQL का Environment Setup और Installation

Local computer पर PHP और MySQL run करने के लिए local server package install करना सबसे आसान तरीका है:

  • XAMPP: Cross-Platform, Apache, MariaDB/MySQL, PHP, Perl का complete package. Windows, Linux और Mac तीनों पर available है।
  • WAMP: Windows, Apache, MySQL, PHP package.
  • LAMP: Linux systems के लिए Apache, MySQL और PHP stack.
  • MAMP: macOS के लिए dedicated development stack.
' + existing_topic + '

XAMPP Control Panel: Apache और MySQL services start करने की status window.

Key Installation Steps:

  1. Download और Install XAMPP: Apache Friends website से setup file download करके default path (जैसे C:\xampp) में install करें।
  2. Services Start करें: XAMPP Control Panel खोलें और Apache और MySQL दोनों के सामने 'Start' button पर click करें।
  3. Default Ports: Apache default रूप से port 80 या 8080 पर listen करता है, और MySQL default रूप से port 3306 पर run होता है। अगर Skype या IIS इन ports को use कर रहे हों, तो config file में port बदलना पड़ता है।
  4. php.ini Configuration: PHP configuration file (php.ini) में ensure करें कि extension=mysqli और extension=pdo_mysql active हों (line के आगे semicolon ; न लगा हो)।
  5. MySQL User और Password Setup: XAMPP में default user root होता है और password blank (खाली) होता है। Production server पर command line से secure password सेट करें:
    ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewSecurePassword@123';
  6. phpMyAdmin GUI Tool: Database visual manage करने के लिए browser में http://localhost/phpmyadmin open करें। यहाँ से new database, tables create करना और SQL queries run करना बेहद आसान है।
  7. Firewall और Remote Access: Remote connection allow करने के लिए MySQL config file (my.ini) में bind-address = 0.0.0.0 सेट करें और port 3306 को firewall में allow करें।

Exam में कैसे लिखें (5 Marks Ready Answer)

University exam (BCA/B.Tech) में जब "Explain use of MySQL in PHP" पूछा जाए, तो full marks लाने के लिए नीचे दिए गए 10 points में answer लिखें:

  1. Introduction: PHP एक server-side scripting language है और MySQL एक open-source relational database management system (RDBMS) है।
  2. Main Purpose: इनका use dynamic, data-driven web applications (जैसे login system, e-commerce site) बनाने के लिए किया जाता है।
  3. Role Division: PHP client request process करके business logic execute करती है, जबकि MySQL actual data को tables (rows और columns) में store रखता है।
  4. Connecting Method: PHP 7/8 में database connect करने के लिए modern MySQLi extension या PDO का use किया जाता है।
  5. Connection Syntax: $conn = mysqli_connect("localhost", "root", "", "db_name"); से connection establish होता है।
  6. CRUD Operations: PHP SQL queries execute करके Create (INSERT), Read (SELECT), Update (UPDATE), और Delete (DELETE) operations perform करती है।
  7. Execution Function: Query run करने के लिए mysqli_query($conn, $sql) function call किया जाता है।
  8. Data Fetching: Database से rows retrieve करने के लिए mysqli_fetch_assoc($result) loop के अंदर use होता है।
  9. Connection Close: Script के end में resource release करने के लिए mysqli_close($conn) लिखना best practice है।
  10. Key Advantage: यह combination free, fast, platform-independent और secure है।

Common Mistakes Students Make (आम गलतियाँ)

Lab practicals और exams में students अक्सर ये गलतियाँ करते हैं, जिनसे बचना चाहिए:

  • पुराने mysql_* functions का use करना: कई पुरानी books में mysql_connect() या mysql_query() लिखा होता है। ये functions PHP 7.0 से remove हो चुके हैं। हमेशा mysqli_* या PDO लिखें।
  • Connection Failure Check न करना: mysqli_connect() के तुरंत बाद if (!$conn) check न लगाने से script आगे चलकर fatal error दे देती है।
  • SQL Injection Vulnerability: User input को बिना sanitize किए direct SQL query में concatenate करना severe security risk है। Real projects में prepared statements use करने चाहिए।
  • SQL Query Syntax Errors: SQL queries में string values के चारों ओर quotes (जैसे '$name') न लगाना या column names की spelling गलत लिखना।
  • Connection Close भूल जाना: High-traffic applications में mysqli_close() न करने से database server के connection pool limit exhaust हो सकती है।

Exam के लिए Important Questions

Short Questions (2–3 marks)

  1. PHP में MySQL database से connect करने वाले function का नाम और syntax लिखिए। (mysqli_connect("host", "user", "pass", "db"))
  2. MySQLi और PDO में दो मुख्य अंतर बताइए। (MySQLi सिर्फ MySQL के लिए है; PDO multiple databases को support करता है)
  3. CRUD operations का full form क्या है? (Create, Read, Update, Delete)
  4. mysqli_fetch_assoc() function का क्या काम है? (यह result set में से एक row को associative array के रूप में fetch करता है)
  5. MySQL का default port number क्या होता है? (Port number 3306)

Long Questions (5–10 marks)

  1. PHP में MySQL के use को detail में समझाइए। PHP script से MySQL database connect करने का complete code लिखिए।
  2. PHP और MySQL का use करके complete CRUD operations (Insert, Select, Update, Delete) के code examples और output समझाइए।
  3. MySQLi extension और PDO में comparison कीजिए। Web application development में database connection की security के लिए कौन-से steps लेने चाहिए?

FAQs

Connecting MySQL with PHP के लिए कौन-सा extension best है?

अगर आपका project सिर्फ MySQL database use कर रहा है, तो MySQLi extension fast और simple है। लेकिन अगर future में PostgreSQL या Oracle पर switch करने की संभावना हो, तो PDO use करना best practice है।

PHP script में MySQL database connection fail होने के मुख्य कारण क्या होते हैं?

Connection failure के मुख्य कारण हैं: MySQL server service start न होना (XAMPP control panel में stop होना), गलत username या password enter करना, database name मौजूद न होना, या port 3306 किसी दूसरे application से block होना।

PHP 8 में mysql_connect() function काम क्यों नहीं करता?

क्योंकि पुराना mysql_* API unsecure और outdated था। इसे PHP 5.5 में deprecate किया गया था और PHP 7.0 में officially remove कर दिया गया था। अब केवल MySQLi और PDO supported हैं।

phpMyAdmin क्या है और इसका क्या use है?

phpMyAdmin एक web-based graphical user interface (GUI) tool है, जिसे PHP में लिखा गया है। यह users को command line के बिना browser के ज़रिए MySQL database create करने, tables manage करने, SQL queries run करने और backup export/import करने की सुविधा देता है।

अक्सर पूछे जाने वाले प्रश्न (FAQs)

Quick Answer: PHP में MySQL का use dynamic websites और web applications में data को create, store और manage करने के लिए एक relational database management system (RDBMS) की तरह किया जाता है।
PHP Notes in Hindi | PHP नोट्स हिंदी में के पाठ्यक्रम में Use Of MYSQL in PHP in Hindi | MySQL का PHP में उपयोग हिंदी में एक महत्वपूर्ण अध्याय है। यह मुख्य सिद्धांतों, परीक्षा प्रश्नों और सूत्रों को समझने के लिए अत्यधिक आवश्यक है।
हाँ, NotesMedia पर आपको Use Of MYSQL in PHP in Hindi | MySQL का PHP में उपयोग हिंदी में के महत्वपूर्ण प्रश्नोत्तर, हल किए गए उदाहरण और नोट्स बिल्कुल मुफ्त मिलेंगे। अधिक अभ्यास के लिए आप हमारा ऐप भी डाउनलोड कर सकते हैं।
Advertisement

In this Chapter

Use Of MYSQL in PHP in Hindi | MySQL का PHP में उपयोग हिंदी में
Introduction to PHP in Hindi | PHP का परिचय हिंदी में
Flow Control Statements in Hindi | PHP में फ़्लो कंट्रोल स्टेटमेंट्स हिंदी में
Embedding PHP in Webpages in Hindi | वेबपेज में PHP को एम्बेड करना
How to Configure our framework in Hindi | फ्रेमवर्क को कॉन्फ़िगर करना हिंदी में
Difference between PHP and HTML in Hindi
Advance Features PHP in Hindi | PHP के एडवांस फीचर्सस हिंदी में
Expression in PHP in Hindi | PHP में एक्सप्रेशन हिंदी में
Juggling Expression in PHP in Hindi | PHP में जुगलिंग हिंदी में
Comments in PHP in Hindi | PHP में Comments हिंदी में
Whitespace in Hindi | Whitespace हिंदी में
Difference Between Echo and Print in PHP in Hindi | echo और print में अंतर हिंदी में
Line & Paragraph Brakes in PHP in Hindi | PHP में लाइन और पैराग्राफ ब्रेक्स हिंदी में
Date & Time Function of PHP in Hindi
Array Functions in Hindi
PHP Stack and Queue in Hindi
RAND Function in Hindi
String Functions in Hindi
PHP Form in Hindi
What is Form Tag in Hindi | Form टैग क्या है
PHP Elements in Hindi
Session & Cookies in Hindi
File Uploading in Hindi
Regular Expression in Hindi
Error Handling in PHP in Hindi
Send Email By PHP in Hindi