Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,093
    Thanks
    297
    Thanked 12 Times in 12 Posts

    Exclamation mysql_num_rows supplied argument is not a valid MySQL result resource?

    Im Getting the above message on my page dispite it actually printing out the correct data as well?

    here is my code but im not sure why im getting the warning? any ideas

    PHP Code:
    <?php
    $getNews 
    "SELECT event_headline FROM events WHERE YEAR(dateSubmitted) = YEAR(CURDATE()) ORDER BY dateSubmitted ASC";
    $sql mysql_query($getNews);?>
    <div class="currentNews_Container">
    <div class="currentNews_Header"><?php print "Events "date('Y');?></div>
    <?php
    if(mysql_num_rows($sql) <> 0)
    {
        while(
    $eventStories mysql_fetch_array($sql))
        {
    ?>
            <div class="NewsItem_Container">
            <?php print $eventStories['news_headline'];?>
            </div><?php
        
    }
    }
        else 
        {
            print 
    "No Current Events";
        }
        
    ?>
    </div>
    thanks
    Luke

  • #2
    Regular Coder
    Join Date
    Mar 2006
    Posts
    238
    Thanks
    3
    Thanked 37 Times in 37 Posts
    Try to change the line
    PHP Code:
    $sql mysql_query($getNews); 
    to
    PHP Code:
    $sql mysql_query($getNews) or die(mysql_error()); 
    Some error would be output which should hint what is wrong ...

    Edit: Of course or die(mysql_error()) should be commented or removed in the Production environment when debugging is over.

  • Users who have thanked SKDevelopment for this post:

    LJackson (10-13-2009)

  • #3
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,093
    Thanks
    297
    Thanked 12 Times in 12 Posts
    thanks mate managed to solve it from the error returned

    really need to start getting into the habbit of adding
    "or die(mysql_error())" saves alot of time

    cheers

  • #4
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,093
    Thanks
    297
    Thanked 12 Times in 12 Posts
    ok again probably something simple,

    but any ideas as to why this wont work
    PHP Code:
    <?php
    $getnews 
    "SELECT * FROM $type WHERE headline = $headline";
    $sql mysql_query($getnews) or die(mysql_error());
    while(
    $row mysql_fetch_array($sql)){
    print 
    $row['content'];
    }
    ?>
    its printing out
    Code:
    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'a test news story' at line 1
    ive tried adding single quotes around the varibles but it still wont work?
    any ideas please

    thanks
    Luke

  • #5
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,093
    Thanks
    297
    Thanked 12 Times in 12 Posts
    ok i finally solved it by using this
    PHP Code:
    $getnews "SELECT * FROM ".$type." WHERE headline = '".$headline."'"
    cheers

  • #6
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,093
    Thanks
    297
    Thanked 12 Times in 12 Posts
    one problem with my above solution is that if a headline has an ' in it it breaks the sql

    and its returning an error:
    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use.

    here is my code of the page before the one with the error with the data being sent via the url:
    PHP Code:
    while($newsStories = mysql_fetch_array($sql))
    {?>
        <div class="NewsItem_Container">
        <a href="information.php?type=news&amp;headline=<?php echo urlencode($newsStories['headline'])?>">
        <?php print $newsStories['headline'];?>
        </a>
        </div><?php
    }
    and my other page where the error is appearing is:
    PHP Code:
    <?php

    if(isset($_GET['type'])){
    $type $_GET['type'];
    $type strtolower($type);
    }

    if(isset(
    $_GET['headline'])){
    $headline urldecode($_GET['headline']);
    $headline htmlspecialchars($headline);
    }
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Kyushindo to Mayo Shin Do Karate Judo Martial Arts in UK</title>
    <link rel="stylesheet" type="text/css" href="page_styling.css" />
    <?php require("functions.php");
    include_once(
    "db_info.php");?>
    </head>

    <body>
    <?php header_info(); ?>

    <div id="wrapper">

    <div id="left">
    <?php include("menu.php");?>
    </div>

    <div id="right">
    <div class="pageinformation">
    <div class="pageinformationHeader"><?php print $headline ?></div>
    <div class="infocontainer">
    <div class="pageinfomationIMG"><img src="testkarate.jpg" /></div>

    <?php
    $getnews 
    "SELECT * FROM ".$type." WHERE headline = '".$headline."'";
    $sql mysql_query($getnews) or die(mysql_error());
    while(
    $row mysql_fetch_array($sql)){
    print 
    htmlspecialchars($row['content']);
    }
    ?>
    </div>
    </div>
    </div>

    <?php footer();?>
    </div>
    </body>
    </html>
    any ideas how to solve this please
    thanks
    Luke

  • #7
    Regular Coder
    Join Date
    Mar 2006
    Posts
    238
    Thanks
    3
    Thanked 37 Times in 37 Posts
    This is because you need to escape $headline before using it in the query. I would also escape $type just in case. Please see mysql_real_escape_string().
    PHP Code:
    $headline mysql_real_escape_string($headline);
    $type mysql_real_escape_string($type); 

  • #8
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,093
    Thanks
    297
    Thanked 12 Times in 12 Posts
    ok that has got rid of the error but now its not displaying the value of $headline?

    here is the updated code
    Code:
    <?php
    require("functions.php");
    include_once("db_info.php");
    
    if(isset($_GET['type'])){
    $type = $_GET['type'];
    $type = strtolower($type);
    }
    
    if(isset($_GET['headline'])){
    $headline = urldecode($_GET['headline']);
    $headline = htmlspecialchars($headline);
    }
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Kyushindo to Mayo Shin Do Karate Judo Martial Arts in UK</title>
    <link rel="stylesheet" type="text/css" href="page_styling.css" />
    </head>
    
    <body>
    <?php header_info(); ?>
    
    <div id="wrapper">
    
    <div id="left">
    <?php include("menu.php");?>
    </div>
    
    <div id="right">
    <div class="pageinformation">
    <div class="pageinformationHeader"><?php print $headline ?></div>
    <div class="infocontainer">
    <div class="pageinfomationIMG"><img src="testkarate.jpg" /></div>
    
    <?php
    $getnews = "SELECT * FROM ".mysql_real_escape_string($type)." WHERE headline = '".mysql_real_escape_string($headline)."'";
    $sql = mysql_query($getnews) or die(mysql_error());
    while($row = mysql_fetch_array($sql)){
    print htmlspecialchars($row['content']);
    }?>
    </div>
    </div>
    </div>
    
    <?php footer();?>
    </div>
    </body>
    </html>
    any ideas
    Luke

    EDIT
    ====

    Ok got it working by removing
    PHP Code:
    $headline htmlspecialchars($headline); 
    thanks mate
    Luke


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •