Solved

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

Posted on 2015-01-23
4
111 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
  • 2
  • 2
4 Comments
 
LVL 108

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 108

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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…

706 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now