• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 151
  • Last Modified:

Need assistance with PDO error: mysql_fetch_array() expects parameter 1 to be resource, object given

I could use some assistance with the following error:    Warning: mysql_fetch_array() expects parameter 1 to be resource, object given.   ($last = end($row36);)   please advise how to adjust for PDO.  

$fh = fopen('/thefind.txt', 'w');
$result36 = $conn->query('SELECT * FROM Feeds');
while ($row36 = mysql_fetch_array($result36)) {
    $last = end($row36);    // Problem here
    foreach ($row36 as $item) {
        fwrite($fh, $item);
        if ($item != $last)
            fwrite($fh, "\t");
    }
    fwrite($fh, "\n");
	
}
fclose($fh);

Open in new window

0
lawrence_dev
Asked:
lawrence_dev
  • 2
  • 2
1 Solution
 
Ray PaseurCommented:
Try it like this.  It's not practical to try to use the obsolete MySQL extension functions any more.
$fh = fopen('/thefind.txt', 'w');
$result36 = $conn->query('SELECT * FROM Feeds');
if (!$result36) trigger_error('FAILURE IN QUERY', E_USER_ERROR);
while ($row36 = $result36->fetch_assoc()) {
    $last = end($row36);    // Problem here
    foreach ($row36 as $item) {
        fwrite($fh, $item);
        if ($item != $last)
            fwrite($fh, "\t");
    }
    fwrite($fh, "\n");
	
}
fclose($fh);

Open in new window

0
 
lawrence_devAuthor Commented:
OK I have an error here:

while ($row36 = $result36->fetch_assoc()) {

Fatal error: Call to undefined method PDOStatement::fetch_assoc()

Just as a note:  I attempted to change to PDO hence the "$result36 = $conn->query('SELECT * FROM Feeds');"
0
 
Ray PaseurCommented:
See if this gets closer.  Sorry, but I don't have any way to test it

// CREATE AND PREPARE A QUERY 
$sql = 'SELECT * FROM Feeds';
$pdos = $pdo->prepare($sql);

// TRY THE QUERY
try
{
    $pdos->execute();
}
catch(PDOException $exc)
{
    var_dump($exc);
    trigger_error($exc->getMessage(), E_USER_ERROR);
}

// DETERMINE HOW MANY ROWS OF RESULTS WE GOT
$num     = $pdos->rowCount();
$num_fmt = number_format($num);
if (!$num)
{
    echo "<br/>QUERY: $sql ";
    echo "<br/>FOUND NO DATA ";
    echo PHP_EOL;
}
else
{
    echo "<br/>QUERY: $sql ";
    echo "<br/>FOUND $num_fmt ROWS OF DATA ";
    echo PHP_EOL;
}

// ITERATE OVER THE RESULTS SET TO SHOW WHAT WE FOUND
while ($row = $pdos->fetch(PDO::FETCH_ASSOC))
{
    print_r($row);
    echo PHP_EOL;
}

Open in new window

0
 
lawrence_devAuthor Commented:
QUERY: SELECT * FROM Feeds
FOUND 15,905 ROWS OF DATA


I added the foreach  below and got it to work.


while ($row36 = $pdos->fetch(PDO::FETCH_ASSOC))
{
   
      
      foreach ($row36 as $item) {
        fwrite($fh, $item);
        if ($item != $last)
            fwrite($fh, "\t");
    }
    fwrite($fh, "\n");

Thanks!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now