DemCodeLines
DemCodeLines

Reputation: 1920

database connection not working

<?
$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.

Upvotes: 0

Views: 2496

Answers (3)

msmafra
msmafra

Reputation: 1714

What is <? echo $row["backpic"]; ?> outputting?

Try this:

<?php
    // You can enable the report of all PHP errors using ONE of the 3 lines below
    ini_set('error_reporting', E_ALL);
    //error_reporting(E_ALL);
    //error_reporting(-1);


$myhost = "localhost";
$myuser = "myuser";
$mypwd = "mypassword";
$mydb = "mydatabase";
$mysqli = new mysqli($localhost, $myuser, $mypwd, $mydb);

if ($mysqli->error) {
    printf("Connection failed: %s\n", $mysqli->error());
    exit();
}

$letter = $_SERVER[REQUEST_URI];
$letter1 = $mysqli->real_escape_string(substr($letter,6));
$query = sprintf("SELECT * FROM letter WHERE username = %d", $username);
$result = $mysqli->query($query);

// $row = $result->fetch_array(MYSQLI_NUM); //$row[0]
// $row = $result->fetch_array(MYSQLI_ASSOC); //$row['field']
// $row = $result->fetch_array(MYSQLI_BOTH); //$row['field'], $row['0']
// $row = $result->fetch_assoc(); //$row['field']
$row = $result->fetch_object(); //$row->['field']

/*if($row > 0) :
    printf ("<h2>%s (%s)</h2>", $row[0], $row['backpic']);
endif;*/

$result->close();
$mysqli->close();
?>

<style type="text/css">
body {
    background-image: url('<?= $row->backpic; ?>');
    background-attachment:fixed;
    margin-bottom:36px;
    }
</style>

Upvotes: 1

Basic
Basic

Reputation: 26756

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!)

  • Work backwards - The browser isn't showing the image. Why?
  • The browser uses the image URL to load the image - is it correct in the source of the page?
  • If the URL is correct, copy/paste it into the address bar - does the image load?
  • If it's not correct, look at what's in the database and work out what you need to do to correct it.

My guess is that urls in the database are in the form: /Images/Image.jpg which works fine when the PHP is alongside the Images folder 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 the http://www.example.com yourself 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?

Upvotes: 2

Ted Avery
Ted Avery

Reputation: 5689

You are trying to output the variable $a951168545, which does not exist.

You've loaded the results of your database query as an array into $row, so you need to output an index of $row. What you should probably be echoing out in the background-image part is $row['backpic'].

If all else fails, insert the line var_dump($row) which will show you exactly what is being loaded into your $row variable, and should indicate what you need to do to display the value you are looking for.

Upvotes: 1

Related Questions