test mysql_query result

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ThaJock
Forum Newbie
Posts: 11
Joined: Tue Nov 04, 2008 10:15 pm

test mysql_query result

Post by ThaJock »

I am trying to write a script that does the following:

1. Loop through my audio folder which contains the following four files:
I'll Be Right Here.mp3
Time Marches On.mp3
Where Did It Go (Our Love).mp3
You're Still The One.mp3

2. Check for .mp3 file extension

3. Query table to see if the file name as string exists in my table(the audio table is empty)
_If not then insert the file name into the table.

Here is my code:

Code: Select all

<?php
include("connect.php");
if ($handle = opendir("audio")) {
    while (false !== ($file = readdir($handle))) {
        
        $checker = end(explode('.', $file));
        if($checker=='mp3'){
            Update($file);
        }
    }
    closedir($handle);
}
 
function Update($audio){
    $result = mysql_query("SELECT * FROM audio WHERE name = '$audio'");
    if(!$result){
        echo $audio."</br>";
        //mysql_query("INSERT INTO audio (name) VALUES ('$audio')");
    }
}
?>
Everything seems to work fine except it only echos these two files:
I'll Be Right Here.mp3
You're Still The One.mp3

If anyone can help me understand why the other 2 strings are not being echoed I would appreciate it.
danielrs1
Forum Commoner
Posts: 29
Joined: Wed Jun 24, 2009 5:30 pm

Re: test mysql_query result

Post by danielrs1 »

Replace the line:

Code: Select all

$result = mysql_query("SELECT * FROM audio WHERE name = '$audio'");
With this:

Code: Select all

$result = mysql_query("SELECT * FROM audio WHERE name = '".mysql_real_escape_string($audio)."'");
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: test mysql_query result

Post by requinix »

Don't forget to do it with the INSERT too.

Code: Select all

if(!$result){
$result will only tell you if the query was valid - not whether it returned results.

Code: Select all

$result = mysql_query("SELECT * FROM audio WHERE name = '$audio'");
if($result && mysql_num_rows($result) == 0) {
danielrs1
Forum Commoner
Posts: 29
Joined: Wed Jun 24, 2009 5:30 pm

Re: test mysql_query result

Post by danielrs1 »

lol I forgot that. If you only make the replacement I suggested, then you'd get no results.

The code you should use is:

Code: Select all

<?php
include("connect.php");
if ($handle = opendir("audio")) {
  while (false !== ($file = readdir($handle))) {
    
     $checker = end(explode('.', $file));
    if($checker=='mp3'){
      Update($file);
    }
  }
 closedir($handle);
}
 
function Update($audio){
  $result = mysql_query("SELECT * FROM audio WHERE name = '".mysql_real_escape_string($audio)."'");
  if($result && mysql_num_rows($result) == 0){
    echo $audio."</br>";
    //mysql_query("INSERT INTO audio (name) VALUES ('$audio')");
  }
}
?>
ThaJock
Forum Newbie
Posts: 11
Joined: Tue Nov 04, 2008 10:15 pm

Re: test mysql_query result

Post by ThaJock »

Thank you guys so much for your help. I am new to php and really only know some of the basics. I am very happy that I joined this community. The turn around time is amazing.

I'm going to study what you guys have shown me in this post and learn from it. Thanks again for your help.
Post Reply