How can I connect MySQL to PHP? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How can I connect MySQL to PHP?

Database and web management.

20th Jul 2020, 7:20 PM
Ogunleke Samuel Ayomide
Ogunleke Samuel Ayomide - avatar
2 Answers
+ 2
The easiest way is to use PHP's standard mysqli functions. https://www.php.net/manual/en/mysqli.query.php The following example copied from the above article should get you started: <?php $link = mysqli_connect("localhost", "my_user", "my_password", "world"); /* check connection */ if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } /* Create table doesn't return a resultset */ if (mysqli_query($link, "CREATE TEMPORARY TABLE myCity LIKE City") === TRUE) { printf("Table myCity successfully created.\n"); } /* Select queries return a resultset */ if ($result = mysqli_query($link, "SELECT Name FROM City LIMIT 10")) { printf("Select returned %d rows.\n", mysqli_num_rows($result)); /* free result set */ mysqli_free_result($result); } If you run into errors, be sure that: - your mysql database is created. - your mysql server is running - php_mysqli extension is enabled in your PHP interpreter. PDO is also popular and a good choice.
20th Jul 2020, 7:45 PM
Josh Greig
Josh Greig - avatar
20th Jul 2020, 7:43 PM
XXX
XXX - avatar