0
function get_subject_by_id($subject_id){
    global $connection; 
    $query = "SELECT * ";
    $query .= "FROM subjects ";
    $query .= "WHERE 'id' ={$subject_id} "; 
    $query .= "LIMIT 1 "; 
    $result_set = mysql_query($query, $connection); 
    confirm_query($result_set); 
    // if no rows are returned fetch_array will return false
    if(!is_null($result_set)){
    echo "got here";
    echo $result_set; 
    } 
    if($subject=mysql_fetch_array($result_set)){ // not working properly
        echo "testing fetch array";
        return $subject; 
    }else {
        return NULL; 
    }

The fetch query keeps returning false although I have 3 items in my table. Some of the code obviously is testing to see if I got result back and those all work but the fetch array isn't executing the if statement because it thinks that it is false. Please help a few hours of frustration belong to this problem

2 Answers 2

3

You have the wrong type of quotes in your query, they should be backticks:

$query .= "WHERE `id` ={$subject_id} "; 
                 ^  ^

Quotes make id a string, not a column name. Since the string id is never equal to $subject_id, the query doesn't return anything.

Sign up to request clarification or add additional context in comments.

1 Comment

thank you for sharing that, it makes more sense as to why it wouldn't work this solved the issue i was having on the client side. Thank you.
1

I think it's because the id column is enclosed in single quotes rather than back ticks

$query .= "WHERE 'id' ={$subject_id} "; 

should be

$query .= "WHERE id ={$subject_id} "; 

Or

$query .= "WHERE `id` ={$subject_id} "; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.