Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm a novice at PHP to do MySQL manipulations. When the problem is boiled down, the database has 2 columns - city, borough. Some cities have a borough and the field is populated. Some cities do not, and the borough field is null. I only want to retrieve data into an array if the borough field is populated. My code looks like:

    while ($row = mysql_fetch_array($fetch, MYSQL_ASSOC)){
        if ( what_column_identifier_goes_here? != null ){
        /* For each row containing borough_data, put it in the return array */
        $row_array['city'] = $row['borough']; 
        array_push($return_arr,$row_array);
        }
    }

I tried searching for an answer, but I couldn't figure out good terms. From the search, I suspect this is deprecated. For the moment, I just want to get something working and then I'll learn PDO and make a whole bunch of changes in several scripts.

Thanks for any help.

share|improve this question
    
Just use a better SELECT query. –  hjpotter92 Jan 30 '13 at 21:22
    
I tried adding AND borough != NULL to the working query. That doesn't work. What should I add to the WHERE clause? –  Mike_Laird Jan 31 '13 at 1:48
    
Use borough IS NOT NULL or borough <> NULL. –  hjpotter92 Jan 31 '13 at 2:04
    
Thanks, Back in a Flash. That works. If your solution was in an Answer, I would have checked it. –  Mike_Laird Jan 31 '13 at 17:09
add comment

3 Answers

up vote 0 down vote accepted
if ($row['borough']) // do something

Or do it in the query:

SELECT city, borough FROM table WHERE borough IS NOT NULL
share|improve this answer
add comment

assuming your query looks like

SELECT city, borough FROM ...

then you can use mysql_fetch_assoc(), which returns a simple associative array: fieldname -> value: (no need for the extra fetch parameter)

if (!empty($row['borough'])) {
    $city stuff....
}

with fetcharray, you have to count positions yourself:

SELECT field1,  field2,  field3, ...  <-- in SQL
       $row[0], $row[1], $row[2], ... <--- in PHP
share|improve this answer
add comment
SELECT FROM TABLE WHERE borough<>''

perhaps this can help you quickly. if you want more help then please visit at http://www.w3made.com

share|improve this answer
    
I used your phrase to replace my if statement above. Don't know if that was your intent. Anyway the following does not work. if ( SELECT FROM TABLE WHERE borough<>'' ){ –  Mike_Laird Jan 31 '13 at 1:54
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.