0

Finally jumping into some PHP for the first time and I've written this program and i'm stuck. I've searched all over the place for about 2 hours to find a solution.

Basically I'm connecting to my local database and trying to grab all the rows from my 'songs' table, and display them by their names. Instead of getting their names, i'm getting a notice that says "Notice: Array to string conversion in C:\xampp\htdocs\musiclibrary\index.php on line 47"

My current output looks like this:

Title   Artist  Genre
Array   Array   Array
Array   Array   Array
Array   

And then my code is...

<?php

        // Require configuration file
        require_once 'config.php';

        // Connect to the database
        $db_server = mysqli_connect($db_hostname, $db_username, $db_password);

        // Check for database connection error
        if(!$db_server)
        {
            die("Unable to connect to MySQL: " . mysql_error());
        }

        // Select a database
        // The mysqli_select_db() function is used to change the default database for the connection.
        mysqli_select_db($db_server, $db_database);

$prompt = array('Story title', 'Time', 'Person');
$prompt = array('Story title', 'Time', 'Person');
// Page title
echo "<h1>My Music Collection</h1>";

// Get music collection
$query = "SELECT * FROM songs";
$result = mysqli_query($db_server, $query);
$rows = mysqli_num_rows($result);

// If rows exist
if($rows > 0)
{
    // Create HTML table
    echo "<table>";
    echo "<tr><th>Title</th><th>Artist</th><th>Genre</th></tr>";

    // Loop through each row in the database table
    for($j = 0; $j < $rows; $j++)
    {
        // Build HTML table row

//PROBLEM LIES HERE ON THESE MYSQL_FETCH_ASSOC PARTS

        echo "<tr>";
        echo "<td>" .  mysqli_fetch_assoc($result,$j,'title') . "</td>";
        echo "<td>" .  mysqli_fetch_assoc($result,$j,'artist') . "</td>";
        echo "<td>" .  mysqli_fetch_assoc($result,$j,'genre') . "</td>";
        echo "</tr>";   
    }       

    echo "</table>";
}

// If there are no songs in the database table
else
{
    echo "There are currently no songs on file.";
}

    ?>

Any solutions to output the names of the rows in my database? Thanks!

3
  • 1
    mysqli_fetch_assoc itself returns an array, which contains the data from the query. You'll want to extract it. Commented Jan 23, 2018 at 3:55
  • Where can I learn to extract it? Commented Jan 23, 2018 at 3:56
  • 1
    mysqli_fetch_assoc() returns the result as an associative array. The way you print is incorrect. You should first assign mysqli_fetch_assoc() to a variable and print the values using array keys. Commented Jan 23, 2018 at 4:16

3 Answers 3

1

Use the code below to replace your code

  1. extract the values to an array first
  2. show the values in table row

    $values = mysqli_fetch_assoc($result);

    echo "<tr>";

    echo "<td>" . $values ['title']. "</td>";

    echo "<td>" . $values ['artist'] . "</td>";

    echo "<td>" . $values ['genre')]. "</td>";

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

Comments

1

You need to loop the mysqli_fetch_assoc function to count row then loop the row result to get value

Here is the code :

if($rows > 0){

echo "<table>";
echo "<tr><th>Title</th><th>Artist</th><th>Genre</th></tr>";

// Loop through each row in the database table
while($row = $result->mysqli_fetch_assoc()){

    echo "<tr>";
    foreach($row as $key => $value){
        echo "<td>" . $row['title'] . "</td>";
        echo "<td>" . $row['artist'] . "</td>";
        echo "<td>" . $row['genre'] . "</td>";
    }        
    echo "</tr>";   
}       

echo "</table>";
}

Hope it helps!

Comments

0

mysqli_fetch_assoc() function accepts only one parameter.

array mysqli_fetch_assoc ( mysqli_result $result )

Correct way to do this would be:

$query = "SELECT * FROM songs";

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

    while ($row = mysqli_fetch_assoc($result)) {
        echo "<tr>";
        echo "<td>" .  $row['title'] . "</td>";
        echo "<td>" .  $row['artist'] . "</td>";
        echo "<td>" .  $row['genre'] . "</td>";
        echo "</tr>";   
    }

    mysqli_free_result($result);
}

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.