Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2017
    Posts
    845
    Rep Power
    0

    Question Error: Undefined Index


    Php Folks,

    This is tiring!
    I have my db details like this:
    db: crawler_index
    tbl: crawler_index

    Tbl Columns: id, time_and_date,url,title,description,internal_url.

    The following script extracts links and indexes to the mysql tbl.
    Problem is, I keep getting this nagging error:
    Notice: Undefined index: description in C:\xampp\htdocs\cURL\crawler_test.php on line

    Error referencing this line:
    PHP Code:
    $description $tags['description']; 
    Context:
    PHP Code:
     // Gets Webpage Description
     
    $b =$main_url;
     @
    $url parse_url$b );
     @
    $tags get_meta_tags($url['scheme'].'://'.$url['host'] );
     
    $description $tags['description']; 
    Full Code:
    PHP Code:
    <?php

    /* 
    // Database Structure 
    CREATE TABLE 'webpage_details' (
     'link' text NOT NULL,
     'title' text NOT NULL,
     'description' text NOT NULL,
     'internal_link' text NOT NULL,
    ) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin1

    */

    //Connect to Mysql Database in this input format: "Server", "MySql User", "Mysql User Password", "MySql Database Name".
    $conn mysqli_connect("localhost""root""""crawler_index");

     
    $main_url="http://php.net/";
     
    $str file_get_contents($main_url);
     
     
    // Gets Webpage Title
     
    if(strlen($str)>0)
     {
      
    $str trim(preg_replace('/\s+/'' '$str)); // supports line breaks inside <title>
      
    preg_match("/\<title\>(.*)\<\/title\>/i",$str,$title); // ignore case
      
    $title=$title[1];
     }
        
     
    // Gets Webpage Description
     
    $b =$main_url;
     @
    $url parse_url$b );
     @
    $tags get_meta_tags($url['scheme'].'://'.$url['host'] );
     
    $description $tags['description'];
        
     
    // Gets Webpage Internal Links
     
    $doc = new DOMDocument
     @
    $doc->loadHTML($str); 
     
     
    $items $doc->getElementsByTagName('a'); 
     foreach(
    $items as $value
     { 
      
    $attrs $value->attributes
      
    $sec_url[]=$attrs->getNamedItem('href')->nodeValue;
     }
     
    $all_links=implode(",",$sec_url);
     
     
    /*
     if (!$conn) 
    {
        //Error Message to show user in technical/development mode to see errors.
        die("Database Error : " . mysqli_error($conn));
        
        //Error Message to show User in Layman's mode to see errors.
        die("Database error.");
        exit();
    }
    */
    //mysqli_query($conn,"insert into crawler_index values($main_url,$title,$description,$all_links)");

    //Insert the user's inputs into Mysql database using php's sql injection prevention method "Prepared Statements".
                
    $stmt mysqli_prepare($conn,"INSERT INTO crawler_index(url,title,description,internal_url) VALUES (?,?,?,?)");
                
    mysqli_stmt_bind_param($stmt,'ssss',$main_url,$title,$description,$all_links);
                
    mysqli_stmt_execute($stmt);
                
                
    //Check if user's registration data was successfully submitted or not.
                
    if (!$stmt)
                {
                    echo 
    "Sorry! Our system is currently experiencing a problem indexing your website.";
                    exit();
                }
                
    echo 
    "Url: $main_url<br>";
    echo 
    "Title: $title<br>";
    echo 
    "Description: $description<br>";
    echo 
    "Links: $all_links<br>";

    ?>
    The code I found here:
    Create Simple Web Crawler Using PHP And MySQL
    Then I updated it by adding PREP STMT.
    My update should work. Why am I getting this totally & utterly nuisansical error about undefined index "description" when I do have a column under that name ?
    Testing the script before attempting to learn from it's tutorial site as I do not want to learn buggy or outdated code.
    Attached Images
    Last edited by UniqueIdeaMan; May 21st, 2018 at 06:57 PM.
  2. #2
  3. Lord of the Dance
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Oct 2003
    Posts
    4,184
    Rep Power
    2012
    You really need to learn how to debug code.

    First question to ask: Where does $tags gets its value from?
    And no, it is not from the database (why would you assume/expect that?)
  4. #3
  5. Wiser? Not exactly.
    Devshed God 2nd Plane (6000 - 6499 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    6,274
    Rep Power
    4193
    Originally Posted by UniqueIdeaMan
    Why am I getting this totally & utterly nuisansical error about undefined index "description" when I do have a column under that name ?
    What exactly does your database having a description column due with get_meta_tags returning an array without a description key?

    What might cause get_meta_tags to do such a thing? Use that idea generating brain of yours to generate an idea as to why it might be.
    Recycle your old CD's



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud
  6. #4
  7. Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2006
    Posts
    2,682
    Rep Power
    1841
    Pretty much been beaten to it, but what do you see if you var_dump that array? (Assuming var_dump is the right function to use to print out the content of an array).
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2017
    Posts
    845
    Rep Power
    0
    Originally Posted by MrFujin
    You really need to learn how to debug code.

    First question to ask: Where does $tags gets its value from?
    And no, it is not from the database (why would you assume/expect that?)
    You mean: You really need to remember what each error code means.
    I have had this undefined index error before but can't quite remember where and when. Assumed that, in the past, when I mistyped a column name (mysql) I got the error.
    Why did I assume like that ? Read what I reply to Kicken.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2017
    Posts
    845
    Rep Power
    0
    Originally Posted by kicken
    What exactly does your database having a description column due with get_meta_tags returning an array without a description key?

    What might cause get_meta_tags to do such a thing? Use that idea generating brain of yours to generate an idea as to why it might be.
    I saw this error:
    Notice: Undefined index: description in C:\xampp\htdocs\cURL\crawler_test.php on line
    And assumed "description" text is the problem somewhere. So, where have I got that text ? Realized, it's one of my column names. And so, assumed I mistyped as I had problems like this before when I mistyped a column name.
    When I checked and saw no typo in this case, I thought php seeing a typo when there is not. But, we all know that humans make mistakes in reading and not computers. Now don't we ?
    However, you people are right. Since the error was pointing to this line:
    PHP Code:
    $description $tags['description']; 
    Then naturally, I should be pondering about the "description meta tag" and not the "description mysql tbl column".
    If you really must know. For a brief moment, I assumed that maybe the "description meta tag" is missing in the page and so the script is spitting the error. I should have checked the src code of the page. But, I assumed the programmer had the sense to realize that not all pages would have the meta tags and so maybe his script code is built in a way to not spit errors when it fails to encounter the tag.
    But, I was wrong maybe. And if I am right about this then I guess the tutor was a bad experienced programmer.
    So, am I right about the meta description missing ?
    Wait, why am I asking you this ? Might aswell check it out myself!

    Thanks folks!
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2017
    Posts
    845
    Rep Power
    0
    UPDATE!
    php.net src code view shows only this:
    PHP Code:
    <meta charset="utf-8">
      <
    meta name="viewport" content="width=device-width, initial-scale=1.0"
    CTRL+F found no "meta description" tag.
    And so, I reckon I found my answer. The page has no meta description tag!

    Now, I must add some condition here somewhere so the eror does not occur if any meta tags are not encountered by the script.
    PHP Code:
    // Gets Webpage Description
     
    $b =$main_url;
     @
    $url parse_url$b );
     @
    $tags get_meta_tags($url['scheme'].'://'.$url['host'] );
     
    $description $tags['description']; 
    Maybe, I should add a line this ?
    PHP Code:
    if(!$description =="")
    {
    Then continue with the script flow}
    else{
    stop script}. 
    What do you say ?
    Ok, so how do I write the code if the meta keywords are not present on the page ?
    I mean, above I dealt with the tag:
    <meta description>

    Now, must deal with the tag:
    <meta keywords>
    But on the tutorial script, I do not see any code referencing to this tag. Do you see it ?
    If so, then which line is it ?

    PHP Code:
    // Gets Webpage Description
     
    $b =$main_url;
     @
    $url parse_url$b );
     @
    $tags get_meta_tags($url['scheme'].'://'.$url['host'] );
     
    $description $tags['description']; 
    I don't really understand this line:
    PHP Code:
     @$tags get_meta_tags($url['scheme'].'://'.$url['host'] ); 
    What does it mean ? It's extracting the meta tags but which one ? The <meta keywords> or the <meta description> ?
    Ok. Maybe, it's extracting both. But, I can see the meta description is supposed to get dumped to the $description variable. But, I see no variable for the meta keywords to be dumped. Hence the confusion. You people understand my confusion. Right ?
    I mean, if the $meta_keywords or $keywords variable existed then I could have added a condition like this:
    PHP Code:
    if(!$keywords =="")
    {
    Then continue with the script flow}
    else{
    stop script}. 
    Ok. I understand my samples are not totally correct as I need to add the str_len but I just did a rough 2 samples at the top pf my head. Ok ?
    Last edited by UniqueIdeaMan; May 23rd, 2018 at 12:08 PM.
  14. #8
  15. Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2006
    Posts
    2,682
    Rep Power
    1841
    I wasn'r certain, but the use of @ is what I thought it would be, and a 30 seconds effort involving a search engine and navigating a web-site and using Ctrl_f to find occurences of the @ symbol led to:
    Inline Error Suppression
    You can also tell PHP to suppress specific errors with the Error Control Operator @. You put this operator at the beginning of an expression, and any error that’s a direct result of the expression is silenced.
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  16. #9
  17. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Caro, Michigan
    Posts
    14,961
    Rep Power
    4575
    Originally Posted by UniqueIdeaMan
    @$tags = get_meta_tags($url['scheme'].'://'.$url['host'] );
    You are the last person that should be suppressing errors. Stop doing it. I'm confident you just copy and pasted this and have no idea what it means, so I'm really just talking to anyone foolish enough to be following along with this debacle.
    -- Cigars, whiskey and wild, wild women. --
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2017
    Posts
    845
    Rep Power
    0
    Originally Posted by SimonJM
    I wasn'r certain, but the use of @ is what I thought it would be, and a 30 seconds effort involving a search engine and navigating a web-site and using Ctrl_f to find occurences of the @ symbol led to:
    Thank you very much my elderly person!
    I was wondering what all these '@' were doing here:
    PHP Code:
    // Gets Webpage Description
     
    $b =$main_url;
     @
    $url parse_url$b );
     @
    $tags get_meta_tags($url['scheme'].'://'.$url['host'] );
     
    $description $tags['description']; 
    Anyway, should not the '@' before the $tags have done the job ?
    nevertheless, I added an '@' infront of the $description. And guess what ? Yep! the error is gone.
    So, one problem solved by a nice elderly person.

    Now, I got to deal with the other error:
    http://forums.devshed.com/php-develo...ml#post2985597
    Can anyone help on that one ?
    Last edited by UniqueIdeaMan; May 26th, 2018 at 01:20 PM.
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2017
    Posts
    845
    Rep Power
    0
    Originally Posted by Sepodati
    You are the last person that should be suppressing errors. Stop doing it. I'm confident you just copy and pasted this and have no idea what it means, so I'm really just talking to anyone foolish enough to be following along with this debacle.
    I already mentioned here that it's copy & paste:
    http://forums.devshed.com/php-develo...ml#post2985598

    And on post 7, I did say:
    "I don't really understand this line:
    PHP Code:
     @$tags get_meta_tags($url['scheme'].'://'.$url['host'] ); 
    What does it mean ? It's extracting the meta tags but which one ? The <meta keywords> or the <meta description> ?"


    No one is foolish in helping a newbie learn.
    You went along with me a while back. So, are you a fool too ?
    Anyway, welcome back! Now, you may continue your hints for my learning purpose. And, all newbies' learning purposes.
    As always: cHeErS!

    Anyway, I guess this line means: Grab The Meta Description from the meta tag <description>.
    I think it does not say to grab the Meta keywords from the <meta keywords> tag. Right, Sepo ?
  22. #12
  23. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Caro, Michigan
    Posts
    14,961
    Rep Power
    4575
    Anyway, I guess this line means: Grab The Meta Description from the meta tag <description>.
    I think it does not say to grab the Meta keywords from the <meta keywords> tag. Right, Sepo ?
    What part of this RTFM is confusing to you? (emphasis mine)

    get_meta_tags — Extracts all meta tag content attributes from a file and returns an array

    Comments on this post

    • UniqueIdeaMan agrees : I'll give you a REP for atleast replying.
    -- Cigars, whiskey and wild, wild women. --
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2016
    Location
    Cheshire, UK
    Posts
    90
    Rep Power
    72
    Only meta elements that have a name attribute

    Comments on this post

    • UniqueIdeaMan agrees : Well explained according to Sepodati and so I'll accept it as that too!
  26. #14
  27. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Caro, Michigan
    Posts
    14,961
    Rep Power
    4575
    Originally Posted by Barand
    Only meta elements that have a name attribute
    Good point. Thanks.
    -- Cigars, whiskey and wild, wild women. --
  28. #15
  29. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2017
    Posts
    845
    Rep Power
    0
    Originally Posted by Barand
    Only meta elements that have a name attribute
    You mean it will scrape the following ?
    <meta name=”description” content=”This is where you add your meta description.”>
    Because this meta tag has the name attribute, this would show as the scrape result ?
    This is where you add your meta description

    But, if it did not have the name attribute then nothing would get scraped ?
    Also, I do not see the meta keywords tag here. Don't they use it any more ?


    I found the sample here:
    https://www.metatags.org/examples_making_meta_tags

    In the olden days did they not come like any of these 2 ?
    1
    <meta keywords>sepodai, barand</met keywords>
    <meta description>barand and sepodati are members of devshed forum</meta description>

    2
    <meta keywords = "sepodai, barand">
    <meta description ="barand and sepodati are members of devshed forum">

    I got into meta tags back in 1998 and then dropped using them after the SE's dropped giving importance to them. And so, can't remember how they used to be.
    Which one was it that they used, out of the 2, in the olden days ?
    Last edited by UniqueIdeaMan; May 26th, 2018 at 01:18 PM.
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo