How to use PHP with MySQL: the complete tutorial (with examples)

65 thoughts on “How to use PHP with MySQL: the complete tutorial (with examples)”

  1. I was able to complete the first step, creating a new user and schema, but when I ran the 1st PHP code I got:

    ‘; echo ‘Error number: ‘ . mysqli_connect_errno() . ‘
    ‘; echo ‘Error message: ‘ . mysqli_connect_error() . ‘
    ‘; die(); } echo ‘Successfully connected!

    What am I doing wrong?

  2. I need sample PHP code for:
    Read Mysql table and create a radio button for 2 items from each row.
    After buttons are selected submit results to different database.

    Example; A person would select 15 items from a choice of 30 items.
    The selected items would be placed in a table for each user.

    I hope you can help….I’ve been having a hard time doing this.
    I’m a beginner.
    Do you provide help for a fee?

    Thanks in advance,

  3. Hi Alex,

    This lesson is wonderful, and each example works perfectly, but I’m having trouble understanding how to make it all function together with a remote MySQL server on the internet, with data sent from and received back to JavaScript active on my local browser. Googling for how to move data between JS and PHP brings up complex examples, each of which fails to explain how to fit their pieces of code into the bigger picture of what I’m trying to do.

    So, have you made — or do you have a link to — a complete working example to serve as a guide for doing the following? Accept items such as keyboard typed text, file upload, or a complex array from my web page’s javascript, and send it to a remote host on the internet to be stored in a MySQL database. And likewise do the reverse, bringing such data back from the remote database, and return it to be handled by javascript on a web page.

    You’ve already provided most of the pieces needed in this lesson, except for how to allow local JavaScript to handle the data both ways, and control the process through the keyboard.

    (You requested the website below, but it is currently only where I am learning, and so is only partly functional.)

    With appreciation,
    Tommy Paul

    • Hello Tommy,
      The idea is to use AJAX connections from JavaScript to execute a remote PHP script. The PHP script is executed just like if it was called in the standard way.
      Basic AJAX implementations are quite simple.

    • MySQLi quoting (or escaping) is specific for MySQL and it’s guaranteed to work properly.
      PDO’s quote() does escape characters such as ‘ and “, but it’s not guaranteed to work with all PDO drivers. In fact, the documentation says that some drivers do not support quote() at all.

      So, quote() will probably work just fine with MySQL in most cases, but it’s not as reliable as MySQLi’s escaping.
      (I’m not aware of any real case where quote() fails, though).

  4. It is better to define the primary key for the order_products table to (product_id, order_id) i.e. the key is made up of the primary keys of the products and orders tables.

  5. Hi, Alex! What if we only want to make user can get the value from MYSQL database by just clicking button to select the value, it will automatically multiplied by 10/100 and pop-up on screen displaying the final price (original price – discount). I’m currently new in working with PHPmyadmin, and I’m stuck at getting and showing the data from database onto web so user can choose them by clicking it but the function not giving the result as expected I don’t know why. I have searched for solution of this problem but I still cannot solve it. Thanks a lot for this article and I will be very glad if you give me some technical steps or suggestions because I don’t want to lose my job…

    • Hello Hani,

      Let’s take one step at a time.
      First, are you able to get the value from the database with PHP? You can copy one of the examples from this tutorial and try it.

      Next, to multiply the value you can simply use the * operator. For example:
      $value = $row[‘db_value’] * 100;

      There are other ways but this is the most simple.
      Finally, you need to output the value in the web page.

      To give you more specific help I need to know where you are stuck exactly. Can you join my Facebook group so we can keep talking there? Here is the link:

  6. Wow! Thank you Alex, this was incredibly useful. I am coming from C# and .NET environment learning PHP for the first time, this tutorial is very on point and I picked up a lot of understanding coding along side it. Thank you for this.

    One question, when using OOP, when is it appropriate to use MySQLi vs. PDO? Is it entirely the programmer’s preference, or are there situations when one is preferable? I guess I ask because I wonder if I should really get familiar with one I like (PDO seems closest to what I am used to) or should I really know both well?

    Thanks again!

    • Hi David,

      It’s really a matter of personal preference.
      PDO has a more structured OOP syntax, so if you are familiar with OOP you will probably like PDO more.


Leave a Comment