Connecting to MySQL with PHP - for the first time

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 :slight_smile:

PHP/MySQL Tutorial - Part 1
Look there, good place to start. That is where I started :slight_smile:
Now with PHP and databases you have to look at security. This may be new to you so try here.
PHP Freaks - PHP Help Tutorial: PHP Security
So, I think that should point you in the right direction.
AND
this.

Thanks Travis, those look like good tuts, so I’ll work through them and see if things become clearer :slight_smile:

I’m taking the lack of responses as a sign that my message caused much rolling of eyes! Will go back to basics for a while longer I guess :blush:

Increase error reporting, that should put you on the right path. :wink:


error_reporting(-1);
ini_set('display_errors', true);

$link = mysqli_connect('http://samcampsall.chrisfulton.co.uk', 'samcamps_joesingh', 'password');
...
...
...

Thanks for the reply Anthony :slight_smile:

I tried popping that code in, but still nothing! I then noticed that in the page source in Firefox, all the PHP code was still there in a fetching shade of pink…

So… now that I’m saving my pages as actual .PHP files, I’m getting some results! :blush:

Errors, but results:
Portrait Gallery.PHP!

Adding the debugging code is great, so I’ll be off trying to work out what that means. A side question though - after the PHP executes on that page I lose the further content that was there (a couple of navigation links and the page footer, as seen here: Portrait Gallery.HTML. Any pointers as to why?

Thanks :slight_smile:

Heh.

I would guess the script is terminated (due to error) prior the that particular section, feel free to pop the code up here though.

Thanks man - a little moment of clarity has allowed me to see that I was getting my MySQL databases mixed up with my users in my code.

It now works!: All praise to the God’s of the internet!

So now I can sit back and call myself a PHP developer, right? :shifty:

Now to actually put stuff in the DB and retrieve it to the webpage…

Seriously tho, thanks for your input - I’m attempting this as I need a new way to earn a living, and as you can see, I’m at a very early stage - any and all responses are encouraging and motivating, so I really appreciate it :slight_smile: