Solved

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

Posted on 2015-01-23
4
117 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 109

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 109

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
paypal ipn to mysql 3 36
hasing a url 16 27
Display images from mysql blob type (Not working) 9 28
What does this formatting equate to? 7 10
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

770 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