Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8045669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:37:04+00:00 2026-06-05T05:37:04+00:00

I have a pretty simple mysql request, 1. request calls to table to get

  • 0

I have a pretty simple mysql request,
1. request calls to table to get an id, if an id exists function ends, if no id, then the id is not banned and 2nd requests, see if the id exists in public table, if no it adds, if it does it counts up 1 for a page view.

Currently this function seems to be causing a serious slow down.
af_ban table has about 8000 rows
af_freefeed table has about 1,000,000 rows. About 10,000 rows added per day.

function pageview($pageid, $pagename){
$sql=mysql_num_rows(mysql_query("SELECT * FROM anotherfeedv3.af_ban WHERE pageid = '".$pageid."'"));
    if(!$sql){
        $sql=mysql_num_rows(mysql_query("SELECT pageid FROM anotherfeedv3.af_freefeed WHERE pageid = '".$pageid."'"));
            if(!$sql){
            $sql = 'INSERT INTO `anotherfeedv3`.`af_freefeed` (`id`, `pageid`, `userid`, `views`, `pagename`) VALUES (NULL, \''.$pageid.'\', \'\', \'1\', \''.htmlspecialchars($pagename, ENT_QUOTES).'\');';
            $insert=mysql_query($sql) or die ('Error! in Insert '.$pageid.' SQL');
            }else{
            $sql = 'UPDATE `anotherfeedv3`.`af_freefeed` SET `pagename` = \''.htmlspecialchars($pagename, ENT_QUOTES).'\', views=views+1 WHERE `af_freefeed`.`pageid` = '.$pageid.';';
            //$sql = 'UPDATE `anotherfeedv3`.`af_freefeed` SET views=views+1 WHERE `af_freefeed`.`pageid` = '.$pageid.';';
            $update=mysql_query($sql) or die ('Failed update of '.$pageid.' in mysql.');
        }
    }
};

Is there a better way i can write this?


Solution!

function pageview($pageid, $pagename){
$q = sprintf('SELECT COUNT(*) AS numrows FROM anotherfeedv3.af_ban WHERE pageid = %d', $pageid);
$r = mysql_query($q);
$a = mysql_fetch_assoc($r);
if($a['numrows'] > 0){ /* ... */ }else {
    $q = sprintf('SELECT COUNT(*) AS numrows FROM anotherfeedv3.af_freefeed WHERE pageid = %d', $pageid);
    $r = mysql_query($q);
    $a = mysql_fetch_assoc($r);
    if($a['numrows'] > 0){ /* ... */ 
    $sql = 'UPDATE `anotherfeedv3`.`af_freefeed` SET `pagename` = \''.htmlspecialchars($pagename, ENT_QUOTES).'\', views=views+1 WHERE `af_freefeed`.`pageid` = '.$pageid.';';
            //$sql = 'UPDATE `anotherfeedv3`.`af_freefeed` SET views=views+1 WHERE `af_freefeed`.`pageid` = '.$pageid.';';
    $update=mysql_query($sql) or die ('Failed update of '.$pageid.' in mysql.');
    }else {
    $sql = 'INSERT INTO `anotherfeedv3`.`af_freefeed` (`id`, `pageid`, `userid`, `views`, `pagename`) VALUES (NULL, \''.$pageid.'\', \'\', \'1\', \''.htmlspecialchars($pagename, ENT_QUOTES).'\');';
            $insert=mysql_query($sql) or die ('Error! in Insert '.$pageid.' SQL');
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T05:37:07+00:00Added an answer on June 5, 2026 at 5:37 am

    With these rows:

    $sql=mysql_num_rows(mysql_query("SELECT * FROM anotherfeedv3.af_ban WHERE pageid = '".$pageid."'"));
    if(!$sql){ /* ... */ }
    

    It appears that you’re only interested in whether results exist.

    If thats the case, you could replace it with something like:

    $q = sprintf('SELECT COUNT(*) AS numrows FROM anotherfeedv3.af_ban WHERE pageid = %d', $pageid);
    $r = mysql_query($q);
    $a = mysql_fetch_assoc();
    if($a['numrows'] > 0){ /* ... */ }
    

    You could condense this, but I split it up pretty liberally for ease of understanding.

    Basically, this lets MySQL determine the number of rows without generating a results set.

    Further, you could use this technique to combine the first two queries:

    $q = 'SELECT (';
    $q.= '    SELECT COUNT(*) FROM anotherfeedv3.af_ban WHERE pageid = %d';
    $q.= ') AS banrows, (';
    $q.= '    SELECT COUNT(*) FROM anotherfeedv3.af_freefeed WHERE pageid = %d';
    $q.= ') AS freerows';
    $r = mysql_query(sprintf($q, $pageid, $pageid);
    $a = mysql_fetch_assoc();
    

    The result of the query will be one row with two columns (banrows, freerows). The values will be the number of rows that match the respective queries above.

    You would then use $a['banrows'] and $a['freerows'] to get the row counts for your later logic.

    If you’re running this query multiple times, there may be some additional performance improvement to be seen if you use PDO and PDO::prepare().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a pretty simple case which I started solving using foreach(), but then
I have the following piece of code, executing a pretty simple MySQL query: $netnestquery
I have a pretty simple LIKE search for MySQL that i'd like to transform
I have the PHP/MySQL done for a polling system. It pretty simple, basically the
I have a table that tracks emails sent. It is pretty simple. ID |
i have pretty simple simple question (i hope so). How do i change the
I have a pretty simple ASP.NET Web Form that looks a bit like the
I have a pretty simple Linq to XML query: var q = from c
I have a pretty simple SQL I need to perform. I have a ProcessUser
I have a pretty simple question which perhaps someone familiar with Server/Client design &

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.