0

I'd really appreciate some help with this code as I can't get it to work properly.

I have two separate functions that both check a table in my database for data against an ID that is fetched from the page's URL. On displaying the information, I want to use an IF ELSE statement to check if there are results from either of those functions, and if there are no results, post nothing, and if there are results, post the results.

Below are my functions:

function getArtistsBySongId($id) {
    $query = "SELECT * FROM `Credit_To_Artist` AS c2a 
    INNER JOIN `Credits` AS cr ON cr.credit_id = c2a.credit_id
    INNER JOIN `Artist` AS a ON a.artist_id = c2a.artist_id
    LEFT OUTER JOIN `Song` AS s ON s.song_id = c2a.song_id
    LEFT OUTER JOIN `Remix` AS r ON r.remix_id = c2a.remix_id
    LEFT OUTER JOIN `Project` AS p ON p.project_id = s.project_id
    WHERE c2a.song_id = $id
    ORDER BY a.artist_name ASC";

    $res = mysql_query($query);

    $artists = Array();
    $artisttoid = Array();
    $songtoid = Array();

    while( $row = mysql_fetch_array($res) ) {
        $artist = $row[artist_name];
        $credit = $row[credit_name];
        $songcr = $row[song_id];

        if(!array_key_exists($artist, $artists) ) {
            $artists[$artist] = Array();
            $artisttoid[$artist] = $row[artist_id];
            $songtoid[$songcr] = $row[song_id];
        }

    $artists[$artist][] = $credit;

    } 
    return array($artists, $artisttoid, $songtoid);
}

function getGroupsBySongId($id) {
    $query = "SELECT * FROM `Credit_To_Artist` AS c2a 
    INNER JOIN `Credits` AS cr ON cr.credit_id = c2a.credit_id
    INNER JOIN `Artist_Group` AS ag ON ag.group_id = c2a.group_id
    LEFT OUTER JOIN `Song` AS s ON s.song_id = c2a.song_id
    LEFT OUTER JOIN `Remix` AS r ON r.remix_id = c2a.remix_id
    LEFT OUTER JOIN `Project` AS p ON p.project_id = s.project_id
    WHERE c2a.song_id = $id
    ORDER BY ag.group_name ASC";

    $res = mysql_query($query);

    $groups = Array();
    $grouptoid = Array();
    $song2id = Array();

    while( $row = mysql_fetch_array($res) ) {
        $group = $row[group_name];
        $credits = $row[credit_name];
        $songcred = $row[song_id];

        if(!array_key_exists($group, $groups) ) {
            $groups[$group] = Array();
            $grouptoid[$group] = $row[group_id];
            $song2id[$songcred] = $row[song_id];
        }
        $groups[$group][] = $credits;
     } 
    return array($groups, $grouptoid, $song2id);
    }

At the moment I have this code:

<?php
if ((getArtistsBySongId($id) != NULL) OR (getGroupsBySongId($id) != NULL)) {
     include 'songs/getsongcredits.php';
}
?>

While the code works in displaying my data, it seems to be ignoring my IF statement, and just posting what's in the include. Would someone be able to let me know the correct way to do this? Thanks in advance.

2
  • 1
    Don't use mysql_* functions annymore they are unsafe and deprecated.. use mysqli_* functions or PDO instead Commented May 19, 2017 at 8:21
  • Building SQL strings from external input makes your application vulnerable to SQL injection attacks. bobby-tables.com Commented May 19, 2017 at 8:39

3 Answers 3

2

Both of your functions are returning an array regardless of the outcome of the query. Therefore you should check if the result returned from your functions are empty or not.

<?php
if (!empty(getArtistsBySongId($id)) OR !empty(getGroupsBySongId($id))) {
  include 'songs/getsongcredits.php';
}
?>
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, I've included your code above but it breaks the page. Does the syntax need to be changed?
There was a syntax error on the if statement. The answer has been updated with the correct syntax. There was a missing brace in the if statement.
0

Since both of your functions return arrays I would consider checking the size of the arrays returned. If you have data then the array size would be greater than 0 otherwise it would be 0.

<?php
$artistsBySongId = count(getArtistsBySongId($id));
$groupsBySongId = count(getGroupsBySongId($id));
if (($artistsBySongId != 0) || ($groupsBySongId != 0)) {
 include 'songs/getsongcredits.php';
}

?>

2 Comments

If all you want to know is whether there was a result returned or not then empty() is the best way to do it. Only use count() when you have a need to know exactly how many things there are.
I agree with you @GordonM
0

Thanks all for taking the time to answer my question. However, neither of the codes worked in my site. A friend of mine has helped me though and it is now working. This is the code he used:

<?php

$errors = array_filter(getArtistsBySongId( $id ));

$errors1 = array_filter(getGroupsBySongId( $id ));

if (empty($errors) AND empty($errors1)) {

} else {

    include 'songs/getsongcredits.php';

}

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.