0

I really, really need help with the following code. I just can't figure out why it's not working.

There is a MySQL Table that I need to query and I simply need to return one row for a specific ID. However mysql_fetch_array only returns the first column from that table - and I'm dying from frustration...

$sqlCommand = "SELECT * FROM sqlInformationen WHERE ID = 2"; 

$sqlConn = mysql_connect($config['mysql_host'],$config['mysql_user'],$config['mysql_pass']);
mysql_select_db($config['db_name']) or die ("Fehler bei der Verbindungsaufnahme."); 

$result = mysql_query($sqlCommand); 
$row = mysql_fetch_array($result);  

echo $row['infTitel'];

The database contains fields "ID", "infTitel", "infZusammenfassung", etc. However only echo $row['ID'] returns a value.

Would greatly appreciate help.

Thanks.

5
  • 1
    What happens when you print_r($row); ? Commented Aug 27, 2012 at 11:29
  • 2
    Please, don't use mysql_* functions for new code. They are no longer maintained and the community has begun the deprecation process. See the red box? Instead you should learn about prepared statements and use either PDO or MySQLi. If you can't decide, try this article. If you care to learn, here is good PDO tutorial. Commented Aug 27, 2012 at 11:31
  • @raina77ow print_r($row) doesn't return anything, echo $row only returns "Array" Commented Aug 27, 2012 at 11:40
  • @Matthias Weird. Ok, let's use var_dump($row) instead (right before echo $row['infTitel'] line). Commented Aug 27, 2012 at 11:42
  • @raina77ow same as print_r - doesn't return anything. however when I query a different table I get a result of what's in the array. Commented Aug 27, 2012 at 11:52

3 Answers 3

3

You need to use :

$row = mysql_fetch_assoc($result); 

to get associative array as result.

mysql_fetch_array($result) also returns both numeric indexes and associative indexes, but I am not sure since which version.

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

Comments

2

After $result = mysql_query($sqlCommand);

$data = array();
while ($row = mysql_fetch_array($result)) {
    $data[] = $row;
}
print_r($data);

2 Comments

This code is a bit odd. You would only print the very last result of the query.
Oh, sorry, forget to add [] to store $row in array. Thank You.
2

Fetch Row gets one row at a time, you should pop it into a while loop like this:

while($row = mysql_fetch_array($result))
{
    echo $row['infTitel'];
    echo $row['col2'];
    echo $row['col3'];
}

If you are echoing the contents of the row that you selected with a select * and you are getting no value, it likely means that the column is empty for that row.

If you are using fetch_array it should return both column names and indexes. Try this to see if you get your data:

while($row = mysql_fetch_array($result))
{
    for($i=0; $i<count($row);, $i++)
    {
        echo $row[$i]."<br>";
    }
}

If this is showing you the data you expect (empty or null rows should return empty from memory) then it is possible a case sensitivity issue with your $row['Colname'] syntax?

7 Comments

I simply need to return one row for a specific ID
Using mysql_fetch_assoc() would be better, considering OP is looking to use an assoc array. mysql_fetch_array returns both rows and assoc.
thank you very much for your quick response! this works fine for returning $row['ID'] - however I don't get any values for $row['infTitel']
@Matthias Are you utterly certain you have the column names correct in the $row['columnName'] part of your code?
@Fluffeh - yes, i've double checked using the terminal.
|

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.