0

I am fetching some set of ids from database and want to print them comma separated using php implode() method.

But it is giving me a warning that I passed invalid arguments to implode. So why do I get this warning and how can I fix my code, to get the id's comma separated?

<?php
session_start();
if(isset($_SESSION['tstid']))
{
    $tstid = $_SESSION['tstid'];
}
include('references.php');
include('config.php');
$query = "select * from testquestions where testid='".$tstid."'";
$res = mysql_query($query);

if(mysql_num_rows($res) > 0)
{

    while($fetch = mysql_fetch_array($res))
    {


            $z =implode(', ', $fetch['qid']);



    }
    echo $z;



}
?>
Rizier123
  • 57,440
  • 16
  • 89
  • 140
Amit Kaushal
  • 411
  • 1
  • 8
  • 23
  • Are you sure that `$fetch['qid']` is an array ? – Ulti Apr 24 '15 at 11:51
  • Please, [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 statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Apr 24 '15 at 12:26

4 Answers4

1
<?php
session_start();
if (isset($_SESSION['tstid'])) {
    $tstid = $_SESSION['tstid'];
}
include('references.php');
include('config.php');
$query = "select * from testquestions where testid='".$tstid."'";
$res = mysql_query($query);
if (mysql_num_rows($res) > 0) {
    $z = array();
    while($fetch = mysql_fetch_array($res)) {
        $z[] = $fetch['qid'];
    }
    echo implode(', ', $z);
}
?>

Explanation: You are using implode() at improper place.

You should first get all the ids in an array and then use implode()

Above is corrected code.

NOTE: Please do not use mysql_ functions as they are deprecated.

Pupil
  • 23,528
  • 5
  • 42
  • 64
0

You are trying to implode a single string, which is obviously not going to work. So you have to first create an array to be able to implode it, e.g.

while($fetch = mysql_fetch_array($res)) {
    $z[] = $fetch['qid'];
    //^^ See here
}

echo implode(",", $z);

Side notes:

mysql_* API is deprecated and will be removed in the future. Use mysqli with prepared statements, or PDO with prepared statements, they're much safer.

Rizier123
  • 57,440
  • 16
  • 89
  • 140
  • @user3567832 You're welcome! (FYI: You can accept the answer how helped you the most and solved your problem (http://meta.stackexchange.com/q/5234)!) – Rizier123 Apr 24 '15 at 12:02
0

The error is showing because you passed a single value to implode. But implode uses an array to convert it in a string. So first create an array of Ids then implode it by comma(or whatever separate you want to use).

Better to use this

if(mysql_num_rows($res) > 0)
{
    $idArr = array();
    while($fetch = mysql_fetch_array($res))
    {
       $idArr[] = $fetch['qid'];    
    }
    $z =implode(', ', $idArr);
    echo $z;
}
Sunil Pachlangia
  • 1,963
  • 2
  • 13
  • 25
0
<?php
session_start();
if(isset($_SESSION['tstid']))
{
    $tstid = $_SESSION['tstid'];
}
include('references.php');
include('config.php');
$query = "select * from testquestions where testid='".$tstid."'";
$res = mysql_query($query);

if(mysql_num_rows($res) > 0)
{
    $z=array();
    while($fetch = mysql_fetch_array($res))
    {


            $z[] =$fetch['qid'];



    }
   echo implode(",", $z);



}
?>
Vikas Umrao
  • 2,790
  • 1
  • 13
  • 23