Database Connection in PHP


/ Published in: PHP
Save to your folder(s)

try to with mysqli_*


Copy this code and paste it in your HTML
  1. <?php
  2. if(basename(__FILE__) == basename($_SERVER['PHP_SELF'])) send_404();
  3. $dbHost = "localhost"; //Location Of Database usually its localhost
  4. $dbUser = "xxxx"; //Database User Name
  5. $dbPass = "xxxx"; //Database Password
  6. $dbDatabase = "xxxx"; //Database Name
  7.  
  8. $db = mysql_connect("$dbHost", "$dbUser", "$dbPass") or die ("Error connecting to database.");
  9. mysql_select_db("$dbDatabase", $db) or die ("Couldn't select the database.");
  10.  
  11. # This function will send an imitation 404 page if the user
  12. # types in this files filename into the address bar.
  13. # only files connecting with in the same directory as this
  14. # file will be able to use it as well.
  15. function send_404()
  16. {
  17. header('HTTP/1.x 404 Not Found');
  18. print '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">'."n".
  19. '<html><head>'."n".
  20. '<title>404 Not Found</title>'."n".
  21. '</head><body>'."n".
  22. '<h1>Not Found</h1>'."n".
  23. '<p>The requested URL '.
  24. str_replace(strstr($_SERVER['REQUEST_URI'], '?'), '', $_SERVER['REQUEST_URI']).
  25. ' was not found on this server.</p>'."n".
  26. '</body></html>'."n";
  27. }
  28.  
  29. # In any file you want to connect to the database,
  30. # and in this case we will name this file db.php
  31. # just add this line of php code (without the pound sign):
  32. # include"db.php";
  33. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.