Hi there,
I recently built my first website for a friend showcasing his photography work. This was all in HTML/CSS but now I want to play around with PHP/MySQL and host the images in a database.
I have the HTML build here
I’ve created a database using phpMyAdmin which is called ‘samcamps_joesingh’ and a table called ‘portraits’ like this one: PHP Riot Tutorial.
I think I’m on vaguely the right track, but I wanted to place code in my HTML that will connect to this database. I’m working on the Portrait Gallery page of this site (Portrait Gallery) which is why it has no images on at present.
Ignoring the tutorial for the moment, I’ve tried adding the following into the body of the Portrait Gallery page:
<?php
$link = mysqli_connect('http://samcampsall.chrisfulton.co.uk', 'samcamps_joesingh', 'password');
if (!$link)
{
$output = 'Unable to connect to the database server.';
include 'output.html.php';
exit();
}
if (!mysqli_set_charset($link, 'utf8'))
{
$output = 'Unable to set database connection encoding.';
include 'output.html.php';
exit();
}
if (!mysqli_select_db($link, 'samcamps_joesingh'))
{
$output = 'Unable to locate the portraits database.';
include 'output.html.php';
exit();
}
$output = 'Database connection established.';
include 'output.html.php';
?>
output.html.php looks like this:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>PHP Output</title>
<meta http-equiv="content-type"
content="text/html; charset=utf-8"/>
</head>
<body>
<p>
<?php echo $output; ?>
</p>
</body>
</html>
That code is taken from Kevin Yank’s book on PHP and MySQL, but I’m aware I’m probably using it entirely wrongly and as I know almost nothing about what I’m doing, you’re probably unsurprised that this does nothing!
Am I anywhere near getting some results with this, or how should I best approach this? I tend to learn best by jumping in at the deep end and floundering a bit, but if I’m best left to drown in this instance, please feel free to put your foot on my head and finish me off.
What I’d like to achieve in the first instance is having a few images in the database table that are retrieved with PHP and displayed in the galley space.
Thanks for your patience and any and all advice