CREATING A DYNAMIC ONLINE GALLERY 4. The code (Multiple domain web hosting)

CREATING A DYNAMIC ONLINE GALLERY 4. The code for submitting the query and extracting the first record from the result depends on which method of connection you are using. For the original MySQL functions, use this: // submit the query $result = mysql_query($sql) or die(mysql_error()); // extract the first record as an array $row = mysql_fetch_assoc($result); For MySQL Improved, use this: // submit the query $result = $conn->query($sql) or die(mysqli_error()); // extract the first record as an array $row = $result->fetch_assoc(); For PDO, use this: // submit the query $result = $conn->query($sql); // get any error messages $error = $conn->errorInfo(); if (isset($error[2])) die($error[2]); // extract the first record as an array $row = $result->fetch(); The code for the original MySQL extension and MySQL Improved is exactly the same as you used in the previous chapter. The PDO code, however, introduces a new method, fetch(), which gets the next record from the result set. You can t use a foreach loop like in the previous chapter, because you need to get the first result on its own. 5. All three methods now have the first record from the result set stored as an array in $row. This means that $row[’image_id’] contains the primary key of the first record, $row[’filename’] contains its filename, and $row[’caption’] contains its caption. You need the filename, caption, and the dimensions of the large version so that you can display the images in the main body of the page. Add the following code: // get the name and caption for the main image $mainImage = $row[’filename’]; $caption = $row[’caption’]; // get the dimensions of the main image $imageSize = getimagesize(’images/’.$mainImage); The getimagesize() function was described in Chapters 4 and 8. 6. You can now use this information to display the thumbnail, main image, and its caption dynamically. The main image and thumbnail have the same name, but you eventually want to display all thumbnails by looping through the full result set, so the dynamic code that needs to go in the table cell needs to refer to the current record in other words, $row[’filename’] and $row[’caption’], rather than to 325
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision J2ee Web Hosting services.

Leave a Reply