0
#GET from Mysql
$select = "SELECT content from content WHERE page = 'index';";
$result = mysql_query($select);
$row = mysql_fetch_row($result);

#Show in diferent places in my page
<?PHP echo $row[0]; ?>
<?PHP echo $row[1]; ?>

I'm using this code above, the problem is: It's showing only the first result. What did I wrong?

  • 2
    RTFM: http://php.net/mysql_fetch_row the function fetches a **SINGLE** row of data. – Marc B Aug 13 '15 at 18:28
  • 1
    If you can, you should [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) [statements](http://php.net/manual/en/pdo.prepared-statements.php) instead, and consider using PDO, [it's really not hard](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Aug 13 '15 at 18:29
  • It's for a client. I really have to do this way or I will have to do using prepared statements in all his pages – Pampulha Tech Aug 13 '15 at 18:32

2 Answers2

0
$select = "SELECT content from content WHERE page = 'index';";
$result = mysql_query($select);

//$row = mysql_fetch_row($result); 
// mysql_fetch_row() fetches ONLY ONE ROW

// If you want to fetch them all, yes, you must use a loop.
while ($row = mysql_fetch_row($result)) {
    $rows[] = $row;
}
// So do this, get all the rows, and then you can use them in different places in your page

// Show in diferent places in your page
<?PHP echo $rows[0][0]; ?>
<?PHP echo $rows[1][0]; ?>
Don't Panic
  • 41,125
  • 10
  • 61
  • 80
-2

It's better to do this in a for loop - that way it will get all the results each time, rather than hardcoding for a scenario with just two results, etc.

<?php
$select = "SELECT content from content WHERE page = 'index';";
$result = mysql_query($select);
$rows = mysql_fetch_assoc($select);

foreach ($rows as $row) {
    echo $row['content']; // associative array with strings for keys
}
?>

Also, you should be using mysqli for security reasons.

Jeff
  • 789
  • 4
  • 13