SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2003
    Location
    Ontario, Canada
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    fetch object problems

    I've never run into this problem before, but for some reason my script is no longer working for me. I've narrowed the problem down to the fetch object in the code below. I threw a echo before the while to see if it would come up and it did, so I figure it has to be something to do with the while.

    PHP Code:
    <?php $query = @mysql_query("SELECT * FROM aci-config ORDER BY id");
    while (
    $data = @mysql_fetch_object($query)) {
    ?>
    !
    <br>vpdn-group <?PHP echo $data->vpdn?>
    <br> accept-dialin
    <br>  protocol l2tp
    <br>  virtual-template 1
    <br> terminate-from hostname <?PHP echo $data->hname?>
    <br> local name <?PHP echo $data->lname?>
    <br> lcp renegotiation always
    <br> l2tp tunnel password 
    <?php
    }
    ?>
    Any help to solve this problem would be much appreciated.

  2. #2
    SitePoint Enthusiast
    Join Date
    Apr 2003
    Location
    Ontario, Canada
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh yes, I forgot to mention my table setup:

    table name: aci-config
    column1: id
    column2: vpdn
    column3: hname
    column4: lname

  3. #3
    SitePoint Enthusiast
    Join Date
    Jul 2003
    Location
    Norway
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    maybe if you remove the @ in front of the fetch, the error may show up in the log

  4. #4
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,609
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Not sure if mySQL likes hyphens in fieldnames. What do you get if you do this?
    PHP Code:
    $query mysql_query"SELECT * FROM aci-config ORDER BY id" ) or die ( mysql_error() ); 
    Ian Anderson
    www.siteguru.co.uk

  5. #5
    public static void brain Gybbyl's Avatar
    Join Date
    Jun 2002
    Location
    Montana, USA
    Posts
    647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Like Silan has pointed out, it's never a good idea to have '@'s while you are debugging code. I don't know how many people post here a day when all of their problems could have been fixed by taking off the @. I don't that it will show up in the log, but it will definitely show up on the screen -- Are you sure that you are indeed connected to the MySQL server? Have you selected your database? Do your tables exist? Is there a syntax error on your query? You will never know until you get rid of our little friend '@'.

    All the @ does is supress error messages, and imo it should never be used on anything related to a database (it should never be used at all now that we have exceptions in php 5), and i've only ever put it to good use when i was using unlink() and chmod() on files that weren't necessarily in existence.

    Also, instead of doing this:

    PHP Code:
    <?PHP echo $data->var?>
    I find it easier to simple go:

    PHP Code:
    <?=$data->var?>
    Saves tons of time, and it's been supported for god knows how long.
    Ryan

  6. #6
    SitePoint Addict Viral's Avatar
    Join Date
    Nov 2001
    Location
    Washington DC
    Posts
    294
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have a related question. Why do people use mysql_fetch_object()? I've never understood what purpose it serves. It's as slow as mysql_fetch_array with half the benefits.

    For individual results, I always use:
    Code:
    $var = mysql_result(mysql_query("select item from table limit 1"),0,0);
    and for multiple results, I use
    Code:
    $result = mysql_query("select * from table");
    while (list($item1, $item2, $item3) = mysql_fetch_row($result)) {
      print "$item1, $item2, $item3<br />";
    }
    Nothing is faster than using mysql_fetch_row, so why use _object? On a few occassions (like when I have lots of fields to pull in) I'll use this
    Code:
    $result = mysql_query("select * from table");
    while ($row=mysql_fetch_assoc($result)) {
      extract($row);
      // do whatever
    }
    Can someone give me an explanation as to when the mysql_fetch_object would be a better solution, worth the extra processing time?

    Thanks,
    --Viral
    A computer without Windows is like a chocolate cake without mustard.

  7. #7
    ********* wombat firepages's Avatar
    Join Date
    Jul 2000
    Location
    Perth Australia
    Posts
    1,717
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    simply coding style / preference

    Quote Originally Posted by PHP Manual
    mysql_fetch_object::
    Speed-wise, the function is identical to mysql_fetch_array(), and almost as quick as mysql_fetch_row() (the difference is insignificant).

  8. #8
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Another reason would be if the person had coded everything in PHP OO style, and so it would be more consistent.

  9. #9
    public static void brain Gybbyl's Avatar
    Join Date
    Jun 2002
    Location
    Montana, USA
    Posts
    647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I find that arrays (although powerful) are harder to keep tallies on in my mind -- With an object, I can keep track of the members easier. This isn't much different than using an associate array, but in the end, like Redemption pointed out: It's all about consistency.

    You wouldn't fetch an array in an oop, it's just not kosher.

    Since mysql_fetch_object() just populates an instance of stdClass with members, it's not different than using an associate array anyway, except that you access the data members with a -> little arrow instead of a $var['poo'] style.

    Plus, everyone always seems to be worried about overhead. If people want to use your script, they are going to wait the 2 extra microseconds that it takes to get the data. If they don't want to wait, then that's their problem, because for every time you can use the syntax that is native to you (as a programmer) in your code (even though it may slow you down a little), then the time you've saved is doubled back into the production of extending and better code for even that same program.

    Long winded. Over.
    Ryan

  10. #10
    SitePoint Enthusiast
    Join Date
    Apr 2003
    Location
    Ontario, Canada
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by siteguru
    Not sure if mySQL likes hyphens in fieldnames. What do you get if you do this?
    PHP Code:
    $query mysql_query"SELECT * FROM aci-config ORDER BY id" ) or die ( mysql_error() ); 
    The problem does no lie in that line of code, I removed the @ prior to posting this thread to see where the problem resided. Which is why I know it's in the fetch object line as I receive the following error:
    Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in /home/xarnet/public_html/aci-config.php on line 60
    When I use the following:
    PHP Code:
    <?php
    $query 
    mysql_query("SELECT * FROM aci-config WHERE sheetid= '1' ORDER BY id");
    while (
    $data mysql_fetch_object($query)) {
    ?>
    !
    <br>vpdn-group <?=$data->vpdn?>
    <br> accept-dialin
    <br>  protocol l2tp
    <br>  virtual-template 1
    <br> terminate-from hostname <?=$data->hname?>
    <br> local name <?=$data->lname?>
    <br> lcp renegotiation always
    <br> l2tp tunnel password 
    <?php
    }
    ?>
    With regards to it being a problem with connecting to the database, I do not believe it is as I am using the following:
    PHP Code:
    <?php
    $db 
    mysql_connect("localhost""user""password");
    if (!
    $db) { 
         echo( 
    "<p>Unable to connect to the " 
               
    "database server at this time.</p>" ); 
         exit(); 

    mysql_select_db("database_name",$db);
    if (! @
    mysql_select_db("database_name") ) { 
         echo( 
    "<p>Unable to locate the " 
               
    "database at this time.</p >" ); 
         exit(); 
    }
    ?>
    And I do not run into any of the echo text, which I assume means that there are no problems on that front.

  11. #11
    SitePoint Enthusiast
    Join Date
    Jul 2003
    Location
    Norway
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in /home/xarnet/public_html/aci-config.php on line 60
    Are you sure that the query returns any rows? maybe if you checked the number of rows returned:

    PHP Code:
    echo mysql_num_rows($query); 

  12. #12
    SitePoint Enthusiast
    Join Date
    Apr 2003
    Location
    Ontario, Canada
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It seems the problem was in fact to do with the hiphen in the table name, I changed it to aciconfig and it worked fine. So thanks siteguru for the suggestion, I didn't originally think that was the problem as no error was returned, but ultimately that was the problem. Anyway thanks to everyone for the help I appreciate it enormously.

  13. #13
    SitePoint Addict been's Avatar
    Join Date
    May 2002
    Location
    Gent, Belgium
    Posts
    284
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'd change
    PHP Code:
    mysql_select_db("database_name",$db);
    if (! @
    mysql_select_db("database_name" ) ) { ... } 
    into
    PHP Code:
    if (! @mysql_select_db("database_name"$db)) { ... } 
    no need to select the database twice (or am I missing something?)
    Per
    Everything
    works on a PowerPoint slide


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
  •