SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Feb 2003
    Location
    Fullerton
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy Resource id #2?????

    I have an Apache server on a Windows 2000 computer with MySQL and PHP 4.0.

    I frequently have trouble pulling information from my database and either end up getting a result of 'Resource ID #2' or 'Supplied argument is not a valid MySQL result resource' when I use mysql_fetch_array or result instead of just mysql_query.

    What's is this??

    Here's the code from my the page I'm having trouble with....

    <?php

    mysql_pconnect("localhost", "nobody", "") or die ("could not connect to database");
    mysql_select_db("book") or die ("could not select database");


    $check1="select title from BOOK where title='$title' ";
    $check2=mysql_fetch_array($check1);


    print $check2;



    while ($row=mysql_fetch_array($check2) )
    {

    $checking = $row["title"];
    if ($checking == $title)
    {
    print $title;
    print "<p>There is already an article by with that title in the database. Please enter the article again using a different title</P>";

    ?>
    <a href="administration.html">Click here</a>
    <?php

    }
    else
    {
    $query="insert into book "
    ." (day, month, year, zip, category, title, document) values "
    ."('$day', '$month', '$year', '$zip', '$category', '$title', '$document')"
    ;

    mysql_query($query);
    include ("infopanel.php");
    }
    }

    ?>

  2. #2
    SitePoint Zealot
    Join Date
    Mar 2002
    Location
    New York, NY
    Posts
    170
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    im no pro but i think all you have to do is this:
    PHP Code:
    <?php 
     
    mysql_pconnect
    ("localhost""nobody""" ) or die ("could not connect to database" ); 
    mysql_select_db("book" ) or die ("could not select database" ); 
     
    //change is here 
    $check1=mysql_query("select title from BOOK where title='$title' " ); 
     
    $check2=mysql_fetch_array($check1); 
     
     
    print 
    $check2
     
     
     
        while (
    $row=mysql_fetch_array($check2) ) 
        { 
     
        
    $checking $row["title"]; 
        if (
    $checking == $title
            { 
            print 
    $title
            print 
    "<p>There is already an article by with that title in the database. Please     enter the article again using a different title</P>"
     
            
    ?> 
            <a href="administration.html">Click here</a> 
            <?php 
     
            

            else 
            { 
            
    $query="insert into book " 
            
    ." (day, month, year, zip, category, title, document) values " 
            
    ."('$day', '$month', '$year', '$zip', '$category', '$title', '$document')" 
            

     
            
    mysql_query($query); 
            include (
    "infopanel.php" ); 
            } 
        } 
     
    ?>
    hope it helps

    zing

    ps.. this should probably be in the PHP section.. just so you know for the future
    Last edited by zing; Feb 24, 2003 at 19:44.

  3. #3
    ********* Wizard silver trophy Cam's Avatar
    Join Date
    Aug 2002
    Location
    Burpengary, Australia
    Posts
    4,495
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    Here is a fixed version of your code
    PHP Code:
    <?php 
    $db 
    mysql_connect("localhost""nobody""" ) or die ("could not connect to database" ); 
    mysql_select_db("book" ) or die ("could not select database" ); 
    //change is here 
    $check1 mysql_query("SELECT title
            FROM BOOK
            WHERE title='
    $title'" ); 
        while (
    $row mysql_fetch_array($check1) ) 
        { 
        
    $checking $row["title"]; 
        if (
    $checking == $title
            { 
            print 
    $title
            print 
    "<p>There is already an article by with that title in the database. Please     enter the article again using a different title</P>"
            
    ?> 
            <a href="administration.html">Click here</a> 
            <?php 
            

            else 
            { 
            
    $query="INSERT INTO book (day, month, year, zip, category, title, document)
        VALUES ('
    $day', '$month', '$year', '$zip', '$category', '$title', '$document')"
            
    $result mysql_query($query); 
    if ( !
    $result ) die('Error');
            include (
    "infopanel.php" ); 
            } 
        } 
    ?>
    There are a number of issues with your code -
    1. Firstly, you used mysql_pconnect(), unless there is a specific reason you want a persistant connection to the DB, you should use mysql_connect().
    2. Secondly, if you want to query the database, you need to actually send the query, not just type it out. This is done using the mysql_query() function. You can read up on exactly how to use MySQL with PHP here.
    3. Thirdly, you should try to structure your SQL better, it makes it easier to read and to edit, I re-typed your SQL queries in the correct format.
    4. Fourthly,
      PHP Code:
      mysql_query($query); 
      mysql_query() returns a Resource ID number and you need to save that to a variable to be able to access the data or check that the query was successful. I fixed that for you as well.

  4. #4
    SitePoint Member
    Join Date
    Feb 2003
    Location
    Fullerton
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Many thanks for the responce - unfortunatly both of these didn't seem to work -

    The first suggestion gives me the invalid sql error again and the second by DJ P@CKMAN results with a blank screen - nothing entered into the database. When I add a couple of lines to basically print the results of $check1 I get 'Resource id #2' - Any answers???

  5. #5
    No. Phil.Roberts's Avatar
    Join Date
    May 2001
    Location
    Nottingham, UK
    Posts
    1,142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You cant print the contents of $check1 because all it contains is a database resource pointer.

    You need to run a query and pass the query result to mysql_fetch_array() before you can print anything.

    PHP Code:
    <?php 

    $db 
    mysql_connect("localhost""nobody""" ) or die ("could not connect to database" ); 
    mysql_select_db("book" ) or die ("could not select database" ); 

    $check1 mysql_query("SELECT title FROM BOOK WHERE title='$title'"$db); 
        
    while (
    $row mysql_fetch_array($check1) ) 

        if (
    $row['title'] == $title
        { 
            print 
    $title
            print 
    "<p>There is already an article by with that title in the database. " 
                  
    "Please enter the article again using a different title</p>"
            print 
    '<a href="administration.html">Click here</a>';
        } 
        else 
        { 
            
    $query "INSERT INTO book (day, month, year, zip, category, title, document) " .
                     
    "VALUES ('$day', '$month', '$year', '$zip', '$category', '$title', '$document')"
            
            
    $result mysql_query($query$db);

            if (!
    $result)
            {
                die(
    'Error: Query failed');
            }
            else
            {
                include(
    "./infopanel.php" ); 
            }
        } 

    ?>
    THE INSTRUCTIONS BELOW ARE OLD AND MAY BE INACCURATE.
    THIS INSTALL METHOD IS NOT RECOMMENDED, IT MAY RUN
    OVER YOUR DOG. <-- MediaWiki installation guide

  6. #6
    SitePoint Member
    Join Date
    Feb 2003
    Location
    Fullerton
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for that - I just get a blank screen when the site jumps to this page.

    $check1 = mysql_query("SELECT title FROM BOOK WHERE title='$title'", $db);

    this line can't returning a result... and I checked to make sure that '$title' contains a result from the form on the previous page...

  7. #7
    SitePoint Member
    Join Date
    Feb 2003
    Location
    Fullerton
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not to worry - I sorted it! Took a different approach...

    // first part jumps to the routine that connects to my database

    <?php include("dbconnect.php"); ?>

    //Then we find out if $legaltitle matches anything in the title colum in the database

    <?php

    $query="select count(*) from book where
    title = '$legaltitle' ";

    $result = mysql_query( $query );

    // if there is a problem, then say so!

    if (!$result)
    {
    print "Cannot run Query.";
    exit;
    }

    // if there isn't - let $count store a value based on anything found

    $count = mysql_result($result, 0, 0);

    // If so then say so and return the user to re-enter the title of the document they were trying to post.

    if ($count > 0 )

    {
    print $legaltitle;
    print "<p>There is already an article with that title in the database. " .
    "Please enter the article again using a different title</p>";
    print '<a href="administration.html">Click here</a>';
    }

    // If that title does not exist then enter it into the database.

    else
    {
    $query = "INSERT INTO book (day, month, year, zip, category, title, document) " .
    "VALUES ('$day', '$month', '$year', '$zip', '$category', '$legaltitle', '$document')";

    $result = mysql_query($query);

    if (!$result)
    {
    die('Error: Query failed');
    }
    else
    {
    include("infopanel.php" );
    }
    }


    ?>

    Maybe this will help someone else.... Thanks for the great feed back though.


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
  •