Whats the problem with this mysql query

Discussion in 'PHP' started by krishmk, May 15, 2009.

  1. #1
    Basically I want to pull out info from database and display it in a table.
    If there is no result matching the query (status=1), it displays the error message without any problem. However when there is one or more result I just see the blank html table with headers but with no data. I mean the variable ($proxy_name, $location, $url etc.) are not being interpreted. [Result Page]

    I need to pull information from 2 tables. The "hits count" has to be derived from the TABLE hits and the rest of the information should be from the TABLE "proxies_info". I have included the second query inside the while loop. Is that the problem?

    <?php
    include ('db_info.inc');
    $con = mysql_connect($hostname, $dbuser, $dbpass);
    $select = mysql_select_db ($selectdb, $con);
    $query = "SELECT * FROM proxies_info WHERE status = 1";
    $result = mysql_query ($query, $con);
    $number_rows = mysql_num_rows ($result);
    if ($number_rows == 0)
    {
    $error_message = "OOPS, We are sorry we dont have any proxies as of yet";
    }
    
    else
    {
    while ($row = mysql_fetch_array ($result))
    {
    $proxy_name = $row ['site_name'];
    $url = $row ['url'];
    $location = $row ['server_location'];
    $date = $row ['entry_date'];
    $query2 = "SELECT * FROM hits WHERE url = $url";
    $result2 = mysql_query ($query2, $con);
    
    while ($row2 = mysql_fetch_array ($result))
    {
    $hits = $row2['counts'];
    }
    }
    }
    
    ?>
    PHP:
    after this I have some HTML code [and below this I have the following php script]

    <?php
    if (isset($error_message))
    {
    echo ("$error_message");
    }
    
    else
    {
    echo ("<table id=\"list\">\n<tr>\n<th>Proxy Name</th><th>Proxy Url</th><th>Server Location</th><th>Date Added</th><th>Hits</th><th>More Info</th></tr>");
    while($row = mysql_fetch_array( $result ))
    {
    echo ("<tr>\n<td>$proxy_name</td><td>$url</td><td>$location</td><td>$date</td><td>$hits</td><td><a href=\"info.php?url=$url\">Info</a></td></tr>\n");
    }
    echo ("</table>");
    }
    
    ?>
    PHP:
     
    krishmk, May 15, 2009 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    My guess is that you're missing a number "2" after your second mysql_fetch_array. Ie, change this:
    
    $query2 = "SELECT * FROM hits WHERE url = $url";
    $result2 = mysql_query ($query2, $con);
    while ($row2 = mysql_fetch_array ($result))
    
    PHP:
    to
    
    $query2 = "SELECT * FROM hits WHERE url = $url";
    $result2 = mysql_query ($query2, $con);
    while ($row2 = mysql_fetch_array ($result2))
    
    PHP:
     
    PoPSiCLe, May 15, 2009 IP
  3. krishmk

    krishmk Well-Known Member

    Messages:
    1,376
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    185
    #3
    Thanks for finding it.
    Now I get a new error message.
    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/xxxx/public_html/webproxies.biz/web_proxies.php on line 24

    Line 24 has the following code:
    while ($row2 = mysql_fetch_array ($result2))
    {
    $hits = $row2['counts'];
    }

    ----------------------------------------------
    Also I guess the problem may be becoz of the $url found in query2 ($url has to be captured from the result of query1)
    $query2 = "SELECT * FROM hits WHERE url = $url";
     
    krishmk, May 15, 2009 IP
  4. grutland

    grutland Active Member

    Messages:
    86
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    71
    #4
    If you look at your SQL:
    $query2 = "SELECT * FROM hits WHERE url = $url";
    PHP:
    You are not selecting a field called "counts", only "hits".
    I'm fairly sure this is what is causing the error.
     
    grutland, May 15, 2009 IP
  5. grutland

    grutland Active Member

    Messages:
    86
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    71
    #5
    You know what, ignore that.
    I read the SQL wrong, did notice the '*', sorry
     
    grutland, May 15, 2009 IP
  6. krishmk

    krishmk Well-Known Member

    Messages:
    1,376
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    185
    #6
    Problem is solved now.
    I just took off the second while loop and merged the php scripts as 1 script below the HTML code.
    Thanks to all who tried to solve this problem.
     
    krishmk, May 15, 2009 IP
  7. pitgo

    pitgo Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Error
    Database query error


    What is this error

    Database error
    Query:
    SELECT * FROM `Banners` WHERE `Active` 0 AND `campaign_start` = NOW() AND `Position` LIKE '%2%' ORDER BY RAND() LIMIT 1

    Mysql error:
    Can't create/write to file '/tmp/#sql_e06_0.MYI' (Errcode: 13)

    Found error in file /home/hamed/public_html/inc/banners.inc.php
    at line 46. Called db_arr function
    with erroneous argument #0

    Debug backtrace:
    Array
    (
    [1] => Array
    (
    [file] => /home/hamed/public_html/inc/db.inc.php
    [line] => 115
    [function] => db_res
    [args] => Array
    (
    [0] => SELECT * FROM `Banners` WHERE `Active` 0 AND `campaign_start` = NOW() AND `Position` LIKE '%2%' ORDER BY RAND() LIMIT 1
    [1] => 1
    )

    )

    [2] => Array
    (
    [file] => /home/hamed/public_html/inc/banners.inc.php
    [line] => 46
    [function] => db_arr
    [args] => Array
    (
    [0] => SELECT * FROM `Banners` WHERE `Active` 0 AND `campaign_start` = NOW() AND `Position` LIKE '%2%' ORDER BY RAND() LIMIT 1
    )

    )

    [3] => Array
    (
    [file] => /home/hamed/public_html/inc/design.inc.php
    [line] => 365
    [function] => banner_put_nv
    [args] => Array
    (
    [0] => 2
    )

    )

    [4] => Array
    (
    [function] => TmplKeysReplace
    [args] => Array
    (
    [0] => Array
    (
    [0] => __banner_left__
    [1] => banner_left
    )

    )

    )

    [5] => Array
    (
    [file] => /home/hamed/public_html/inc/design.inc.php(493) : runtime-created function
    [line] => 27
    [function] => preg_replace_callback
    [args] => Array
    (
    [0] => /__([a-zA-Z0-9_-]+)__/
    [1] => TmplKeysReplace
    [2] =>



    __page_header__









    __styles__
    __java_script__

    var urlIconLoading = '__images__loading.gif';





    $(document).ready( function() { //onload
    //apply top menu functionality
    $( 'table.topMenu' ).dolTopMenu();

    //apply rss aggregator
    sClockIcon = '__images__icons/clock.gif';
    $( 'div.RSSAggrCont' ).dolRSSFeed();
    } );





    __ray_invite_js__

    __extra_js__

    __extra_css__











    __ray_invite_swf__

    __banner_left__
    __banner_right__





    __TOP_Links__
    __TOP_News__
    __TOP_ContactUs__
    __TOP_About__
    __TOP_Privacy__
    __TOP_Termsofuse__
    __TOP_FAQ__
    __TOP_Feedback__
    __TOP_Aff__
    __TOP_Invitefriend__
    __TOP_Bookmark__
    __switch_lang_block__




    __main_logo__
    __hello_member__

    __banner_top__

    __top_menu__




    __page_header_text__





    __page_header_text__


    __page_main_code__








    __banner_bottom__





    __BMI_Links__
    __BMI_News__
    __BMI_ContactUs__
    __BMI_About__
    __BMI_Privacy__
    __BMI_Termsofuse__
    __BMI_FAQ__
    __BMI_Feedback__
    __BMI_Aff__
    __BMI_Invitefriend__
    __BMI_Bookmark__

    __copyright__







    )

    )

    [6] => Array
    (
    [file] => /home/hamed/public_html/inc/design.inc.php
    [line] => 489
    [function] => __lambda_func
    [args] => Array
    (
    [0] =>



    __page_header__









    __styles__
    __java_script__

    var urlIconLoading = '__images__loading.gif';





    $(document).ready( function() { //onload
    //apply top menu functionality
    $( 'table.topMenu' ).dolTopMenu();

    //apply rss aggregator
    sClockIcon = '__images__icons/clock.gif';
    $( 'div.RSSAggrCont' ).dolRSSFeed();
    } );





    __ray_invite_js__

    __extra_js__

    __extra_css__











    __ray_invite_swf__

    __banner_left__
    __banner_right__





    __TOP_Links__
    __TOP_News__
    __TOP_ContactUs__
    __TOP_About__
    __TOP_Privacy__
    __TOP_Termsofuse__
    __TOP_FAQ__
    __TOP_Feedback__
    __TOP_Aff__
    __TOP_Invitefriend__
    __TOP_Bookmark__
    __switch_lang_block__




    __main_logo__
    __hello_member__

    __banner_top__

    __top_menu__




    __page_header_text__





    __page_header_text__


    __page_main_code__








    __banner_bottom__

    __boonex_footers__



    __BMI_Links__
    __BMI_News__
    __BMI_ContactUs__
    __BMI_About__
    __BMI_Privacy__
    __BMI_Termsofuse__
    __BMI_FAQ__
    __BMI_Feedback__
    __BMI_Aff__
    __BMI_Invitefriend__
    __BMI_Bookmark__

    __copyright__







    )

    )

    [7] => Array
    (
    [file] => /home/hamed/public_html/guestbook.php
    [line] => 60
    [function] => PageCode
    [args] => Array
    (
    )

    )

    )


    Called script: /guestbook.php

    Request parameters:
    Array
    (
    [owner] => 3
    [action] => show_add
    )


    --
    Auto-report system

     
    pitgo, Jun 18, 2009 IP