An open API service indexing awesome lists of open source software.

https://github.com/codeadamca/php-mysql-images

A basic sample of integrating images into a MySQL database.
https://github.com/codeadamca/php-mysql-images

images mysql php

Last synced: about 2 months ago
JSON representation

A basic sample of integrating images into a MySQL database.

Awesome Lists containing this project

README

        

# Using PHP to Display Images from a MySQL Database

A basic sample of integrating images into a MySQL database.

## The End Goal

The `images.sql` file in the repository includes a list of Instagram images that can be imported into your MySQL database. There are also three image files in the repository that should be placed in the same folder as your PHP file. Once those have been imported we will use PHP and SQL to display the data from the MySQL database in an HTML webpage.

There are multiple methods of retrieving data from a MySQL database using PHP. For simplicity sake the example below will use a series of `mysqli` PHP functions.

## Steps

1. Open up phpMyAdmin.

If you're using a local server phpMyAdmin can usually be accessed by starting your server and then clicking on the phpMyAdmin link. If you're using a hosting account there will be a link to phpMyAdmin in your control panel.

Once you have phpMyAdmin open, click on the import tab and select the `images.sql` file from this repository. This will create a table called `links` and populate it with some sample data.

2. Place the images form the repository in the same folder as your PHP.

3. Create a new file and name it `images.php`. In that file place the following code:

```php
';
echo 'Error Message: ' . mysqli_connect_error() . '
';
exit;
}

?>



PHP, MySQL, and Images

PHP, MySQL, and Images

';
exit;
}

echo '

The query found ' . mysqli_num_rows($result) . ' rows:

';

while ($record = mysqli_fetch_assoc($result))
{
echo '


';
}

?>


?>
```

The first line of PHP will initiatie a connection to your MySQL server. The `mysqli_connect` function requires a host, username, password, and database name.

If you are using a local PHP server link MAMP or WAMP your host is `localhost` and your username and password are likely both `root`. This may vary depending on how you set up your local host. The database name will be whataver you named your database. If you don't have one go ahead an create one.

If you are using a hosting account, your MySQL user, password, and database will need to be created in your hosting control panel. There is likely help in your control panel on what to use for your host.

The second part of the above code is not complete. The next few steps will complete the PHP script.

> [More information on PHP and `mysqli_connect()`](https://www.php.net/manual/en/function.mysqli-connect.php)

4. Add PHP to th existing while loop to output the name and image:

```php
';
echo '

'.$record['name'].'

';
echo '';

}

?>
```

***

## Repo Resources

* [Visual Studio Code](https://code.visualstudio.com/)
* [Filezilla](https://filezilla-project.org/) (or any FTP program)

Full tutorial URL: https://codeadam.ca/learning/php-mysql-images.html