PHP MySQL Connect In Hindi

📔 : PHP 🔗

PHP में MySQL Database से 3 method से connect कर सकते हैं।

  1. mysqli  procedural
  2. mysqli Object Oriented
  3. PDO

इनमे से आप कोई method use कर सकते हैं। main difference इनमे यही है कि PDO 12 different database driver support करता है जबकि mysqli सिर्फ MySQL को ही support करता है।


इस PHP MySQL series में आप MySQL procedural पढ़ेंगे।

PHP mysqli_connect()

Database connect करने के लिए PHP Predefined Function mysqli_connect() का use करते हैं।


Syntax :

 mysqli_connect(hostname, username, password,database_name);
  • hostname required होता है ,यह host name या IP Address हो सकता है ।
  • username आपके database का username होता है , default root होता है।
  • password आपके database का password होता है , अगर password set नहीं है तो इसे blank छोड़ दें।
  • database_name database का name होता है , हालाँकि यह optional होता है।

File : php_mysql_connect.php

Copy Fullscreen Close Fullscreen
<?php
$connection = mysqli_connect('localhost', 'root', null);
if(! $connection)
echo 'Database connection error : '.mysqli_connect_error();
else
echo 'Database connected successfully . ';
?>
Output
Database connected successfully .

ऊपर दिए गए example में mysqli_connect_error() function का use database connection error को show करने के लिए किया गया है।


हो सकता है कि server name localhost work न करे , तो वहां पर आप localhost:3307 use कर सकते हैं। और अगर आपको database के credentials check करने हो तो आप C:\xampp\phpmyadmin\config.inc.php file में देख सकते है।

Important
By chance अगर database credentials गलत हुए तो ये message show होगा -
Warning: mysqli_connect(): (HY000/1045): Access denied for user 'root'@'localhost' (using password: NO) in C:\xampp\htdocs\file_name.php
कुछ इस तरह से error message show होता।


PHP mysqli_close

बैसे तो जब भी PHP Script end होती है बैसे ही database connection automatically close हो जाता है , But फिर भी अगर आप connection close करना चाहते हैं तो mysqli_close() function का use करके database connection close कर सकते हैं।

mysqli_close($connection);

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers