SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    Non-Member
    Join Date
    Oct 2008
    Posts
    372
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    2 additional strings showing up for no reason?

    Hey,

    I'm running a loop to pull my information. Here is the index page:
    PHP Code:
      $fields $connection->fetchArray();
      foreach(
    $fields as $field)
      {
        
    $id $field['ID'];
        echo 
    $id;
      } 
    I only have one record in the database, for now. When you run it, it displays:

    1Yy

    It should only be displaying '1'. I don't get where those two strings are coming from?

    Here is the method:
    PHP Code:
      public function fetchArray()
      {
        
    $this->fetchArray mysql_fetch_assoc($this->query);
        if (!
    $this->fetchArray)
          Throw New 
    Exception(mysql_error());
        else
        {
          return 
    $this->fetchArray;
          
          
    $array = array();
          while (
    $this->fetchArray)
          {
            
    $array[] = $this->fetchArray;
          }
          return 
    $array;
        }
      } 

  2. #2
    Use The Cloud
    Join Date
    Jan 2006
    Location
    Boise, ID
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Add some sort of unique separator to determine where the extra characters are coming from.

    PHP Code:
    echo "**$id**"
    And paste the output again.
    Brad Hanson, Web Applications & Scalability Specialist
    ► Is your website outgrowing its current hosting solution?
    ► PM me for a FREE scalability consult!
    ► USA Based: Available by Phone, Skype, AIM, and E-mail.

  3. #3
    Non-Member
    Join Date
    Oct 2008
    Posts
    372
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It echos:

    **1****Y****y**

  4. #4
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    I'm not so sure your fetchArray method would work as expected. I think you mean to do this:

    PHP Code:
    public function fetchArray()
    {
        
    // Assuming $this->query already gone though mysql_query();
        
    if ( !$this->query || mysql_num_rows$this->query ) < )
            throw new 
    Exceptionmysql_error(), mysql_errno() );

        
    $arr = array();
        while ( 
    $row mysql_fetch_assoc$this->query ) )
            
    $arr[] = $row;
        return 
    $arr;

    Last edited by logic_earth; Feb 12, 2009 at 20:33.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  5. #5
    Non-Member
    Join Date
    Oct 2008
    Posts
    372
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by logic_earth View Post
    I'm not so sure your fetchArray method would work as expected. I think you mean to do this:

    PHP Code:
    public function fetchArray()
    {
        
    // Assuming $this->query already gone though mysql_query();
        
    if ( !$this->query || mysql_num_rows() < )
            throw new 
    Exceptionmysql_error(), mysql_errno() );

        
    $arr = array();
        while ( 
    $row mysql_fetch_assoc$this->query ) )
            
    $arr[] = $row;
        return 
    $arr;

    What's the second parameters in the exception for?

    I understand the rest.

  6. #6
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    The second parameter of an exception is a integer code. I included mysql_errno() in the second parameter for completeness. I also made an error. mysql_num_rows() needs to be passed $this-query.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  7. #7
    Non-Member
    Join Date
    Oct 2008
    Posts
    372
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes I spotted that error myself and I fixed it in my code.

    Thanks


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
  •