Jump to content

I'm getting an unexpected t_string error


green917

Recommended Posts

Hey all,

 

Relative newbie here. I am trying to create a script that generates a random image (and the associated title) from my mySQL database. The code doesn't seem to be working as the image is blank and the title does not appear either. A new set of eyes may be able to find the problem. Thanks!

 

<?php
include 'open.php';
include 'opendb.php';

$offset_result = mysql_query( " SELECT FLOOR(RAND() * COUNT(*)) AS 'offset' FROM 'recipes' ");
$offset_row = mysql_fetch_object( $offset_result );
$offset = $offset_row->offset;
$result = mysql_query( " SELECT * FROM 'recipes' LIMIT $offset, 1 " );

print "<table width=\"366\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">";
print "<tr>";
print "<td class=\"recipe_head\">Healthy Lifestyle Gourmet Recipes</td>";
print "<tr>";
print "<td class=\"recipe_title\">{$result['title']}</td>";
print "</tr>";
print "<tr>";
print "<td><image src=\"{$result['image_path'].$result['image_name']}\" height=\"{$result['image_height']}\" width=\"{$result['image_width']}\" /></td>";
print "</tr></table>";

mysql_close();
?>

Link to comment
Share on other sites

Here, this should help:

 

<?php

include 'open.php';

include 'opendb.php';

 

$offset_result = mysql_query( " SELECT FLOOR(RAND() * COUNT(*)) AS 'offset' FROM 'recipes' ");

$offset_row = mysql_fetch_object( $offset_result );

$offset = $offset_row->offset;

$result = mysql_query( " SELECT * FROM 'recipes' LIMIT $offset, 1 " );

 

print "<table width=\"366\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">";

print "<tr>";

print "<td class=\"recipe_head\">Healthy Lifestyle Gourmet Recipes</td>";

print "<tr>";

print "<td class=\"recipe_title\">{$result['title']}</td>";

print "</tr>";

print "<tr>";

print "<td><image src=\"{".$result."['image_path']".$result."['image_name']}\" height=\"{".$result."['image_height']}\" width=\"{".$result."['image_width']}\" /></td>";

print "</tr></table>";

 

mysql_close();

?>

Link to comment
Share on other sites

It would help if you indicated which line the error was reported on.  I think your problem might be in this line:

 

print "<td><image src=\"{$result['image_path'].$result['image_name']}\" height=\"{$result['image_height']}\" width=\"{$result['image_width']}\" /></td>";

 

I'm not sure, but I don't think you can use the dot operator inside the curly-braces.  I'm not sure, because I almost never use curly-braces.  This type of statement is very difficult to read when working with your code, and prone to errors. There are a couple of different ways to clean it up.

 

First, instead of escaping double quotes inside a double-quoted string, switch to single quotes for one or the other.

 

My favorite method is to use the printf() function:

printf('<td><image src="%s" height="%d" width="%d" /></td>',
    $result['image_path'] . $result['image_name'], 
    $result['image_height'], $result['image_width']);

I have switched to using printf() and sprintf() for HTML and SQL generation almost entirely. The reason is clear. If you look at that code, you can see the HTML you are going to generate with the placeholders for the variables, then the variables are listed in the order they are applied.

 

Or use single quotes for the print and concatenate the variables:

print '<td><image src="' . $result['image_path'] . $result['image_name'] . 
    '" height="' . $result['image_height'] . '" width="' . $result['image_width'] . '" /></td>';

 

If you really like the curly-braces, I think the correct way would be to remove the "." between the image_path and image_name and replace it with more curly-braces (they are separate varaibles):

print "<td><image src=\"{$result['image_path']}{$result['image_name']}\" height=\"{$result['image_height']}\" width=\"{$result['image_width']}\" /></td>";

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.