0

I'd really like some help with the following MySQL / PHP problem (maybe bug?)

I'm trying to retrieve and display an array of data from my database using MySQL / PHP, but when I echo out the array, it returns the first value as 'null'.

So, even though the database has the following info:

"Example 1", "Example 2", "Example 3"...

The php echos out:

"null", "Example 2, "Example 3"

I would imagine this would be a common problem, but I haven't managed to find the required information elsewhere on the internet, so I'm hoping you kind folks can help.

My PHP

/* If connection to database, run sql statement. */
if ($conn) {
    $fetch = mysql_query("SELECT column FROM table WHERE approved = '1' ");

    // declare empty array to fill later
    $result = array();

    // make sure the MySQL pointer is looking at the first row
    mysql_data_seek($fetch, 0);


    while ($row = mysql_fetch_array($fetch, MYSQL_ASSOC)) {

        foreach ($row as $value) {

            $row_array = $value;

            // push info into new array with just the value
            array_push($result, $row_array);
        }
    }
}
/* Free connection resources. */
mysql_close($conn);

/* Toss back results as json encoded array. */
echo json_encode($result);

UPDATE

New code courtesy of Mark B:

if ($conn)
{
$sql = "SELECT column_name FROM table WHERE comment_approved = '1' "; 
$query = mysql_query($sql) or die(mysql_error());

$result = array();
while ($row = mysql_fetch_assoc($query)) {
    $result[] = $row['column_name'];

}

}

/* Free connection resources. */
mysql_close($conn);

/* Toss back results as json encoded array. */
echo json_encode($result);

NOTE:

The 'null' problem still occurs with or without the:

 mysql_data_seek($fetch, 0);

as that appears to do nothing.

Any help would be great!

SOLVED

Thanks to Mark B who pointed out that the problem was probably in the database rather than the PHP, it turned out there was the character ` lurking where there should have been a '. This caused the information to appear 'null'.

2
  • why you use seek? it is already at 0 in the beginning? and yes, use or die(mysql_error()); Commented May 16, 2011 at 19:47
  • I don't think you need the mysql_data_seek() function, since mysql_query() should return the result set with the pointer at 0 already. You should really only need that if you're reusing the same data set after iterating through it (like you are in your while loop). Commented May 16, 2011 at 19:47

3 Answers 3

4
$sql = "SELECT column FROM table WHERE approved = '1'";
$result = mysql_query($sql) or die(mysql_error());

$data = array();
while($row = mysql_fetch_assoc($result)) {
    $data[] = $row['column'];
}

echo json_encode($data);

You shouldn't have to do the seek, as you've not done anything with the result at the time. And since you're only fetching a single column from the database, there's no need for the inner foreach() loop either.

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

13 Comments

+1 for writing what I was going to write and saving me the typing!
Thanks very much for your input, I've tried the above code and also removed the 'seek', but I'm still getting the same results.
You may want to look at the data in the database. Run the query manually in the mysql monitor. PHP won't create a null entry unless the data's coming out of the query with a null in it.
I'm in PHPMyAdmin, running the above SQL query brings 10 results, all of which have the correct information, however when run on the website it brings 10 results, but the first is 'null'. :/
What kind of data are we talking about here? Other than overly-large blobs and possibly unicode text that's corrupted, I can't think of anything that'd cause mysql and/or php to convert a field's data into "null" in something so simple.
|
0

Try removing the call to mysql_data_seek. I see no reason why the MySQL pointer wouldn't already point to the first row at the first call to mysql_fetch_array.

Comments

0

You could try removing mysql_data_seek($fetch, 0); as the pointer will already be on the first record if you have just made the query.

Comments

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.