?
Solved

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

Posted on 2015-01-23
4
Medium Priority
?
133 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 111

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 111

Accepted Solution

by:
Ray Paseur earned 2000 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

777 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