1

What I want is to convert an array to a string so I can display it on my websites. I made a query to select the values id, title, date, auteur. When i use my function I made for this, I get the following error:

Notice: Array to string conversion in /Applications/MAMP/htdocs/beheer/index.php on line 444 Array.

The function is as following:

<?php
include ('connect/connect.php');



function getListContentMain() {
    global $con;
    $dbname = 'content';
    mysqli_select_db($con,$dbname);
    $q = "SELECT * FROM main LIMIT 5";
    $result = mysqli_query($con,$q);

    $ListContentMainData = array();
    while($row = mysqli_fetch_array($result)) {
        echo $row['id'];
        echo $row['title'];
        echo $row['date'];
        echo $row['auteur'];
    }

    $ListContentMainData[] = $row;

    return $ListContentMainData;
    }

    ?>
2
  • 2
    What line is 444? Also, $ListContentMainData[] = $row; needs to go inside of your loop. Commented Apr 8, 2014 at 15:30
  • I made a query to select the values id, title, date, auteur... No you didn't. Your query selects every column *, you then display your selection using echo and return the last $row (again, including all columns) wrapped in another array. Commented Apr 8, 2014 at 15:35

2 Answers 2

1

implode it up

return implode(' ',$ListContentMainData);

You are returning an array but you are trying to print it as a normal string , so in that case you need to implode it (array) and convert it to a string.

Rewrite like..

  $ListContentMainData = array();
    while($row = mysqli_fetch_array($result)) {
        echo $row['id'];
        echo $row['title'];
        echo $row['date'];
        echo $row['auteur'];
        $ListContentMainData[]=$row['id'];
        $ListContentMainData[]=$row['title'];
        $ListContentMainData[]=$row['date'];
        $ListContentMainData[]=$row['auteur'];
    }

    return implode(' ',$ListContentMainData);
Sign up to request clarification or add additional context in comments.

Comments

1

This will make an array of strings, one for each row in the table with columns separated by a space (you could change that in the implode statement).

function getListContentMain() {
    global $con;
    $dbname = 'content';
    mysqli_select_db($con,$dbname);
    $q = "SELECT * FROM main LIMIT 5";
    $result = mysqli_query($con,$q);

    $ListContentMainData = array();
    while($row = mysqli_fetch_array($result)) {
        echo $row['id'];
        echo $row['title'];
        echo $row['date'];
        echo $row['auteur'];
        array_push($ListContentMainData, implode(' ', $row));
    }
    return $ListContentMainData;
}

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.