Problem with select in PHP

Asked

Viewed 52 times

0

Notice: Undefined index: album_name in C: xampp htdocs gallery.php on line 69

Line 69:

        $album_name = $db->data[0]['album_name'];

code of this part:

$db->query( "select * from fotos" )->fetchAll();
    if ( $db->rows >= 1 )
    {
        $album_name = $db->data[0]['album_name'];

        echo "<h1>" .  $album_name  . "</h1>\n";
        echo "<a href=\"album.php\" class=\"back\"><img src=\"images/left.png\"/> Voltar</a>";

I need to select to put the album name on my site, but I have this problem

  • 1

    Below the $db->query( "select * from fotos" )->fetchAll(); but before IF puts a print_r($db); and sends the result.

  • mysql Object ( [query] => select * from photos [data] => Array ( [0] => Array ( [foto_id] => 4 [foto_url] => test.jpg [foto_caption] => [foto_data] => 2017-02-07 00:00:00 [foto_album] => 1 [foto_pos] => 0 [foto_info] => ) [1] => Array ( [foto_id] => 5 [foto_url] => test2.jpg [foto_caption] => [foto_data] => 2017-02-07 00:00:00 [foto_album] => 1 [foto_pos] => 1 [foto_info] => ) [result] => Resource id #8 [Rows] => 2 [config:protected] => Array ( [host] => localhost [port] => 3306 [dbname] => photoupload [user] => root [password] => ) [host:protected] => localhost [port:protected] =>

  • 3306 [user:protected] => root [pass:protected] => [dbname:protected] => photoupload [con:protected] => Resource id #6 )

  • As you can see inside your object there is no "album_name", you are sure that the table is Photos?

  • is another table the album, just make another select inside this for $album_name ?

  • I’ll put an answer down here to get it written better

Show 1 more comment

3 answers

2


PHP is stating that the index album_name does not exist, give a var_dump($db->data[0]) and see if there are any fields with the name album_name.

If you’re on an album chart, just try a JOIN:

$db->query("select * from fotos, albuns WHERE fotos.foto_album=albuns.album_id")->fetchAll();
  • No, because the select is in the photos, result : array(7) { ["foto_id"]=> string(1) "4" ["foto_url"]=> string(8) "test.jpg" ["foto_caption"]=> string(0) "" ["foto_data"]=> string(19) "2017-02-07 00:00:00:00" ["foto_album"]=> string(1) "1" ["foto_pos"]=> string(1) "0" ["foto_info"]=> string(0) "" }

  • just make another select right ?

  • where the field is album_name? if it is in an album table, show the fields that may be possible to make an SQL by bringing the information from the two tables.

  • album_name is in albuns table, only make a Join in select then ?

  • that, make a Join and return the information from the two tables

  • Okay, I’ll do it and I’ll put it right

  • Straight, thank you very much, it worked !

Show 2 more comments

1

Do a JOIN as follows:

$db->query("select a.*, b.album_name AS album_name from fotos a INNER JOIN tabela_com_album_name b ON a.foto_album = b.id_do_album")->fetchAll();
  • I tested it here and it worked Grateful ! was it ! thank you

0

Are you sure album_name is the correct name of the column in your database? As already said, from a var_dump or print_r to see exactly what is coming from your query.

  • is not, I need to make another query for another table, just make another select ?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.