0

I am trying to produce an array of values to be used by a variable, but I can't get it to work. I don't want to "see" the results. Instead, I want the variable to "use" the results. Here's my code:

$rss_results = mysql_query("SELECT feed_id, feed_url, feed_title, feed_order, feed_page_id FROM user_feeds  WHERE ((feed_owner = '" . $_SESSION['UserId'] . "'));");

    if ($rss_results) {

    while($row = mysql_fetch_array($rss_results))
    {
    $feed->set_feed_url(print "'".$row[feed_url]."',");

    }

    }
    else {
      // something went wrong.
      echo mysql_error();

The problem is with the line:

$feed->set_feed_url(print "'".$row[feed_url]."',");

EDIT 1) Yes. I know print doesn't belong there. I forgot to remove it after doing some troubleshooting earlier.

2) I am trying to use this code with an RSS parser calleld SimplePie.

3) Everyone seems to agree that the following code works

   $feed->set_feed_url(array("'" . $row['feed_url'] . "',"));

...but it doesn't. If I weren't accessing my db, the code would look something like this....

$feed->set_feed_url(array('http://www.tmz.com/rss.xml', 'feed://rss.cbc.ca/lineup/topstories.xml'));

...which works just fine. I am trying to reproduce that effect using values stored in a mysql db. If the code worked, it would be working. So there must be a problem with the code that is "supposed" to be working.

Richard
  • 209
  • 2
  • 10
  • 4
    It's not helpful to your question, but it should be noted that you should stop using `mysql_*` functions. They're being deprecated. Instead use [PDO](http://php.net/manual/en/book.pdo.php) (supported as of PHP 5.1) or [mysqli](http://php.net/manual/en/book.mysqli.php) (supported as of PHP 4.1). If you're not sure which one to use, [read this SO article](http://stackoverflow.com/questions/13569/mysqli-or-pdo-what-are-the-pros-and-cons). – Matt Aug 07 '12 at 15:43
  • Why are you setting a method parameter as 'print'? This makes no sense, you pass variables to methods.... not printed characters... – David Barker Aug 07 '12 at 15:45
  • @DavidBarker - Actually, `print()` always returns 1, so you're not passing printed characters, you're always passing 1. – nickb Aug 07 '12 at 15:48
  • I'm pretty sure no one would agree that `$feed->set_feed_url(array("'" . $row['feed_url'] . "',"));` works, because that's not how dynamically populating arrays works. – Matt Aug 07 '12 at 16:28

1 Answers1

1

When accessing an associative array, you must put the index name inside quotes:

Instead of

$row[feed_url]

use

$row['feed_url']

Also,

$feed->set_feed_url(print "'".$row[feed_url]."',");

makes no sense. If you want to pass a value to a method, pass the variable, don't print it to the screen.

$feed->set_feed_url("'" . $row['feed_url'] . "',");

If you don't want the string you pass to $feed->set_feed_url() to be enclosed in 's, just pass it like a regular variable:

$feed->set_feed_url($row['feed_url']);

UPDATE:

Since you mention you're trying to pass an array, you have to first initialize it, then populate it, then send it to the method:

$urls = array();
while ($row = mysql_fetch_array($rss_results)) {
    $urls[] = $row['feed_url'];
}
$feed->set_feed_url($urls);

Doing this:

$feed->set_feed_url(array("'" . $row['feed_url'] . "',"));

Will simply pass the following to $feed->set_feed_url():

array(
    [0] => String() "'[value of $row['feed_url'] ]',"
)

If you want to call $feed->set_feed_url() once for each iteration of the loop, this will work fine:

while($row = mysql_fetch_array($rss_results)) {
    $feed->set_feed_url(array($row['feed_url']));
}

Please note: You should stop using mysql_* functions. They're being deprecated. Instead use PDO (supported as of PHP 5.1) or mysqli (supported as of PHP 4.1). If you're not sure which one to use, read this SO article.

Community
  • 1
  • 1
Matt
  • 6,993
  • 4
  • 29
  • 50
  • Exactly what I was thinking :) – David Barker Aug 07 '12 at 15:47
  • I forgot to remove print from when I was testing something earlier. Also I tried the code you suggested, and it doesn't seem to be working. – Richard Aug 07 '12 at 15:56
  • @Richard What does `var_dump($row['feed_url']); exit();` inside the loop produce? – Matt Aug 07 '12 at 15:57
  • @Matt cURL error 6: Couldn't resolve host 'http'string(39) "http://rss.cbc.ca/lineup/topstories.xml" – Richard Aug 07 '12 at 16:01
  • @Richard I'm assuming you added that AFTER `$feed->set_feed_url()`? – Matt Aug 07 '12 at 16:03
  • Yes. I added after $feed->set_feed_url... Also http://rss.cbc.ca/lineup/topstories.xml is only one of the values I am trying to access. – Richard Aug 07 '12 at 16:03
  • @Richard Well you're passing the variable to your method correctly. Now the error lies in "why can't cURL resolve the host?" That's a new question. – Matt Aug 07 '12 at 16:05
  • @Richard OH OH OH You're passing **'rss.cbc.ca/lineup/topstories.xml'** instead of **rss.cbc.ca/lineup/topstories.xml** See the difference? – Matt Aug 07 '12 at 16:10
  • actually CBC government owned radio/tv broadcast in Canada – Richard Aug 07 '12 at 16:13
  • @Richard sorry, that was a typo. My point was that you were surrounding your URL with single-quotes. – Matt Aug 07 '12 at 16:14
  • The RSS parser I am using requires single quotes. I have updated my original post with more details. – Richard Aug 07 '12 at 16:24
  • @Richard, the quotes aren't necessary except to define a `string literal`. I'll update my answer. I didn't realize you needed an `array` to pass to the method. – Matt Aug 07 '12 at 16:26