Jump to content

Print() with variables


benji87

Recommended Posts

Hi all im trying to display a news story with an uploaded image. Im new to working with images stored in a database so im not quite sure the best way to script it in different ways at the moment. But what im trying to do is get the result then refresh the headers and print out some html with the results variables embedded into the html as you can see in the script below:

[code]
<?php
require_once('includes/db.php');

$query=("SELECT * FROM ssrfc_test ORDER BY id DESC LIMIT 0, 1");
$getdata = mysql_query($query) or die(mysql_error());
if($row = mysql_fetch_assoc($getdata))
{

$imgtitle = htmlspecialchars($row["imgtitle"]);
$imgdata = $row["imgdata"];
$id = $row["id"];
$title = $row["title"];
$message = $row["message"];

}else{
        $errmsg = "MySql Error!";
}

header("Content-type: image/jpeg");
print '<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<table width="100%" border="1" cellspacing="2" cellpadding="0" bordercolor="#000000">
  <tr>
    <td width="2%">$id</td>
    <td width="2%">$title</td>
    <td width="2%">$message</td>
    <td width="2%">$imgtitle</td>
    <td width="92%">$imgdata</td>
  </tr>
</table>
</body>
</html>';
exit();
?>[/code]

All it does is just print the name of the variable. Ive tried a hundred different ways i can think of including echo() and printf() someone please help me out. Thanks
Link to comment
https://forums.phpfreaks.com/topic/26560-print-with-variables/
Share on other sites

Variables are only parsed when contained within double quotes.

[code=php:0]
print "<html>
<head>
<title>Untitled Document</title>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">
</head>

<body>
<table width=\"100%\" border=\"1\" cellspacing=\"2\" cellpadding=\"0\" bordercolor=\"#000000\">
  <tr>
    <td width=\"2%\">$id</td>
    <td width=\"2%\">$title</td>
    <td width=\"2%\">$message</td>
    <td width=\"2%\">$imgtitle</td>
    <td width=\"92%\">$imgdata</td>
  </tr>
</table>
</body>
</html>";
[/code]
Link to comment
https://forums.phpfreaks.com/topic/26560-print-with-variables/#findComment-121492
Share on other sites

Try:-

[code]
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?php
require_once('includes/db.php');

$query=("SELECT * FROM ssrfc_test ORDER BY id DESC LIMIT 0, 1");

$getdata = mysql_query($query) or die(mysql_error());

$num = mysql_numrows ($getdata);
if ($num !=0)
{
while ( $row = mysql_fetch_array( $getdata ) )
{
    $list ="<table width=\"100%\" border=\"1\" cellspacing=\"2\" cellpadding=\"0\" bordercolor=\"#000000\">";
    $list .="<tr>";
    $list .="<td width=\"2%\">".$row["id"]."</td>";
    $list .="<td width=\"2%\">".$row["title"]."</td>";
    $list .="<td width=\"2%\">".$row["message"]."</td>";
    $list .="<td width=\"2%\">".$row["imgtitle"]."</td>";
    $list .="<td width=\"92%\">".$row["imgdata"]."</td>";
    $list .="</tr>";
}
$list .="</table>";

echo ($list)
}
else
{ echo ("MySql Error");
exit();}
?>
</body>
</html>

[/code]

Or something close to that
Not sure how to fit in the special characters tho
Link to comment
https://forums.phpfreaks.com/topic/26560-print-with-variables/#findComment-121494
Share on other sites

if you shouldn't use while loops for single results, whats best in my instance?

[code]<?php

#create query
$sql = "select * from stu where stu_id=\"$stu_id\" ";

#exe query
$rs = mysql_query( $sql, $conn )
or die( "Could not execute Query");

while ( $row = mysql_fetch_array( $rs ) )
{
$list = "<table border=\"0\" align=\"center\" cellpadding=\"4\" width=\"100%\">";
$list .= "<tr>";
$list .= "<th class=table width=\"25%\">ID:</th>";
$list .= "<td class=tblleft>".$row["stu_id"]."</td>";
$list .= "</tr>";
$list .= "<tr>";
$list .= "<th class=table width=\"25%\">Name:</th>";
$list .= "<td class=tblleft>".$row["forename"]." ".$row["surname"]."</td>";
$list .= "</tr>";
$list .= "<tr>";
$list .= "<th class=table width=\"25%\">D.O.B:</th>";
$list .= "<td class=tblleft>".$row["birth_dt"]."</td>";
$list .= "</tr>";
}
$list .= "</table>";

#display details
echo( $list );
}
else
{ echo ("No details);
exit();}

?>[/code]

It will only ever find 1 match but it sits within the HTML body (using tenplates)
Link to comment
https://forums.phpfreaks.com/topic/26560-print-with-variables/#findComment-121515
Share on other sites

Ive also tried what you gave me round and it did work and only returned one result but it did exactly as the other scripts do just return binary instead of an image. I really dont understand as it works fine when you just print $imgdata and no html
Link to comment
https://forums.phpfreaks.com/topic/26560-print-with-variables/#findComment-121519
Share on other sites

Your telling the browser your sending it text....

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

As for your question Round. Just remove the loop. eg;

[code=php:0]
$row = mysql_fetch_array( $rs );
$list = "<table border=\"0\" align=\"center\" cellpadding=\"4\" width=\"100%\">";
$list .= "<tr>";
$list .= "<th class=table width=\"25%\">ID:</th>";
$list .= "<td class=tblleft>".$row["stu_id"]."</td>";
$list .= "</tr>";
$list .= "<tr>";
$list .= "<th class=table width=\"25%\">Name:</th>";
$list .= "<td class=tblleft>".$row["forename"]." ".$row["surname"]."</td>";
$list .= "</tr>";
$list .= "<tr>";
$list .= "<th class=table width=\"25%\">D.O.B:</th>";
$list .= "<td class=tblleft>".$row["birth_dt"]."</td>";
$list .= "</tr>";
$list .= "</table>";
echo $list;
[/code]
Link to comment
https://forums.phpfreaks.com/topic/26560-print-with-variables/#findComment-121572
Share on other sites

[quote]Nope thats not the solution still the same problem Shocked( please somebody[/quote]

You need to tell the browser your sending an image via a header call (this your allrerady doing). Unfortunately, this also meens you'll need to put the code to pull your image in a seperate script and place a link to this script within <img> tags.

At the moment your telling the broswer your sending an image, then your telling it your sending text, and then you actually do send text.

yet another reason not to store images in a database!
Link to comment
https://forums.phpfreaks.com/topic/26560-print-with-variables/#findComment-121616
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.