SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Evangelist jazztie's Avatar
    Join Date
    Mar 2001
    Location
    the Netherlands
    Posts
    519
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have the following code:

    PHP Code:
        $sql     "SELECT * FROM rubrieken";
        
    $result mysql_query($sql);
        
    $page     mysql_fetch_array ($result);
        
    $rubr     split(","$page["rubrieken"]); 

        for(
    $i=0;$i<count($rubr);$i++) {
             
    $titel mysql_result(mysql("$dbname","SELECT titel FROM rubrieken WHERE id=$rubr[$i]"),0,0); 
             
    $rubid mysql_result(mysql("$dbname","SELECT id FROM rubrieken WHERE id=$rubr[$i]"),0,0);
             print 
    "Rubriek: <B>$titel, id: $rubid</B><br>";
        } 
    But when I execute this script I get an error.
    It reads:
    Warning: Supplied argument is not a valid MySQL result resource in C:\...\add.php on line 36

    and the same on line 37.

    Line 36 is the '$titel = mysql_result..... ' statement
    Line 37 is the '$rubid = .......' statement

    What am I doing wrong?

  2. #2
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    mysql_result(mysql(" -- inside mysql_result, shouldn't you have mysql_query? I don't think mysql() is a function.

  3. #3
    SitePoint Evangelist jazztie's Avatar
    Join Date
    Mar 2001
    Location
    the Netherlands
    Posts
    519
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've changed the code to:

    PHP Code:
        $sql     "SELECT * FROM rubrieken";
        
    $result mysql_query($sql);
        
    $page     mysql_fetch_array ($result);
        
    $rubr     split(","$page["rubrieken"]); 

        for(
    $i=0;$i<count($rubr);$i++) {
             
    $sql01     "SELECT * FROM rubrieken WHERE id = $rubr[$i]";
             
    $res01     mysql_query($sql);
             
    $rubid mysql_result($result1,0,'id');
             
    $titel mysql_result($result1,0,'titel');
             print 
    "Rubriek: <B>$titel, id: $rubid</B><br>";
        } 
    But I still get the same errors...
    Last edited by jazztie; May 18, 2001 at 07:38.

  4. #4
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why is the mysql_result() function including "$result1"? I don't see that variable in the script.

  5. #5
    SitePoint Evangelist jazztie's Avatar
    Join Date
    Mar 2001
    Location
    the Netherlands
    Posts
    519
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's part of the problem... thanx...
    but I don't get the, at least, 30 'rubrieken' on the screen.

    Just the first one.

  6. #6
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by jazztie
    I've changed the code to:

    PHP Code:
       $sql    "SELECT * FROM rubrieken";
            
    $result mysql_query($sql);
            
    $page   mysql_fetch_array ($result);
            
    $rubr   split(","$page["rubrieken"]); 
    I'm starting to develop an intimate knowledge of your database - and that's scary! don't you mean that SQL statement to be
    PHP Code:
       $sql    "SELECT * FROM pages"
    If I am correct, I don't understand why in your first sql query you select all the rows from the table, but then only extract the first row in the result set?

  7. #7
    SitePoint Evangelist jazztie's Avatar
    Join Date
    Mar 2001
    Location
    the Netherlands
    Posts
    519
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well the thing is...

    I want to post all the 'rubrieken' available in a list/menu...
    so I need to get all the rubrieken available from the table rubrieken... not the table pages...

    'Pages' can contain more than one 'rubrieken' (id) (rubrieken is plural for rubriek)... so, the main goal is to select exsisting rubrieken to be added to the pages.rubrieken table...

    What I'm trying to do with the code is just to print all the rubrieken on a page... the step towards putting them in a list/menu is pretty small... even I can do that..

    But with the following code i still get errors... I don't understand why:

    PHP:--------------------------------------------------------------------------------
    $sql = "SELECT * FROM rubrieken";
    $result = mysql_query($sql);
    $page = mysql_fetch_array ($result);
    $rubr = split(",", $page["rubrieken"]);

    for($i=0;$i<count($rubr);$i++) {
    $sql01 = "SELECT * FROM rubrieken WHERE id = $rubr[$i]";
    $res01 = mysql_query($sql);
    $rubid = mysql_result($result1,0,'id');
    $titel = mysql_result($result1,0,'titel');
    print "Rubriek: <B>$titel, id: $rubid</B><br>";
    }--------------------------------------------------------------------------------

  8. #8
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So what you want is a list of all the Rubrieken. Is that right? That is very easy to code. But, well, here is why I am confused. In earlier posts you have given the structure of table Rebrieken(id, titel). Well, the code you give makes no sense given that data schema. You say you only get one rubriek being printed out. Below I add notes to follow what logically should occur.

    PHP Code:
    $sql*****=*"SELECT***FROM*rubrieken";
    ****$result*=*mysql_query($sql);
    ****$page*****=*mysql_fetch_array*($result);
              
    // $page should be an associateive array with
              // two keys "id" and "titel".
              // Therefor the reference to $page["rubrieken"]
              // is a reference to a uninitialised (empty) variable.
              // Thus, we can expect the array $rubr to be 1 element long
              // Why 1 element? - see my note below.
    ****$rubr*****=*split(",",*$page["rubrieken"]);*
              
    // $rubr is one element long.
              // The for loop, therefor, will be executed once.
    ****for($i=0;$i<count($rubr);$i++)*{
    *********$sql01*****=*"SELECT***FROM*rubrieken*WHERE*id*=*$rubr[$i]";
    *********$res01*****=*mysql_query($sql);
    *********$rubid*=*mysql_result($result1,0,'id');
    *********$titel*=*mysql_result($result1,0,'titel');
    *********print*"Rubriek:*<B>$titel,*id:*$rubid</B><br>";
    ****
    Why is $rubr 1 element long when we passed an empty string to the function split()? Well according to a user comment in the anotated manual this is what happens http://www.php.net/manual/en/function.split.php .

    Assuming that you simply want to list all the rubrieken in the table rubrieken this would suffice:
    PHP Code:
    $sql "SELECT * FROM Rubrieken";
    $result mysql_query($sql);
    while ( 
    $row mysql_fetch_array($result) {
       
    // function extract will take the key/value pairs in 
       // array $row and place them in their own variables
       // being $titel and $id
       
    extract($row);
       
    print*"Rubriek:*<B>$titel,*id:*$id</B><br>";



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •