Related Topics

what is Protocols in Hindi

What is a Program in Hindi

What is a Secure Connection in Hindi

Introduction to WWW in Hindi

What are Development Tools in Hindi

What is a Web Browser in Hindi

What is a Server in Hindi

What is a UNIX Web Server in Hindi

What is Logging Users in Hindi

What is Dynamic IP Web Design in Hindi

Web Site Design Principles in Hindi

Site Planning in Hindi

Website Navigation in Hindi

what is Web Systems Architecture in Hindi

Architecture of Web-Based Systems in Hindi

Client-Server Architecture in Hindi

What is Caching in Hindi

: Proxies in Hindi

What is an Index in Hindi

What is a Load Balancer in Hindi

What is a Queue in Hindi

Web Application Architecture in Hindi

JavaScript in Hindi

Client-Side Scripting in Hindi

Introduction to Simple JavaScript in Hindi

: JavaScript Variables in Hindi

What is a Function in JavaScript in Hindi

What are Conditions in JavaScript in Hindi

What are Loops in JavaScript in Hindi

What is Repetition (Looping) in JavaScript? in Hindi

What is an Object in JavaScript in Hindi

JavaScript Own Objects in Hindi

DOM in Hindi

What is a Web Browser Environment in Hindi

Forms in JavaScript in Hindi

DHTML in Hindi

What are Events in DHTML in Hindi

Browser Control in JavaScript in Hindi

AJAX in Hindi

AJAX-based Web Application in Hindi

Alternatives to AJAX in Hindi

XML in Hindi

Uses of XML in Hindi

Simple XML in Hindi

XML Key Components in Hindi

What is DTD (Document Type Definition) in Hindi

What is XML Schema (XSD) in Hindi

XML with Application in Hindi

XSL in Hindi

XSLT in Hindi

Web Service in hindi

PHP in Hindi

Server-Side Scripting in Hindi

PHP Arrays in Hindi

PHP Functions in Hindi

PHP Forms in Hindi

Advanced PHP Databases in Hindi

Introduction to Basic Commands in PHP in Hindi

Server Connection in PHP in Hindi

Understanding Database Selection in PHP in Hindi

PHPMyAdmin in Hindi

Database Bugs in Hindi

PHP Database Query in Hindi

Related Subjects

Database Creation in PHP in Hindi

RGPV University / DIPLOMA_CSE / Web Technology

Database Creation in PHP

What is a Database?

डाटाबेस एक संग्रह है जहाँ हम डेटा को संरचित तरीके से स्टोर करते हैं। PHP के साथ डाटाबेस बनाने के लिए हम MySQL का उपयोग करते हैं। MySQL एक open-source relational database management system (RDBMS) है। यह डेटा को tables में स्टोर करता है, जिससे इसे manage करना आसान हो जाता है।

Setting Up the Environment for Database Creation

  • सबसे पहले, आपको PHP और MySQL की environment setup करनी होती है। इसके लिए, आपको XAMPP या WAMP जैसे software की जरूरत होगी जो PHP, MySQL और Apache server को एक साथ provide करते हैं।
  • आपको Apache और MySQL servers को start करना होगा ताकि आप database creation पर काम कर सकें।
  • इसके बाद, phpMyAdmin का इस्तेमाल करके आप MySQL database को manage कर सकते हैं। यह web-based interface है जो MySQL database के साथ interact करता है।

Database Connection in PHP

PHP में database connection बनाने के लिए हम MySQLi या PDO (PHP Data Objects) का उपयोग करते हैं। यहाँ हम MySQLi का उदाहरण देखेंगे:

connect_error) { die("Connection failed: " . $conn->connect_error); } echo "Connected successfully"; ?>

यह कोड PHP के द्वारा MySQL server से कनेक्ट होता है और यदि कनेक्शन सही नहीं होता तो error message दिखाता है।

Creating a Database

अब जब हमारा connection सेटअप हो चुका है, तो हम नया डाटाबेस बना सकते हैं। PHP के द्वारा डाटाबेस बनाने के लिए हम SQL query का उपयोग करते हैं।

query($sql) === TRUE) { echo "Database created successfully"; } else { echo "Error creating database: " . $conn->error; } ?>

यह कोड "myDatabase" नाम का एक नया डाटाबेस बनाएगा। यदि कोई समस्या आती है तो error message दिखाई देगा।

