1

Recently I rebuilt my website, and I was using this simple code to grab and fetch data from mysql db as rss:

<?php



$connection = mysql_connect("localhost", "site_user", "1212121") or die (mysql_error()); // 'database_name' 
$db         = mysql_select_db("site_db", $connection) or die (mysql_error());

$rss_query = @mysql_query("SELECT * FROM jokes where valid = '1' order by id asc LIMIT 0,20");

echo $rss = "<?xml version='1.0' encoding='utf-8' ?>

<rss version='2.0'>

<channel>

<title>site</title>

<link>www.site.com</link>
<description>feed</description>
 ";

while ($q_rss = mysql_fetch_array($rss_query)) {

$id[]    =  $q_rss['id'];
$title[] =  $q_rss['joketitle'];
$des[]   =  $q_rss['preview'];
}    

$count = count($id); 
for ( $i = 0; $i <= $count-1; $i ++) {   
echo $r = " 
<item>
 <title>".$title[$i]."</title>

<link>www.site.com/view.php?byt=".$id[$i]."</link>

<description>...".substr($des[$i],0,70)."</description>

</item>
";
}
echo "
</channel>

</rss>";




?>

regarding the link: www.site.com/view.php?byt=".$id[$i]." it was changed to www.site.com/byt/[id] which id= numbers

the error that appear is :

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /home/site/public_html/rss.php on line 25

Notice: Undefined variable: id in /home/site/public_html/rss.php on line 32

my php and mysql version are up to date ..

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
FAISAL
  • 23
  • 5
  • This error normally means that the query has failed and returned `false` in the `mysql_query(...)` but in your case it could means that the connection also failed. Instead of hiding errors i.e. adding `@` check for them and react accordingly – RiggsFolly Feb 07 '16 at 11:40
  • Please dont use the `mysql_` database extension, it is deprecated (gone for ever in PHP7) Especially if you are just learning PHP, spend your energies learning the `PDO` or `mysqli_` database extensions, [and here is some help to decide which to use](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) as you are doing a rewrite, now is the perfect time to make this changes as well – RiggsFolly Feb 07 '16 at 11:41
  • Add error reporting to the top of your file(s) _while testing_ right after your opening PHP tag for example ` – RiggsFolly Feb 07 '16 at 11:41

1 Answers1

0

Is there a particular reason you would want to add the items to arrays and then iterate through these arrays later rather than adding the rss content directly in the recordset iteration?

Also, the mysql_* suite of functions are deprecated so you would be advised to migrate over to mysqli - though here there is no real risk of sql injection as you are not taking user input as a parameter to the sql.

That said, there ought to be some sort of logical test to see if the query has produced a recordset.

<?php
    $connection = mysql_connect("localhost", "site_user", "1212121") or die (mysql_error());
    $db         = mysql_select_db("site_db", $connection) or die (mysql_error());
    $rss_query  = mysql_query("SELECT * FROM `jokes` where `valid` = '1' order by `id` asc LIMIT 0,20");

    echo $rss = "<?xml version='1.0' encoding='utf-8' ?>
    <rss version='2.0'>
        <channel>
            <title>site</title>
            <link>www.site.com</link>
            <description>feed</description>";

    if( $rss_query && mysql_num_rows( $rss_query ) > 0 ) {
        while( $q_rss = mysql_fetch_array($rss_query) ) {
            echo "
            <item>
                <title>{$q_rss['id']}</title>
                <link>{$q_rss['joketitle']}</link>
                <description>...{$q_rss['preview']}</description>
            </item>";
        }
    } else {
        echo "
            <item>
                <title>Error</title>
                <link></link>
                <description>The sql query has failed for some reason</description>
            </item>";   
    }

    echo "
        </channel>
    </rss>";

?>
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46