2

On my website, a user's background URL is stored with their data in a database. I need to get this value and store it in a variable. However, I am having trouble doing so, the code I have right now gives me an error but doesnt tell me what the error is.

My code:

<?php
error_reporting(E_ALL);
include 'config.php';
$pin = $_COOKIE["UID"];

// Create connection
$conn = new mysqli($db_servername, $db_username, $db_password, $db_name);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$query = "SELECT * from users where pin ='$pin'";
$result = $conn->query($query);

if ($conn->query($query) === TRUE) {
    $bg_url = $row[bg_url];
} else {

    echo "Error: " . $query . "<br>" . $conn->error;
}

echo $bg_url;

mysqli_close($conn);
?>
1
  • 1
    You don't ask for the error Commented Sep 19, 2015 at 21:38

1 Answer 1

2
$query = "SELECT * from users where pin ='$pin'";
$result = $conn->query($query);

Actually yes, the John's answer is correct, you're not using the return values properly, but you are neither accesing the variables properly, for example, I don't see where $row comes from. Try this and change it for array mode if you need to. I'm not very used to mysqli_* API, because I use PDO mostly.

if ($result) {
    while($row = $result->fetch_object()) {
        $bg_url = $row->bg_url;
    }
} else {

    echo "Error: " . $query . "<br>" . $conn->error;
}

echo $bg_url;
Sign up to request clarification or add additional context in comments.

2 Comments

Worked perfectly, Thanks!
Glad I could be helpful

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.