How to make PHP interact with server's MySQL db?

How to make PHP interact with server's MySQL db?


Hey folks!

I’m a newbie and I wanted to create a website. After learning basic JS, HTML, CSS, MySQL and PHP, I’ve got a somewhat good idea what my page should do. What I don’t know is how to make the PHP page request and rewrite SQL data.

Does anyone recommend a good tutorial? How do I make SQL and PHP interact? Also, does anyone recommend a good free Django course?

Thanks a lot!


You can connect to differents types of database with PDO ( )


  function connect_to_db () {
    try {
      $host = 'localhost';
      $dbname = 'test_db';
      $user = 'username';
      $pass = 'password';

      $db = new PDO ("mysql:host=$host;dbname=$dbname;charset=utf8mb4", $user, $pass);
      $db->setAttribute (PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
      $db->setAttribute (PDO::ATTR_EMULATE_PREPARES, false);

      return $db;
    catch (PDOException $ex) {
      die_error ($ex->getMessage( ));
  $db_handle = connect_to_db ();
  /* example query */
  $stmt = $db_handle->prepare ('SELECT * FROM my_table WHERE id=?');
  $result = $stmt->execute (array ($element_id));
  // ...
  $data = $stmt->fetchAll (PDO::FETCH_ASSOC);


@P1xt Thanks! I only found it last because of all the hate of w3cfools… but this seems to me to be the best tutorial on the subject

@NeckersBOX Great!! I couldn’t do much with the manual, I’ll try reading it again now on the right section!