Creating a Table in the Database

अब हम उस डाटाबेस में एक टेबल बनाएंगे। टेबल बनाने के लिए हम CREATE TABLE SQL statement का उपयोग करते हैं।

select_db("myDatabase"); // Create table $sql = "CREATE TABLE Users ( id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, name VARCHAR(50) NOT NULL, email VARCHAR(50) NOT NULL )"; if ($conn->query($sql) === TRUE) { echo "Table Users created successfully"; } else { echo "Error creating table: " . $conn->error; } ?>

इस कोड में, "Users" नाम का टेबल बनता है जिसमें तीन कॉलम हैं: id, name, और email।

Inserting Data into the Table

अब हम इस टेबल में डेटा insert करेंगे। इसके लिए INSERT INTO SQL statement का उपयोग करते हैं।

query($sql) === TRUE) { echo "New record created successfully"; } else { echo "Error: " . $sql . "
" . $conn->error; } ?>

यह कोड "Users" टेबल में एक नया रिकॉर्ड insert करता है जिसमें नाम "John Doe" और email "john@example.com" है।

Retrieving Data from the Table

हम SELECT SQL query का उपयोग करके डेटा retrieve कर सकते हैं।

query($sql); if ($result->num_rows > 0) { // Output data of each row while($row = $result->fetch_assoc()) { echo "id: " . $row["id"]. " - Name: " . $row["name"]. " - Email: " . $row["email"]. "
"; } } else { echo "0 results"; } ?>

यह कोड "Users" टेबल से सभी डेटा को fetch करता है और उसे display करता है।

Updating Data in the Table

UPDATE SQL query का उपयोग करके हम table में डेटा update कर सकते हैं।

query($sql) === TRUE) { echo "Record updated successfully"; } else { echo "Error updating record: " . $conn->error; } ?>

यह कोड "Users" टेबल में id=1 वाले रिकॉर्ड का email अपडेट करता है।

Deleting Data from the Table

DELETE SQL query का उपयोग करके हम table से डेटा delete कर सकते हैं।

query($sql) === TRUE) { echo "Record deleted successfully"; } else { echo "Error deleting record: " . $conn->error; } ?>

यह कोड "Users" टेबल से id=1 वाले रिकॉर्ड को delete करता है।

Handling Errors in PHP

PHP में errors को handle करने के लिए हम try-catch blocks का उपयोग कर सकते हैं। यह सुनिश्चित करता है कि यदि कोई error आती है, तो उसे properly handle किया जा सके।

connect_error) { throw new Exception("Connection failed: " . $conn->connect_error); } } catch (Exception $e) { echo "Caught exception: " . $e->getMessage(); } ?>

यह कोड PHP exceptions का उपयोग करता है ताकि यदि कोई error आए, तो उसे पकड़कर handle किया जा सके।

FAQs

Database एक संरचित संग्रह होता है, जहां डेटा को स्टोर किया जाता है। PHP में, MySQL जैसे relational database management system (RDBMS) का उपयोग करके डेटा को tables में संग्रहित किया जाता है।

PHP में डाटाबेस बनाने के लिए, आप MySQLi या PDO का उपयोग करके SQL query "CREATE DATABASE" लिखते हैं। यह एक नया डाटाबेस बनाएगा, जिसे बाद में आप tables और data के लिए उपयोग कर सकते हैं।

PHP को MySQL डाटाबेस से कनेक्ट करने के लिए, आपको MySQLi या PDO का उपयोग करना होगा। आपको डाटाबेस की जानकारी जैसे server name, username, और password provide करनी होती है।

PHP में डेटा insert करने के लिए, आप "INSERT INTO" SQL statement का उपयोग करते हैं। आपको insert करने वाले data को SQL query में specify करना होता है और फिर query को execute करना होता है।

PHP में डेटा retrieve करने के लिए, आप "SELECT" SQL query का उपयोग करते हैं। यह query डाटाबेस से rows को fetch करती है और फिर आप उसे PHP में process कर सकते हैं।

MySQLi (MySQL Improved) PHP का एक extension है, जो MySQL के साथ interaction के लिए उपयोग किया जाता है। यह database connection, queries और transactions के लिए सुविधाएं प्रदान करता है।

Please Give Us Feedback