SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Member
    Join Date
    Jul 2006
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Resource id #3 error (was "Who can help with this one?")

    Hi,

    I'm a beginner to PHP.The following script is based on a book by Kevin Yank. I've tried to modify it a bit,though unfortunately unsuccessful:
    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Jokes CMS</title>
        <meta http-equiv="content-type" 
            content="text/html; charset="iso-8859-1">
    </head>
    <body>

        <?php if(!isset($_GET['antwort'])) : ?>
        
        <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="get">
            <input type="hidden" name="id" value="<?php echo $_GET['id'];?>"/>
            <label>Wollen Sie die Kategorie 
            
            <?php 
            
            
    // mit Datenbankserver verbinden
            
            
    $dbcnx = @mysql_connect('localhost','root','Nimbin');
            if(!
    $dbcnx)
            {
                exit(
    '<p>Verbindung zum Datenbankserver'.
                         
    'derzeit nicht möglich</p>');
            }
            
            
    // Datenbank auswählen
            
            
    if(!@mysql_select_db('jokes',$dbcnx))
            {
                exit(
    '<p>Witzedatenbank ist derzeit nicht erreichbar.</p>');
            }
            
            
    $id $_GET['id'];
            
    $name mysql_query("SELECT Name FROM categories WHERE ID = '$id'");
            
            echo 
    $name;?> wirklich löschen ?</label><br />
                
                <input type="submit" name ="antwort" value="Ja" />
          </form>
        
        
        <?php else : 
            
    // mit Datenbankserver verbinden
            
            
    $dbcnx = @mysql_connect('localhost','root','Nimbin');
            if(!
    $dbcnx)
            {
                exit(
    '<p>Verbindung zum Datenbankserver'.
                         
    'derzeit nicht möglich</p>');
            }
            
            
    // Datenbank auswählen
            
            
    if(!@mysql_select_db('jokes',$dbcnx))
            {
                exit(
    '<p>Witzedatenbank ist derzeit nicht erreichbar.</p>');
            }
            
            
            
    $id $_GET['id'];
            
    $sql1 "DELETE FROM categories WHERE ID=$id";
            
    $sql2 "DELETE FROM JokeLookup WHERE CID=$id";
            
    $ok1 = @mysql_query($sql1);
            
    $ok2 = @mysql_query($sql2);
            
            if(
    $ok1 and $ok2)
            {
                echo 
    '<p>Kategorie erfolgreich gelöscht.</p>';
            }
            else
            {
                echo 
    '<p>Fehler beim Löschen der Kategorie: '.
                          
    mysql_error().'</p>';
          }
         
         endif; 
    ?> 
         
        <p><a href="cats.php">Zurück zur Kategorienliste</a></p>
        <p><a href="index.html>Zurück zur Startseite</a></p> 
        
    </body>
    </html>
    I'm having problems with this part of the code:
    PHP Code:
    $id $_GET['id'];
    $name mysql_query("SELECT Name FROM categories WHERE ID = '$id'");
    echo 
    $name
    I was expecting the Browser to output the content of the column Name, but instead I get something like "Resource id #3".
    Can somebody explain why ?
    Thanx for your help !

  2. #2
    SitePoint Guru toasti's Avatar
    Join Date
    Feb 2004
    Location
    Grahamstown
    Posts
    634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you want:
    Code:
     $id = $_GET['id'];
    $result = mysql_query("SELECT Name FROM categories WHERE ID = '$id'");
    $row = mysql_fetch_assoc($result);
    $name = $row['Name'];
    echo $name;
    if you want to echo all the names in the db you would do something like:

    Code:
     $id = $_GET['id'];
    $result = mysql_query("SELECT Name FROM categories WHERE ID = '$id'");
    while($row = mysql_fetch_assoc($result)){
         $name = $row['Name'];
         echo $name."<br/>";
    }
    mysql_query returns a result resource (it runs the query and stores the result in the $result variable (in my code).
    mysql_fetch_assoc, fetchs a row from your result and stores it an associative array ($row)
    Last edited by toasti; Jul 18, 2006 at 06:14. Reason: added an explanation

  3. #3
    SitePoint Member
    Join Date
    Jul 2006
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks toastie, I'll try.
    Thanks also for renaming the thread.
    Next time I'll give more details in the subject field.

  4. #4
    SitePoint Guru toasti's Avatar
    Join Date
    Feb 2004
    Location
    Grahamstown
    Posts
    634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sure.

    cant take the praise for renaming the thread though ...must have been a moderator (i'm not that important )

  5. #5
    SitePoint Member
    Join Date
    Jul 2006
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So what is the difference between mysql_fetch_array() and mysql_fetch_assoc() ?

  6. #6
    SitePoint Member
    Join Date
    Jul 2006
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Both seem to do the same job ? ...

  7. #7
    SitePoint Guru toasti's Avatar
    Join Date
    Feb 2004
    Location
    Grahamstown
    Posts
    634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    php manual:
    mysql_fetch_array - Fetch a result row as an associative array, a numeric array, or both
    mysql_fetch_assoc - Fetch a result row as an associative array
    mysql_fetch_array you can specify the array to return. For example:
    Code:
    while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
       printf("ID: %s  Name: %s", $row[0], $row[1]);  
    }
    
    or
    
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
       printf("ID: %s  Name: %s", $row["id"], $row["name"]);
    }
    
    or
    
    $result = mysql_query("SELECT id, name FROM mytable");
    
    while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
       printf ("ID: %s  Name: %s", $row[0], $row["name"]);
    }

  8. #8
    SitePoint Guru toasti's Avatar
    Join Date
    Feb 2004
    Location
    Grahamstown
    Posts
    634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ...so mysql_fetch_assoc($result) == mysql_fetch_array($result, MYSQL_ASSOC)

  9. #9
    SitePoint Member
    Join Date
    Jul 2006
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Alright, again I became a bit smarter...
    Thanks for that!

  10. #10
    Evil Genius MilchstrabeStern's Avatar
    Join Date
    Nov 2003
    Location
    Arizona
    Posts
    1,131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Also, resources and objects are not meant to be printed/echoed. However, PHP is aware of this and gives each resource/object an ID number. So when you do attempt to display and object or resource, you are presented with "Resource id #3" etc.
    ]


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
  •