I started working with php and mysql today. Basically, what I have is a blank page with pieces that I fill out from finding the identifier in the database. So, on my homepage, I have a url that looks like this:
<a href="content/display.php?id=id1">
And then in my display.php I have this:
<?php
include '../includes/header.php';
$id = $_GET['id'];
$mysqli = new mysqli('localhost','username','password','dbname');
if($result = $mysqli->query("SELECT * FROM portfolio WHERE id='".$id."'"))
{
while($row = $result->fetch_object())
{
$head = $row->head;
$img1 = $row->img1;
$img2 = $row->img2;
$img_url = $row->imgurl;
$img_thumb = $row->imgthumb;
$vid = $row->vid;
$swf = $row->swf;
$url = $row->url;
$url_text = $row->urltext;
$text = $row->text;
}
}
else echo $mysqli->error;
?>
This is a rare table in which not all of these fields will have information (many may be empty). They basically contain file names, and then in html I have code that looks like this:
if(isset($img1))
{
echo '<img src="images/'.$img1.'" />';
}
A few questions,
- Is this the best way to do this?
- Every time I visit display.php, do I open the database connection again? It may not be good ...
- , , , , , , . , html. ?
!