SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2003
    Location
    Serbia
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    The PHP Anthology Volume I, Chapter 3 - PHP and MySQL

    This forum thread discusses the SitePoint article 'The PHP Anthology Volume I, Chapter 3 - PHP and MySQL' by Harry Fuecks.

    "Chapter 3 of The PHP Anthology provides you with all the essentials of MySQL, PHP?s favorite database. From the basics of avoiding SQL injection attacks, Harry steps us through to the more advanced issues of MySQL?s FULLTEXT search facilities, corrupt table repair and database back up, and much more."

  2. #2
    SitePoint Enthusiast
    Join Date
    May 2003
    Location
    Serbia
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried using the example 3.9.5 to access db and fetch data from it, but I get an error msg:

    Fatal error: Call to a member function on a non-object in ........ on line 19

    And line 19 is:
    while ($row = $result->fetch()) {


    So I'm not sure what to do, but would like to make it work.

  3. #3
    My precious!!! astericks's Avatar
    Join Date
    Mar 2002
    Location
    Vancouver, BC
    Posts
    1,971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this is so coz most likely you missed to initialise the object $result.

    according to the article, sometihng like:
    $result = &new MySQL($host, $dbUser, $dbPass, $dbName);

  4. #4
    SitePoint Enthusiast
    Join Date
    May 2003
    Location
    Serbia
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well here's the code:

    PHP Code:
    <?php 
    // Include the MySQL class 
    require_once 'mysql.php'

    $host   'localhost'// Hostname of MySQL server 
    $dbUser 'user';    // Username for MySQL 
    $dbPass 'pass';    // Password for user 
    $dbName 'blabla'// Database name 

    // Connect to MySQL 
    $db = &new MySQL($host$dbUser$dbPass$dbName); 

    $sql "SELECT * FROM articles"

    // Perform a query getting back a MySQLResult object 
    $result $db->query($sql); 

    // Iterate through the results 
    while ($row $result->fetch()) { 
     echo 
    'Title: '  $row['a_title']  . '<br />'

    ?>
    $db is actually the only object that needs to be created, and I don't know how $result should be initiated?

  5. #5
    SitePoint Member
    Join Date
    Mar 2004
    Location
    France
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    [QUOTE=zoki]Well here's the code:

    Actually i think the $result object should be created when you run the query methods of the $db objects:
    function &query($sql)
    {
    if (!$queryResource = mysql_query($sql, $this->dbConn)) {
    trigger_error('Query failed: ' . mysql_error($this->dbConn)
    . ' SQL: ' . $sql);
    return new MySQLResult($this, $queryResource);
    }
    }


    It doesn't work for me either, well i guess the problem comes from the $dbConn which seems not to be initialised in the method Connecttodb.
    If you have a clue ... Thanks

  6. #6
    SitePoint Member zerk's Avatar
    Join Date
    Apr 2004
    Location
    Münster/Germany
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A colleague of mine stated that php4 has problems with creating a new object on return. I couldn't test it yet, but this could be a solution:

    Code:
    function &query($sql) {
       	if (!$queryResource = mysql_query($sql, $this->dbConn)) {
         	trigger_error('Query failed: ' . mysql_error($this->dbConn) . ' SQL: ' . $sql);
         	$result = new MySQLResult($this, $queryResource);
         	return $result;
       	}
    }

  7. #7
    Free your mind Toly's Avatar
    Join Date
    Sep 2001
    Location
    Panama
    Posts
    2,181
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The error is being caused because you are trying to return the result inside the if condition. Just put it outside.

    PHP Code:
        function &query($sql)
        {
            if (!
    $queryResource mysql_query($sql$this->dbConn)) {
                
    trigger_error ('Query failed: '.mysql_error($this->dbConn).
                               
    ' SQL: '.$sql);
            }

            return new 
    MySQLResult($this$queryResource);
        } 
    Community Guidelines | Community FAQ

    "He that is kind is free, though he is a slave;
    he that is evil is a slave, though he be a king." - St. Augustine

  8. #8
    SitePoint Member zerk's Avatar
    Join Date
    Apr 2004
    Location
    Münster/Germany
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah...obvious

    i must have been blind...


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
  •