cameron9584
cameron9584

Reputation: 3

Query Structure: SELECT FROM places WHERE Type = 1

Okay so i am trying to display all entries in one of my database tables where the field 'Type' has a value of 1 but i keep getting an error. I'm not sure how to structure my query.

<?php
include 'page-start.php';
?>
<?php
$myQuery  = "SELECT places.*, Type.TypeName ";
$myQuery .= "FROM places ";
$myQuery .= "WHERE Type = '1' ";
$myQuery .= "INNER JOIN Type ON places.Type = Type.TypeID";

$result = $con->query($myQuery);
if (!$result) die('Query error: ' . mysqli_error($result));
?>



 <?php

            while($row = mysqli_fetch_array($result))
            {

    echo ' <div class="one-third column" id="education">';
    echo '<h3 class="place-head"><a href="educationdetails.php?ID=' . $row['PlaceID'] . '">' . $row['PlaceName'] . '</a></h3>';
    echo    ' <div class="a-image">';
    echo        '<img src="'. $row['ImageURL'] . '"/>';
    echo    ' </div>';
    echo        ' <div class="a-info">';


    echo        ' </div>';
    echo    '</div>';   
            }

    ?>

After playing around i managed to get it working by executing the query on its own underneath the first query like this:

<?php
$myQuery  = "SELECT places.*, Type.TypeName ";
$myQuery .= "FROM places ";
$myQuery .= "INNER JOIN Type ON places.TypeID = Type.TypeID";

$result = $con->query($myQuery);
if (!$result) die('Query error: ' . mysqli_error($result));
?>
<?php
$myQuery = "SELECT * FROM `places` WHERE `TypeID` = '1'";
$result = $con->query($myQuery);
if (!$result) die('Query error: ' . mysqli_error($result));
?>

I don't know if this was the correct way of getting round my problem but it works. Thanks for the help anyway guys.

Upvotes: 0

Views: 195

Answers (2)

Zero
Zero

Reputation: 11

I believe you have an SQL syntax error?

$myQuery  = "SELECT places.*, Type.TypeName ";
$myQuery .= "FROM places ";
$myQuery .= "WHERE Type = '1' ";
$myQuery .= "INNER JOIN Type ON places.Type = Type.TypeID";

The inner join should precede your predicate ("WHERE Type = '1'"):

$myQuery  = "SELECT places.*, Type.TypeName ";
$myQuery .= "FROM places ";
$myQuery .= "INNER JOIN Type ON places.Type = Type.TypeID";
$myQuery .= "WHERE Type = '1' ";

Upvotes: 1

Kyle Hale
Kyle Hale

Reputation: 8120

Don't pass $result to mysqli_error, that's not what it wants, it wants your $con variable passed in as the parameter.

Also, since you're using the mysqli object, just call $con->error to print the error message.

And since you're returning a mysqli_result objects, you should probably just use

while ($row = $result->fetch_array(MYSQLI_ASSOC) )

as your while loop command.

Upvotes: 0

Related Questions