Solved

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

Posted on 2015-01-23
4
125 Views
Last Modified: 2015-01-23
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
Comment
Question by:lawrence_dev
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 40567481
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
 

Author Comment

by:lawrence_dev
ID: 40567507
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
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 40567541
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
 

Author Comment

by:lawrence_dev
ID: 40567743
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question