SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Hybrid View

  1. #1
    SitePoint Zealot
    Join Date
    Jul 2000
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,
    I have a problem with my script, and I can't seem to fix it. So hopefully someone will help me
    Right, the script is for my search engine which launches soon.
    What it does, is when a user searches for something, I want to log it in a database of all search queries, so I know what people are searching for. So, if the keywords exists, it should add one to the popularity of that keyword, otherwise, if the keyword doesn't exist, it should add a new entry with the keyword, and set the popularity as one.
    You see?? Quite simple really.

    Ok, so the current code is:
    Code:
    <?php
    // if the keyword exists
    
    if ( isset($keyword) )  {
    
    mysql_connect("localhost", "root", "");
    mysql_select_db("eliter");
    
    $result = mysql_query("SELECT COUNT(*) FROM keywords WHERE keyword='$keyword' "); 
    $rows = mysql_num_rows($result);
    
    if ( $rows == '0' ) {
    
    mysql_query("INSERT INTO keywords SET keywords='$keyword', popularity='1' ");
    
    echo("<font size=1 face=verdana color=c0c0c0>The new keyword has been added to the database</font>");
    
    
    } else {
    
    // find the popularity of the keyword
    
    $selectpop = mysql_query("SELECT popularity FROM keywords WHERE keywords='$keyword' ");
    
    while ( $details = mysql_fetch_array($selectpop) ) {
    
    $popularity = $details["popularity"];
    
    
    // new popularity = old popularity plus one
    
    $newpopularity = $popularity + 1;
    
    
    
    mysql_query("UPDATE keywords SET popularity='$newpopularity' WHERE keywords='$keyword' ");
    
    echo ("
    <font size=1 face=verdana color=c0c0c0>
    The keyword $keyword has been adjusted.</font><br>
    ");
    
    }
    
    } 
    
    } else {
    
     echo ("
    <font size=1 face=verdana color=c0c0c0>
    There was an <b>error</b> adding the keyword into the database.</font>
    ");
    
    } 
    
    ?>
    The error I get is "Warning: Supplied argument is not a valid MySQL result resource in C:/PROGRAMMING/eliter/kwlog.php on line 11".

    Before I added the part where it checks if the keyword exists, it worked fine, but now it doesn't..
    Could someone please help?
    Cheers.
    -AJ

  2. #2
    SitePoint Wizard jumpthru's Avatar
    Join Date
    Apr 2000
    Location
    Los Angeles, California
    Posts
    1,008
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $result = mysql_query("SELECT COUNT(*) FROM keywords WHERE keyword='$keyword' ");
    $rows = mysql_num_rows($result);

    if ( $rows == '0' ) { //This line is invalid

    mysql_query("INSERT INTO keywords SET keywords='$keyword', popularity='1' ");

    echo("<font size=1 face=verdana color=c0c0c0>The new keyword has been added to the database</font>");
    That needs to be changed to the following

    Code:
    $result = mysql_query("SELECT COUNT(*) FROM keywords WHERE keyword='$keyword' "); 
    $rows = mysql_num_rows($result);
    
    if ($rows) { //Change this line
    
    mysql_query("INSERT INTO keywords SET keywords='$keyword', popularity='1' ");
    
    echo("<font size=1 face=verdana color=c0c0c0>The new keyword has been added to the database</font>");

  3. #3
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    One other thing in the follwoing line
    $result = mysql_query("SELECT COUNT(*) FROM keywords WHERE keyword='$keyword' ");
    $rows = mysql_num_rows($result);

    It appears that in your where clause you call fieldname "keyword" but everywhere else you use fieldname "keywords" If the fieldname is keywords then change it to be keywords if it is "keyword" change all the rest to be "keyword"
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  4. #4
    SitePoint Zealot
    Join Date
    Jul 2000
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Guys, thanks for your help.

    Now, I do not get an error.

    However, it still does not work as it should.

    So if I enter kwlog.php?keyword=BLAHBLAH ( and the keyword blahblah is not already in the db), it DOES add it.
    However, if I refresh the page, then it says the same message:
    "the new keyword has been added to the database" or whatever it is.

    It says this also when the keyword already exists, and it does not update the popularity.

    Any clues why this still isn't working?
    Thanks again
    -AJ
    PS: Personally, I reckon the Mysql_num_rows thing is dodgy, because i have never used that or the COUNT function before.


  5. #5
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:
    <?php
    // if the keyword exists

    if ($keyword) {
    mysql_connect("localhost", "root", "");
    mysql_select_db("eliter");
    $result = mysql_query("SELECT * FROM keywords WHERE keywords = '$keyword' ");
    if (mysql_num_rows($result) > 0) {
    $details = mysql_fetch_array($result);
    $popularity = $details["popularity"];
    // new popularity = old popularity plus one
    $newpopularity = $popularity + 1;
    mysql_query("UPDATE keywords SET popularity='$newpopularity' WHERE keywords='$keyword' ");
    echo ("<font size=1 face=verdana color=c0c0c0>The keyword $keyword has been adjusted.</font><br>");
    }
    else {
    mysql_query("INSERT INTO keywords SET keywords='$keyword', popularity='1' ");
    echo("<font size=1 face=verdana color=c0c0c0>The new keyword has been added to the database</font>");
    }
    }
    else {
    echo ("<font size=1 face=verdana color=c0c0c0>There was an <b>error</b> adding the keyword into the database.</font>");
    }

    ?>
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  6. #6
    SitePoint Zealot
    Join Date
    Jul 2000
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You beauty!

    It works like a dream!
    Thanks again mate! You have helped me out quite a bit so far!
    -AJ


Bookmarks

Posting Permissions

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