Silly Database error

Discussion in 'PHP' started by denyo, Mar 16, 2009.

  1. #1
    Hello i am getting this database error Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in /home/www/user/domain.com/www/components/com_component/component.php on line 334

    Line 334

    //echo $database->getQuery();
    $result1 = $database->query();
    while ($row1=mysql_fetch_object($result1)

    Is there a error return or if false command i can enter in order to avoid this error ?


    Thanks in advance
     
    denyo, Mar 16, 2009 IP
  2. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #2
    $row_count = mysql_num_rows ($result1);
    if($row_count <1) { echo "no rows returned"; }

    something like if($result) won't return what you want.
     
    shallowink, Mar 16, 2009 IP
  3. denyo

    denyo Well-Known Member

    Messages:
    142
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    123
    #3
    where do i have to put this piece of code ?
     
    denyo, Mar 16, 2009 IP
  4. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #4
    $result1 = $database->query();

    *here looks good* make it an IF() ELSE to encompass the WHILE.

    while ($row1=mysql_fetch_object($result1)
     
    shallowink, Mar 16, 2009 IP
  5. denyo

    denyo Well-Known Member

    Messages:
    142
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    123
    #5
    //echo $database->getQuery();
    $result1 = $database->query();
    $row_count = mysql_num_rows ($result1);
    if($row_count <1) { echo "no rows returned"; }
    while ($row1=mysql_fetch_object($result1))

    {


    I know i messed it up ! Now i get two errors :p
     
    denyo, Mar 16, 2009 IP
  6. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #6
    $result1 = $database->query();
    $row_count = mysql_num_rows ($result1);

    if($row_count <1) { echo "no rows returned"; }
    else {
    while ($row1=mysql_fetch_object($result1)) {
    //put code here to display results
    }
    }
    try that. tried to line up the braces since that was most likely your error.
     
    shallowink, Mar 16, 2009 IP
  7. denyo

    denyo Well-Known Member

    Messages:
    142
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    123
    #7
    $result1 = $database->query();
    while ($row1=mysql_fetch_object($result1))
    if($row_count <1) { echo "no rows returned"; }
    else {
    while ($row1=mysql_fetch_object($result1)) {
    //put code here to display results
    }
    }

    Sorry again, error is still appearing . what do i have to enter here ? //put code here to display results
     
    denyo, Mar 16, 2009 IP
  8. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #8
    um no. if that's the code you put, its wrong the 2nd line isn't part of the code I posted:

    $result1 = $database->query();
    if($row_count <1) { echo "no rows returned"; }
    else {
    while ($row1=mysql_fetch_object($result1)) {
    //put code here to display results
    }
    }


    if you use a WHILE statement it has to have the { }
    the code you want to run goes inside of those.

    //put code here to display results
    means to insert the correct PHP to display the results something like
    extract($row1) ;

    but then you have to know the names of the fields. (if you have a table with a field of products_id you would access it via $products_id )

    I'd highly recommend heading over to w3schools.com or tizag.com for their PHP MySQL tutorials. Most of this is covered (lightly but covered).
     
    shallowink, Mar 16, 2009 IP
  9. denyo

    denyo Well-Known Member

    Messages:
    142
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    123
    #9
    Thank you, you re great ! Now everything works like a charm ! Also you helped me understanding how things work and what was causing the problem !

    i will give a try to the links you gave me
     
    denyo, Mar 16, 2009 IP