SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Thread: wierd one

  1. #1
    SitePoint Guru
    Join Date
    Apr 2001
    Location
    BC, Canada
    Posts
    630
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    wierd one

    this one is making me really scratch my head
    Im querying the database with a normal a select statement.. it should be pulling out a lot of info... but it just returns 5???
    this is the code:

    PHP Code:
    $resultmysql_query("SELECT * FROM articles, lookup, writers WHERE lookup.aid = articles.aid AND lookup.wid = writers.wid ORDER BY published DESC LIMIT $offset,1")
            or die(
    "Error retrieving article".mysql_error()); 
    normaly I would then do something like
    PHP Code:
    $row mysql_fetch_array($result); 
    But then I get an error saying that $result is a scalar?
    So I then tried jesut echoing $result .. and it printed 5

    anyone know whats going on??
    thanx

  2. #2
    SitePoint Guru
    Join Date
    Apr 2001
    Location
    BC, Canada
    Posts
    630
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I ran it in a mysql shell (telnet) and it worked fine, I just took out the variable $offset and replaced it with a number..worked like a charm..

    I took the limit statement out in the php.. and it works now, but I need it in there because Its being used in a "previous" link thingy.

  3. #3
    SitePoint Guru
    Join Date
    Apr 2001
    Location
    BC, Canada
    Posts
    630
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    tried using mysql_affected_rows to see if it is even really getting anything from hte database.. I get this error

    Warning: 5 is not a MySQL link index in article.php3 on line 45

  4. #4
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I can't solve your first problem, but with the call to mysql_affected_rows don't pass it the result set identifier($result) as the arguement. It expects a database link identifier. Just call it without passing any parameters and it will use the last used database by default. eg;

    $affectedRows = mysql_affected_rows();

  5. #5
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    freakysid and slighltywacked mysql_affected_rows() is only for returning the number of affected rows from an UPDATE DELETE or INSERT statement. use mysql_num_rows() for a SELECT statement.

    Sounds like your problem is that $offset is getting reset to 0 somehow in your script where, do you assign $offset a value in your script? Can you show us the code? Also your limit says only return one row starting at $offset. LIMIT works like this

    LIMIT 0, 20 //returns the first 20 rows

    LIMIT 20, 20 //returns rows 20 through 40.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  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)
    Doh, yeah!

    But having $offset == 0 would not cause a problem as your example shows. But having $offset not set would be a problem because then the sql query would look like this:

    ... LIMIT*,1

    That is why I always write my code like the example below (which I think is from following your advice freddy):

    $sql = "SELECT * FROM Table WHERE foo='$bar'";
    $result = mysql_query($sql);
    echo "<br>$sql<br>"; //for debugging only
    Last edited by freakysid; Jun 27, 2001 at 13:14.

  7. #7
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Funny you should mention that, since I just(I mean like 5 seconds ago) found a major bug in a project I am working on, by printing the SQL statement to the screen.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  8. #8
    SitePoint Guru
    Join Date
    Apr 2001
    Location
    BC, Canada
    Posts
    630
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    thanx guys... I solved the problem by doing it the long way... using loops..

    but for arguments sake..in that statement..yes... $offset was defined from a querystring.. and if it wasnt in the query string, it was created with a value of 0

    also "$offset,1" is what I wanted because I only wanted one row of information..
    This is being used in a article type application.. and thisis being used when the user clicks hte "previous" link


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
  •