WEB PHP Folk v.help

Discussion in 'OT Technology' started by 420am, Oct 11, 2007.

  1. 420am

    420am OT Supporter

    Joined:
    Mar 10, 2003
    Messages:
    10,936
    Likes Received:
    0
    Location:
    Madison, Wisconsin
    Yo, php folk. I'm at a loss here. I've been meaning to learn php but just haven't found the time so here I am. The goal of this piece of code is to grab a field out of $table then use that result in the WHERE listid for query_name. Basically this retrieves the current playlist id and then take that info and get the name of the playlist. The info just happens to be in differant tables. I've tried it a couple of different ways. But, I could never get $pl_num to be global so I could use in in $query_name. This doesn't work at all. I'm just including it to help give you an idea of what I'm trying to do. It spits out this:


    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in X.php on line 19

    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in X.php on line 28





    <?php

    $db_name="X";// Database name
    $connection=mysql_connect("localhost","X","X") or die("I Couldn't connect");
    $db=mysql_select_db($db_name,$connection) or die("I Couldn't select your database");
    $table="table"; // Table name
    $table2="table2"; // Table2 name
    $field="field"; // Field name

    make_global();

    function make_global()
    {
    global $pl_num;
    $pl_num++;
    $query_id = "SELECT * FROM $table";
    $pl_num = $row['playlistid'];

    while ($row = mysql_fetch_array($result_id))
    {
    echo $row['playlistid'];
    echo "<br><br>";
    }

    $query_name = "SELECT * FROM $table2 WHERE listid = '$pl_num'";
    $result_name = mysql_query($query_name);

    while($row = mysql_fetch_array($result_name))
    {
    echo $row['name'];
    echo "<br><br>";
    echo $row['playlistid'];
    }

    }


    mysql_close($connection);

    ?>
     
  2. RaginBajin

    RaginBajin Have you punched a donkey today?

    Joined:
    Dec 24, 2001
    Messages:
    8,740
    Likes Received:
    0
    Location:
    NoVA
    Looks like your table does not have the playlistid in it at all.

    That's why it is puking on that.
     
  3. Slid.

    Slid. I'm a guy.

    Joined:
    Oct 25, 2001
    Messages:
    1,928
    Likes Received:
    0
    Location:
    NH
    You're not executing the first query.. execute is in bold:

    Code:
    function make_global()
    {
    global $pl_num;
    $pl_num++;
    $query_id = "SELECT * FROM $table";
    [b]$result_id = mysql_query($query_id);[/b]
    
    while ($row = mysql_fetch_array($result_id))
    {
    [b]$pl_num = $row['playlistid'];[/b]
    echo $row['playlistid'];
    echo "<br><br>";
    }
    
    $query_name = "SELECT * FROM $table2 WHERE listid = '$pl_num'";
    $result_name = mysql_query($query_name);
    
    while($row = mysql_fetch_array($result_name))
    {
    echo $row['name'];
    echo "<br><br>";
    echo $row['playlistid'];
    }
    
    }
    
    $pl_num = $row['playlistid']; is out of place also, fixed and bolded.
     
  4. Slid.

    Slid. I'm a guy.

    Joined:
    Oct 25, 2001
    Messages:
    1,928
    Likes Received:
    0
    Location:
    NH
    Not to mention, as it is (not having the queries nested) you're only going to get "* FROM table2" for the LAST playlist in table.
     
  5. 420am

    420am OT Supporter

    Joined:
    Mar 10, 2003
    Messages:
    10,936
    Likes Received:
    0
    Location:
    Madison, Wisconsin
    It does though.
     
  6. 420am

    420am OT Supporter

    Joined:
    Mar 10, 2003
    Messages:
    10,936
    Likes Received:
    0
    Location:
    Madison, Wisconsin
    Yeah, nesting appears to be my downfall. :hsd:
     
  7. 420am

    420am OT Supporter

    Joined:
    Mar 10, 2003
    Messages:
    10,936
    Likes Received:
    0
    Location:
    Madison, Wisconsin
    tried this too. still doesn't work. It's not liking the line: $name_query = "SELECT * FROM tbl_playlist WHERE listid ='8'";

    The result should be like 'Main Playlist'.

    Any suggestions?

    Code:
    <?php
    
    $db_name="xx";
    $connection=mysql_connect("localhost","xx","xx") or die("I Couldn't connect");
    $db=mysql_select_db($db_name,$connection) or die("I Couldn't select your database");
    
    //Select playlist name where listid = 8
    $name_query = "SELECT * FROM tbl_playlist WHERE listid ='8'";
    
    $result_name = mysql_query($name_query);
    if (!$result_name) error_message(mysql_error());
    
    //Select current playlist id
    $number_query = "SELECT * FROM tbl_iceradio";
    $result_number = mysql_query($number_query);
    if (!$result_number) error_message(mysql_error());
    
    echo "<TABLE width='35%' border='1' align='left'>";
    while ($name_data = mysql_fetch_assoc($result_name)) {
    
    echo $name_data[playlistid];
    echo $number_data[name];
    
    }
    
    mysql_close ($connection);
    
    ?>
    
     
    Last edited: Oct 12, 2007
  8. Slid.

    Slid. I'm a guy.

    Joined:
    Oct 25, 2001
    Messages:
    1,928
    Likes Received:
    0
    Location:
    NH
    When I have trouble with queries I take a few steps back. There's nothing wrong with your PHP or mySQL code except that you may be referencing table names incorrectly or trying to access a listid that doesn't exist.

    Try:

    Code:
    $result = mysql_query("SELECT * FROM tbl_playlist");
    while ($row = mysql_fetch_assoc($result)) {
      print $row['listid'] . '<br>';
    }
    
    That should spit out each "listid" that's in the database.. my guess is that there is no 8. I'd recommend that if you don't already have it you install phpMyAdmin.. it will make things like this stand out and be easy to fix.
     
  9. 420am

    420am OT Supporter

    Joined:
    Mar 10, 2003
    Messages:
    10,936
    Likes Received:
    0
    Location:
    Madison, Wisconsin
    I ran the querys in phpmyadmin and they both worked. The problem appears to be that it doesn't like doing 2 queries. One of the queries needs to be a function I think. :hsd:
     
  10. 420am

    420am OT Supporter

    Joined:
    Mar 10, 2003
    Messages:
    10,936
    Likes Received:
    0
    Location:
    Madison, Wisconsin
    Got it!

    Code:
    <?php
    
    $db_name="dbname";
    $connection=mysql_connect("localhost","dbuser","dbpass") or die("I Couldn't connect");
    $db=mysql_select_db($db_name,$connection) or die("I Couldn't select your database");
    
    function get_playid()
    { 
    
    //Get the playlistid of the current playlist
    $number_query = "SELECT * FROM tbl_1";
    
    $result_number = mysql_query($number_query);
    
    if (!$result_number) error_message(mysql_error());
    
     while ($number_data = mysql_fetch_assoc($result_number))
      {
       //Get the name of the current playlist using the playlistid we got from the number_query
       $name_query = "SELECT * FROM tbl_2 WHERE listid ='$number_data[playlistid]'";
    
       $result_name = mysql_query($name_query);
    
       if (!$result_name) error_message(mysql_error());
    
         while ($name_data = mysql_fetch_assoc($result_name))
          {
           echo $name_data[name]; //great success
          }
      }
    
    }
    
    get_playid();
    
    mysql_close ($connection);
    
    ?>
    
     

Share This Page