problems with mysql_fetch_array($IsResult,MYSQL_NUM)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Patrick

    problems with mysql_fetch_array($IsResult,MYSQL_NUM)

    Hi

    I am trying to write a simple database class to encapsulate all the
    database functions in one. Howerver I am having problems with
    while($row = mysql_fetch_arr ay($IsResult,MY SQL_NUM)) line it never
    executes the loop. $IsResult is reeves no value.

    What am I doing wrong

    Thanks

    pat

    <?php

    class CDatabase
    {
    // Database
    var $misOpen = false;
    var $mResult;

    function getResults()
    {
    return $this->mResult;
    }

    function isOpen()
    {
    return $this->misOpen;
    }

    // constructer
    function CDatabase($user name,$password, $host,$database name)
    {
    $db_connection = mysql_connect($ host,$username, $password);
    $this->misOpen = false;

    if($db_connecti on)
    {
    $this->misOpen = true;
    mysql_select_db ($databasename) ;
    }

    echo mysql_error();
    }

    function __destruct()
    {
    $this->disconnect() ;
    }

    function disconnect()
    {
    if ($this->isOpen())
    {
    mysql_close();
    }
    }

    function sqlQuery($query )
    {
    $numberofResult s = 0;
    $IsResult = mysql_query($qu ery);

    echo "IsResult= '".$IsResult ."' <br/>\n";

    while($row = mysql_fetch_arr ay($IsResult,MY SQL_NUM))
    {
    $this->mResult[] = $row;
    $numberofResult s++;
    echo $numberofResult s++;
    echo "loop";
    }

    return $numberofResult s;
    }

    };

    ?>

  • purcaholic

    #2
    Re: problems with mysql_fetch_arr ay($IsResult,MY SQL_NUM)

    On 28 Mai, 09:55, Patrick <hornep...@gmai l.comwrote:
    Hi
    >
    I am trying to write a simple database class to encapsulate all the
    database functions in one. Howerver I am having problems with
    while($row = mysql_fetch_arr ay($IsResult,MY SQL_NUM)) line it never
    executes the loop. $IsResult is reeves no value.
    >
    What am I doing wrong
    >
    Thanks
    >
    pat
    >
    <?php
    >
    class CDatabase
    {
    // Database
    var $misOpen = false;
    var $mResult;
    >
    function getResults()
    {
    return $this->mResult;
    }
    >
    function isOpen()
    {
    return $this->misOpen;
    }
    >
    // constructer
    function CDatabase($user name,$password, $host,$database name)
    {
    $db_connection = mysql_connect($ host,$username, $password);
    $this->misOpen = false;
    >
    if($db_connecti on)
    {
    $this->misOpen = true;
    mysql_select_db ($databasename) ;
    }
    >
    echo mysql_error();
    }
    >
    function __destruct()
    {
    $this->disconnect() ;
    }
    >
    function disconnect()
    {
    if ($this->isOpen())
    {
    mysql_close();
    }
    }
    >
    function sqlQuery($query )
    {
    $numberofResult s = 0;
    $IsResult = mysql_query($qu ery);
    >
    echo "IsResult= '".$IsResult ."' <br/>\n";
    >
    while($row = mysql_fetch_arr ay($IsResult,MY SQL_NUM))
    {
    $this->mResult[] = $row;
    $numberofResult s++;
    echo $numberofResult s++;
    echo "loop";
    }
    >
    return $numberofResult s;
    }
    >
    };
    >
    ?>

    Your class seems to work (but i haven't tested it).

    I suppose the passed query is invalid. And an try{}catch{} block to
    your sqlQuery function and throw an error, if mysql_query fails.

    Tip:
    You increment the $numberofResult s counter twice ("$numberofResu lts++"
    and "echo $numberofResult s++"), once is enough.
    Use __construct() instead of CDatabase().

    purcaholic

    Comment

    • Ravi

      #3
      Re: problems with mysql_fetch_arr ay($IsResult,MY SQL_NUM)

      Your class is working fine. Please check weather the query is
      executing or not. Check it using
      mysql_query($qr y) or die(mysql_error ()); this code

      Comment

      • Schraalhans Keukenmeester

        #4
        Re: problems with mysql_fetch_arr ay($IsResult,MY SQL_NUM)

        At Mon, 28 May 2007 00:55:35 -0700, Patrick let h(is|er) monkeys type:
        Hi
        >
        I am trying to write a simple database class to encapsulate all the
        database functions in one. Howerver I am having problems with
        while($row = mysql_fetch_arr ay($IsResult,MY SQL_NUM)) line it never
        executes the loop. $IsResult is reeves no value.
        >
        What am I doing wrong
        >
        Thanks
        >
        pat
        >
        <?php
        >
        class CDatabase
        {
        // Database
        var $misOpen = false;
        var $mResult;
        >
        function getResults()
        {
        return $this->mResult;
        }
        >
        function isOpen()
        {
        return $this->misOpen;
        }
        >
        // constructer
        function CDatabase($user name,$password, $host,$database name)
        {
        $db_connection = mysql_connect($ host,$username, $password);
        $this->misOpen = false;
        >
        if($db_connecti on)
        {
        $this->misOpen = true;
        mysql_select_db ($databasename) ;
        }
        >
        echo mysql_error();
        }
        >
        function __destruct()
        {
        $this->disconnect() ;
        }
        >
        function disconnect()
        {
        if ($this->isOpen())
        {
        mysql_close();
        }
        }
        >
        function sqlQuery($query )
        {
        $numberofResult s = 0;
        $IsResult = mysql_query($qu ery);
        >
        echo "IsResult= '".$IsResult ."' <br/>\n";
        >
        while($row = mysql_fetch_arr ay($IsResult,MY SQL_NUM))
        {
        $this->mResult[] = $row;
        $numberofResult s++;
        echo $numberofResult s++;
        echo "loop";
        }
        >
        return $numberofResult s;
        }
        >
        };
        >
        ?>
        Contrary to other comments' suggestion your class does not work fine yet.
        But you knew that, or you hadn't come here ;-)
        There's a few things worth paying further attention to:

        1. The reason why your query does not execute.
        You open a connection and store the result (resource or FALSE) in a local
        variable. As soon as the constructor exits it goes out of scope and the
        resource is gone.

        Fix: change $db_connection to $this->db_connectio n and declare it as a
        class variable.

        2. Error checking consistence:
        You check for a succesful mysql_connect() but don't check the result of
        the mysql_select_db ()

        Fix: do a second result check after the mysql_select_db and postpone the
        $misOpen = true assignment till after the second check.
        You could combine the testslike so:

        function CDatabase ($h, $u, $p, $db) {
        $this->misOpen =
        (($this->db_connectio n = mysql_connect($ h,$u,$p)) &&
        mysql_select_db ($db))
        // mysql_select_db only executes if mysql_connect returns true
        // to suppress mysql warnings/errors, prepend the functions with '@'
        }

        Looking at the use of keyword 'var' in your script I assume you use PHP4.x
        If so, ignore purcaholic's remark about using __construct().
        If not (and you _are_ using PHP5) you ought to declare your vars without
        the var keyword and since they should be hidden from the class user,
        declare them private.

        In the sqlQuery() function, you collect the result and print it's value (I
        assume this is debugging extra code) but don't test the value before
        you start fetching rows from the db. Besides, you don't know if there are
        any rows to be fetched.

        You might rewrite:
        if ($result = mysql_query($qu ery))
        {
        if (($numrows = mysql_numrows($ result)) 0)
        {
        while ($this->mresult[] = mysql_fetch_arr ay ($result));
        // if you want to display the counter and 'loop', remove the ; from
        // previous line, and write your code here between { and }
        }
        return $numrows;
        }

        The try/catch block suggestion is nice, but I'd leave that for a later
        stage when you feel a little more comfortable around PHP, imho. Or, if
        you've become curious about it, study it on a separate trail and only
        implement it in practical code when you have a good grasp of how, when and
        why to use it.

        And you know (of course?!) there are ready-to-use db classes and
        extensions available ? PDO comes standard with PHP I believe, and PEAR has
        a good universal db extension.

        Lastly, I probably made some typos here and there, hope you manage to use
        the suggested snippets nonetheless.

        Have a nice day, and good luck programming!

        --
        Schraalhans Keukenmeester - schraalhans@the .spamtrapexampl e.nl
        [Remove the lowercase part of Spamtrap to send me a message]

        "strcmp('apples ','oranges') is -1"

        Comment

        Working...