<?
$test1 = mysql_connect("localhost","username","pass");
if (!$test1)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("database", $test1);
$letter = $_SERVER[REQUEST_URI];
$letter1 = substr($letter,6);
$query = "SELECT * FROM letter WHERE username='" . $letter1 . "'";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
?>
<style type="text/css">
body {
background-image: url('<? echo $row["backpic"]; ?>');
background-attachment:fixed;
margin-bottom:36px;
}
</style>
That is my code for retrieving a field from the mysql database and setting that field as a background image (the field contains a url).
For some reason this code doesn’t work. I have checked all the values and they are supposed to be correct because this exact same code exists in another file and it works perfectly there.
Please help!
There is no error generated.
By the way, there is line of code that comes before all this: include(“connectval.php”);
There that file has the same code as this but different variable names and the substring part is not there. I don’t think that interferes with this code though.
If this code works in a different file but not this one, it’s likely a path problem – are the URLs in the database fully qualified or relative?
What does the generated HTML look like? What exactly is in the database?
Steps I’d follow to debug this:
(I assume by “it doesn’t work” you mean you don’t see a background image? If so, you should be explicit!)
My guess is that urls in the database are in the form:
/Images/Image.jpgwhich works fine when the PHP is alongside theImagesfolder but wrong when loaded from a different folder – try making them fully qualified (http://www.example.com/Images/Image.jpg). If you can’t change the database, add thehttp://www.example.comyourself in code.EDIT:
As suggested in comments on OP by Jared Farrish:
Try printing out
$query. Copy/paste the string into MySQL Workbench or phpMyAdmin – Do you see the correct results?