开发者

mysql statement not working

开发者 https://www.devze.com 2023-04-13 00:54 出处:网络
I have this code if(!isset($_GET[\'album_id\'])) { die(\"Album Not Found!\"); } else { $album_id = mysql_real_es开发者_C百科cape_string($_GET[\'album_id\']); }

I have this code

if(!isset($_GET['album_id'])) { die("Album Not Found!"); } else { $album_id = mysql_real_es开发者_C百科cape_string($_GET['album_id']); }

$sql = "SELECT * FROM `audio_albums` WHERE `album_id` = ".$album_id."";
$qry = mysql_query($sql);
$num = mysql_num_rows($qry);

if($num ==  1) {
    // Fetch Array
    $arr = mysql_fetch_array($qry);
    // Assign Values
    $album_name     = $arr['album_name'];
    $album_name_seo = $arr['album_name_seo'];
    $album_id       = $arr['album_id'];
    // Fetch Songs
    $sql2 = "SELECT audio_id,album_id,title FROM `audios` WHERE `album_id` = ".$album_id." AND `public_private` = 'public' AND `approved` = 'yes' LIMIT 0, 30 ";
    $qry2 = mysql_query($sql2);
    $arr2 = mysql_fetch_array($qry2);
    print_r($arr2);
} else {
    echo "Album Not Found!";
} 

and when i execute the code, it results in this

Array
(
    [0] => qCpPdBZIpkXfVIg4iUle.mp3
    [audio_id] => qCpPdBZIpkXfVIg4iUle.mp3
    [1] => 1
    [album_id] => 1
    [2] => Ambitionz Az a Ridah
    [title] => Ambitionz Az a Ridah
)

Actually it fetches data of only one row but there are several rows in result. Whats wrong in the code? why isn't it working?


Well, mysql_fetch_array fetches one row. You just need to do a loop to fetch all of them, as the manual shows: http://php.net/mysql_fetch_array

while ($arr2 = mysql_fetch_array($qry2)) {
    print_r($arr2);
}


You really should learn some SQL ( yes , actually learn it ), and stop using the horribly outdated mysql_* functions.

$stmt = $dbh->prepare('
    SELECT 
       audios.audio_id AS audio_id
       audio_albums.album_id AS album_id
       audios.title AS title
       audio_albums.album_name AS album
       audio_albums.album_name_seo AS seo_album
    FROM audios
       LEFT JOIN audio_albums USING (album_id)
    WHERE
       audio_albums.album_id = :id
       audios.public_private = "public" AND 
       audios.approved = "yes" 
    LIMIT 0, 30
');
$stmt->bindParam( ':id', $_GET['album_id'], PDO::PARAM_INT );
if ( $stmt->execute() )
{
   var_dump( $stmt->fetchAll(PDO::FETCH_ASSOC) );
}else{
   echo 'empty .. try another';
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号