0

Im not sure whats going wrong with this code. I have a paragraph stored in the database under content, but for some reason I can't get it to display. Any help would be greatly appreciated, I'm a newer developer and welcome any feedback.

<?php
require_once('db_connection.php'); //connection credentials

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

$sql = "SELECT content FROM content";
$result = mysqli_query($conn, $sql);

while($row = mysqli_fetch_row($result)) { 
    echo $row;
}

mysqli_close($conn);
?>
Rjoel
  • 66
  • 1
  • 10

1 Answers1

1

echo $row; you're missing the column's object's array itself that you want to echo.

What you want is echo $row[0];

However, both column and table name are the same, so make sure that that is indeed correct.

As per the manual:

Example pulled from the manual:

$query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 50,5";

if ($result = mysqli_query($link, $query)) {

    /* fetch associative array */
    while ($row = mysqli_fetch_row($result)) {
        printf ("%s (%s)\n", $row[0], $row[1]);
    }

    /* free result set */
    mysqli_free_result($result);
}

If the above failed, then that could mean that your query may have failed and you need to check for errors on the query, using mysqli_error($conn).

Reference:

Same thing goes for the connection.

Reference:

Example from the manual:

<?php
$link = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");

if (!$link) {
    echo "Error: Unable to connect to MySQL." . PHP_EOL;
    echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
    echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
    exit;
}

echo "Success: A proper connection to MySQL was made! The my_db database is great." . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;

mysqli_close($link);
?>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • Wonderful, thank you for the help. Now if I need to use that block of code several time it would be best for me to put it in a function right? But if I do that how do you control which row gets displayed? – Rjoel Aug 14 '17 at 00:01
  • @Rjoel98 You're welcome. If you need to display something several times, that would depend on "how" exactly and there are multiple ways to do this, too many actually. If you're going to use a function, then you'll need to make sure that you pass your db connection inside the function, otherwise you'll get an variable scope issue. Here's a Q&A on this on Stack https://stackoverflow.com/q/16959576/1415724 on variable scoping. If you're going to want to traverse through different pages, then you'll need to use sessions for this http://php.net/manual/en/book.session.php or include the file. – Funk Forty Niner Aug 14 '17 at 00:04
  • @Rjoel98 About your: *"But if I do that how do you control which row gets displayed?"* - Again, many ways to do this. The `[0]` and `[1]` object arrays control which column in their sequence you chose in the query, as shown in the example I pulled from the manual. You can also use a few other functions such as outlined in http://php.net/manual/en/class.mysqli-result.php - `mysqli_fetch_assoc()` for instance uses an associative array `$row["Name"], $row["CountryCode"]` so you can use the names if that makes tracking as names for the given columns easier. See the other functions on that page. – Funk Forty Niner Aug 14 '17 at 00:17