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

  • Home
  • SEARCH
  • 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 597227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:15:27+00:00 2026-05-13T16:15:27+00:00

$res (array)-> (count 50 (!) ) Example: ( [1] => Array ( [artistname] =>

  • 0

$res (array)-> (count 50 (!) )
Example:

(
    [1] => Array
        (
            [artistname] => Lady GaGa
            [songname] => Love Games
            [duration] => 3:31
            [url] => 7e91a5ca16ae
            [server] => 3
        )

    [2] => Array
        (
            [artistname] => DJ Layla
            [songname] => Single Lady
            [duration] => 3:20
            [url] => f0906a3087eb
            [server] => 3
        )

    [3] => Array
        (
            [artistname] => Lady Gaga
            [songname] => Bad Romance (Bimbo Jones Clean Radio Remix)
            [duration] => 3:59
            [url] => 36e77d5a80357
            [server] => 3
        )
}

PHP code:

$massquery  = '';
foreach($res as $value)
{
    if(!get_magic_quotes_gpc())
    {
        $value['artistname']    = mysql_escape_string($value['artistname']);
        $value['songname']      = mysql_escape_string($value['songname']);
        $value['duration']      = mysql_escape_string($value['duration']);
        $value['url']           = mysql_escape_string($value['url']);
        $value['server']        = mysql_escape_string($value['server']);
    }

    $value['artistname']    = trim($value['artistname']);
    $value['songname']      = trim($value['songname']);
    $value['duration']      = trim($value['duration']);
    $value['url']           = trim($value['url']);
    $value['server']        = trim($value['server']);

    $sh     = mysql_query("SELECT `artistname`,`songname`,`server` FROM `music` WHERE `artistname`='".$value['artistname']."' AMD `songname`='".$value['songname']."' AND `server`='".$value['server']."' LIMIT 1");
    if(!mysql_num_rows($sh))
    {
        $massquery  .= '("'.$value['artistname'].'", "'.$value['songname'].'", "'.$value['duration'].'", "'.$value['url'].'", "'.$value['server'].'"),';
    }
}

if(!empty($massquery))
{
    $massquery  = substr($massquery, 0, -1);
    $query      = mysql_query('INSERT INTO `music` (`artistname`, `songname`, `duration`, `url`, `server`) VALUES '.$massquery);
}
mysql_close($mysql);

It turns out 50 requests “SELECT” to the database, which is very bad = (
How can I optimize this code?

From answers:

CREATE TABLE `music` (
  `id` int(50) NOT NULL auto_increment,
  `artistname` varchar(50) NOT NULL,
  `songname` varchar(50) NOT NULL,
  `duration` varchar(6) NOT NULL,
  `url` varchar(255) NOT NULL,
  `server` int(5) NOT NULL,
  PRIMARY KEY  (`id`),
  KEY `artistname` (`artistname`,`songname`,`server`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8;

INSERT INTO `music` VALUES ('test', 'btest', 1);

…

SELECT `artistname` , `songname` , `server`
FROM `music`
WHERE FALSE
OR (
`artistname` = 'test'
AND `songname` = 'btest'
AND `server` = '1'
)
OR (
`artistname` = 'sas'
AND `songname` = 'asf'
AND `server` = '1'
)
LIMIT 0 , 30 

How do I INSERT those songs that are not yet in the database?
Sorry for bad english

  • 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-05-13T16:15:27+00:00Added an answer on May 13, 2026 at 4:15 pm

    You want to insert new records only if no other record with the tuple (artistname,songname,server) (already) exists.
    If you create a unique index for these three fields MySQL won’t insert a doublet. Then you can either use something like

    INSERT IGNORE INTO
      tablename
      (a,b,c,x,y,z) 
    VALUES
      (1,2,3,4,5,6),
      (7,8,9,10,11,12),
      ...
      (95,96,97,98,99,100)
    

    or a prepared statement, e.g.

    $pdo = new PDO("mysql:host=localhost;dbname=test", 'localonly', 'localonly'); 
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    /* test table */
    $pdo->exec('
        CREATE TEMPORARY TABLE foo (
            id int auto_increment,
            artistname varchar(64) not null,
            songname varchar(64) not null,
            duration varchar(16) not null,
            url varchar(64) not null,
            server int not null,
            primary key(id),
            unique key (artistname,songname,server)
        )
    ');
    
    $data = array(
        array(':artistname' => 'Lady GaGa', ':songname' => 'Love Games', ':duration' => '3:31', ':url' => '7e91a5ca16ae', ':server' => 3),
        array(':artistname' => 'DJ Layla', ':songname' => 'Single Lady', ':duration' => '3:20', ':url' => 'f0906a3087eb', ':server' => 3),
        array(':artistname' => 'Lady Gaga', ':songname' => 'Bad Romance (Bimbo Jones Clean Radio Remix)', ':duration' => '3:59', ':url' => '36e77d5a80357', ':server' => 3)
    );
    
    
    /* the "actual" test script */
    $stmt = $pdo->prepare('
        INSERT IGNORE INTO
            foo
            (duration, artistname, songname, server, url)
        VALUES
            (:duration, :artistname, :songname, :server, :url)
    ');
    // first run, all three records should be inserted
    foreach( $data as $params ) {
        $stmt->execute($params);
    }
    
    // second run
    // same artist/songname, different server
    $newData = $data[0]; $newData[':server'] = 4;
    $data[] = $newData;
    // and a completly new record
    $data[] = array(':artistname' => 'xyz', ':songname' => 'The ABC song', ':duration' => '2:31', ':url' => 'whatever', ':server' => 2);
    // again insert all records (including the three that have already been inserted)
    foreach( $data as $params ) {
        $stmt->execute($params);
    }
    
    /* fetch all records */
    foreach( $pdo->query('SELECT * FROM foo', PDO::FETCH_NUM) as $row ) {
        echo join(', ', $row), "\n";
    }
    

    prints

    1, Lady GaGa, Love Games, 3:31, 7e91a5ca16ae, 3
    2, DJ Layla, Single Lady, 3:20, f0906a3087eb, 3
    3, Lady Gaga, Bad Romance (Bimbo Jones Clean Radio Remix), 3:59, 36e77d5a80357, 3
    4, Lady GaGa, Love Games, 3:31, 7e91a5ca16ae, 4
    5, xyz, The ABC song, 2:31, whatever, 2
    

    The first three records have not been duplicated.

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

Sidebar

Ask A Question

Stats

  • Questions 408k
  • Answers 408k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Function instances have a non-standard name attribute which will return… May 15, 2026 at 6:51 am
  • Editorial Team
    Editorial Team added an answer How to use UTF-8: import codecs # ... # title… May 15, 2026 at 6:51 am
  • Editorial Team
    Editorial Team added an answer It was indeed a configuration error, just not one that… May 15, 2026 at 6:51 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.