+ 1
How can I connect MySQL to PHP?
Database and web management.
2 Respostas
+ 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.