Issue with JOIN in PHP MySQL

Having a bit of a struggle here with adding JOINs to a query. I am connecting to two separate databases (on the same server). For this reason, I am writing this mysqli simply and will convert to a prepared statement once it’s working.

// REMOVED: DB VARIABLES

$conn = new mysqli($servername, $username, $password, $db_connective_data);
if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); }
$conn2 = new mysqli($servername, $username, $password, $db_resources);
if ($conn2->connect_error) { die("Connection failed: " . $conn2->connect_error); }

$sql = "SELECT * FROM downloads LEFT JOIN resource_data ON downloads.resource_id_REF=resource_data.resource_id WHERE downloads.user_basics_id_REF='$user_id'";
$result = $conn->query($sql);

$number_of_download_rows_returned = mysqli_num_rows($result) -1;

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        $resource_id_REF[] = $row['resource_id_REF'];
        $download_date[] = date('Y-m-d', strtotime($row['download_date']));
        $resource_title[] = $row['resource_title'];
        $resource_title_link[] = str_replace(" ", "-", $row['resource_title']);
    }
}

$conn->close();

A query without a JOIN works fine (albeit without returning the resource_title):

$sql = "SELECT * FROM downloads WHERE downloads.user_basics_id_REF='$user_id' ORDER BY downloads.download_date DESC";

What am I missing here? The first code sample will return no results. The second one will return three.

Any assistance is greatly appreciated.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s