PHP/MySQL warnings about results - PHP

This is a discussion on PHP/MySQL warnings about results - PHP ; I've made a function to fetch all results as an array of result- arrays. Getting the result arrays is easy, via mysql_fetch_array, and function itself is quite simple, as follows: function db_query($db, $query) { $result = mysql_query($query, $db); $res_array = ...

+ Reply to Thread
Page 1 of 3 1 2 3 LastLast
Results 1 to 10 of 24

PHP/MySQL warnings about results

  1. Default PHP/MySQL warnings about results

    I've made a function to fetch all results as an array of result-
    arrays. Getting the result arrays is easy, via mysql_fetch_array, and
    function itself is quite simple, as follows:

    function db_query($db, $query)
    {
    $result = mysql_query($query, $db);
    $res_array = array();

    if ($result) //it is a search
    {
    while($data = mysql_fetch_array($result,MYSQL_ASSOC))
    array_push($res_array,$data);
    }

    return $res_array;
    }

    But there's a slight problem: when the query in question is an INSERT,
    UPDATE or DELETE query, PHP will show me a warning saying:

    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL
    result resource in /home/grad/ccomp/06/guedesav/public_html/PbBlog/inc/
    db.php on line 28
    (line 28 is "while($data = mysql_fetch_array($result,MYSQL_ASSOC))")

    and havingthis warning on the HTML output leads to be impossible to
    use header() to, for instance, go back to the post removal page. Is
    there any way to know prior to fetching if my result is of and INSERT/
    UPDATE/DELETE instead of a SELECT query?

  2. Default Re: PHP/MySQL warnings about results

    On Nov 21, 5:43 pm, bruno_guedesav <bruno_guede...@yahoo.com.br>
    wrote:
    > I've made a function to fetch all results as an array of result-
    > arrays. Getting the result arrays is easy, via mysql_fetch_array, and
    > function itself is quite simple, as follows:
    >
    > function db_query($db, $query)
    > {
    > $result = mysql_query($query, $db);
    > $res_array = array();
    >
    > if ($result) //it is a search
    > {
    > while($data = mysql_fetch_array($result,MYSQL_ASSOC))
    > array_push($res_array,$data);
    > }
    >
    > return $res_array;
    >
    > }
    >
    > But there's a slight problem: when the query in question is an INSERT,
    > UPDATE or DELETE query, PHP will show me a warning saying:
    >
    > Warning: mysql_fetch_array(): supplied argument is not a valid MySQL
    > result resource in /home/grad/ccomp/06/guedesav/public_html/PbBlog/inc/
    > db.php on line 28
    > (line 28 is "while($data = mysql_fetch_array($result,MYSQL_ASSOC))")
    >
    > and havingthis warning on the HTML output leads to be impossible to
    > use header() to, for instance, go back to the post removal page. Is
    > there any way to know prior to fetching if my result is of and INSERT/
    > UPDATE/DELETE instead of a SELECT query?


    Hello, Bruno.

    I don't think it is a good idea to include a fetch_array into a
    db_query() type
    of function, first of all. If you really want to do that, however,
    then make a
    db_select_query() function that will do that, and do the rest with a
    db_query function.

    If you really want to stick with the idea, or if you have already done
    a lot of code
    with this function, then try this - mysql_fetch_array() will return
    FALSE if no rows
    are available in the result. Put an 'at' sign in front of the name of
    the function (like
    @mysql_fetch_array(...)). This will prevent it from echoing any
    warnings, and the function
    should still return FALSE if 1. no rows are available in the result 2.
    (probably) if the
    resource argument isn't compatible with the fetch function.

    I don't find it a good idea, though. For instance, I do it like this
    (for different
    db compatibility): I have a function db_query, which executes a query,
    and have a function
    db_fetch() which fetches a row from a result returned by db_query. Of
    course, I don't use
    exact function names db_fetch and db_query, but you got the point. So,
    I don't use db_fetch
    unless I know exactly that I gave a SELECT statement to db_query.

    Generally, when writing functions, try to stick with their exact
    purposes - if it's executing
    a statement, then it should do only that. If it's fetching a result,
    it should do only that. When
    including one into another, sooner or later you'll have to write
    another function that doesn't
    include others and the code will get messy. A -function- should have
    its -function- not -functions-.
    This way the code is cleaner and more easily understood.

    Cheers,
    Darko

  3. Default Re: PHP/MySQL warnings about results

    On Wed, 21 Nov 2007 08:43:08 -0800 (PST), bruno_guedesav wrote...
    >
    >I've made a function to fetch all results as an array of result-
    >arrays. Getting the result arrays is easy, via mysql_fetch_array, and
    >function itself is quite simple, as follows:
    >
    >function db_query($db, $query)
    >{
    > $result = mysql_query($query, $db);
    > $res_array = array();
    >
    > if ($result) //it is a search
    > {
    > while($data = mysql_fetch_array($result,MYSQL_ASSOC))
    > array_push($res_array,$data);
    > }
    >
    > return $res_array;
    >}
    >
    >But there's a slight problem: when the query in question is an INSERT,
    >UPDATE or DELETE query, PHP will show me a warning saying:
    >
    >Warning: mysql_fetch_array(): supplied argument is not a valid MySQL
    >result resource in /home/grad/ccomp/06/guedesav/public_html/PbBlog/inc/
    >db.php on line 28
    >(line 28 is "while($data = mysql_fetch_array($result,MYSQL_ASSOC))")
    >
    >and havingthis warning on the HTML output leads to be impossible to
    >use header() to, for instance, go back to the post removal page. Is
    >there any way to know prior to fetching if my result is of and INSERT/
    >UPDATE/DELETE instead of a SELECT query?



    I don't think you're suppose to use the mysql_fetch_array() function on INSERTs
    since that function is intended to capture the results from a query, rather than
    adding, deleting , or updating.

    I normally use mysql_query() for INSERTS and there's a few other functions like
    mysql_error and mysql_result you can use in conjunction with that.

    Tom
    --
    NewsGuy Free Trial Accounts
    Now a massive 20 Gigabytes of unrestricted downloads !
    http://newsguy.com/


  4. Default Re: PHP/MySQL warnings about results

    "bruno_guedesav" <bruno_guedesav@yahoo.com.br> wrote in message
    news:e3845f5f-57a0-4e47-b43d-403060cec833@i37g2000hsd.googlegroups.com...
    > I've made a function to fetch all results as an array of result-
    > arrays. Getting the result arrays is easy, via mysql_fetch_array, and
    > function itself is quite simple, as follows:
    >
    > function db_query($db, $query)
    > {
    > $result = mysql_query($query, $db);
    > $res_array = array();
    >
    > if ($result) //it is a search
    > {
    > while($data = mysql_fetch_array($result,MYSQL_ASSOC))
    > array_push($res_array,$data);
    > }
    >
    > return $res_array;
    > }
    >
    > But there's a slight problem: when the query in question is an INSERT,
    > UPDATE or DELETE query, PHP will show me a warning saying:
    >
    > Warning: mysql_fetch_array(): supplied argument is not a valid MySQL
    > result resource in /home/grad/ccomp/06/guedesav/public_html/PbBlog/inc/
    > db.php on line 28
    > (line 28 is "while($data = mysql_fetch_array($result,MYSQL_ASSOC))")


    When I've had this problem, I've implemented error-trapping - which I
    probably should have done to start with.
    The way I do it is to code something that says "if result is not an array,
    don't try to perform array functions on it".
    Or in your case "if connection is false, don't try to query it".






    >
    > and havingthis warning on the HTML output leads to be impossible to
    > use header() to, for instance, go back to the post removal page. Is
    > there any way to know prior to fetching if my result is of and INSERT/
    > UPDATE/DELETE instead of a SELECT query?




  5. Default Re: PHP/MySQL warnings about results

    In our last episode,
    <e3845f5f-57a0-4e47-b43d-403060cec833@i37g2000hsd.googlegroups.com>,
    the lovely and talented bruno_guedesav
    broadcast on comp.lang.php:

    > I've made a function to fetch all results as an array of result-
    > arrays. Getting the result arrays is easy, via mysql_fetch_array, and
    > function itself is quite simple, as follows:


    > function db_query($db, $query)
    > {
    > $result = mysql_query($query, $db);
    > $res_array = array();


    > if ($result) //it is a search
    > {
    > while($data = mysql_fetch_array($result,MYSQL_ASSOC))
    > array_push($res_array,$data);
    > }


    > return $res_array;
    > }


    > But there's a slight problem: when the query in question is an INSERT,
    > UPDATE or DELETE query, PHP will show me a warning saying:


    These queries do not return a result resource. See the manual. You have to
    test $result before attempt to fetch, or you have to use you function only
    for queries that do return result resources. (See the manual.)

    > Warning: mysql_fetch_array(): supplied argument is not a valid MySQL
    > result resource in /home/grad/ccomp/06/guedesav/public_html/PbBlog/inc/
    > db.php on line 28
    > (line 28 is "while($data = mysql_fetch_array($result,MYSQL_ASSOC))")


    > and havingthis warning on the HTML output leads to be impossible to
    > use header() to, for instance, go back to the post removal page. Is
    > there any way to know prior to fetching if my result is of and INSERT/
    > UPDATE/DELETE instead of a SELECT query?


    Yes. Sort of. You can read the manual to determine which mysql queries
    return results. Of course in some cases even those that do have result
    returns will have an empty result. You need to test it.

    --
    Lars Eighner <http://larseighner.com/> usenet@larseighner.com
    Countdown: 425 days to go.

  6. Default Re: PHP/MySQL warnings about results

    On Nov 21, 8:06 pm, Lars Eighner <use...@larseighner.com> wrote:
    > In our last episode,
    > <e3845f5f-57a0-4e47-b43d-403060cec...@i37g2000hsd.googlegroups.com>,
    > the lovely and talented bruno_guedesav
    > broadcast on comp.lang.php:
    >
    >
    >
    > > I've made a function to fetch all results as an array of result-
    > > arrays. Getting the result arrays is easy, via mysql_fetch_array, and
    > > function itself is quite simple, as follows:
    > > function db_query($db, $query)
    > > {
    > > $result = mysql_query($query, $db);
    > > $res_array = array();
    > > if ($result) //it is a search
    > > {
    > > while($data = mysql_fetch_array($result,MYSQL_ASSOC))
    > > array_push($res_array,$data);
    > > }
    > > return $res_array;
    > > }
    > > But there's a slight problem: when the query in question is an INSERT,
    > > UPDATE or DELETE query, PHP will show me a warning saying:

    >
    > These queries do not return a result resource. See the manual. You have to
    > test $result before attempt to fetch, or you have to use you function only
    > for queries that do return result resources. (See the manual.)
    >
    > > Warning: mysql_fetch_array(): supplied argument is not a valid MySQL
    > > result resource in /home/grad/ccomp/06/guedesav/public_html/PbBlog/inc/
    > > db.php on line 28
    > > (line 28 is "while($data = mysql_fetch_array($result,MYSQL_ASSOC))")
    > > and havingthis warning on the HTML output leads to be impossible to
    > > use header() to, for instance, go back to the post removal page. Is
    > > there any way to know prior to fetching if my result is of and INSERT/
    > > UPDATE/DELETE instead of a SELECT query?

    >
    > Yes. Sort of. You can read the manual to determine which mysql queries
    > return results. Of course in some cases even those that do have result
    > returns will have an empty result. You need to test it.
    >


    I've tried it, I read the manual, but anyway it will go into the loop,
    so it's just useles...

    I guess I'll try the "@" to supress error messages(that's what I was
    looking for, by the way); if it doesn't go well, there's always time
    to spli the function in two.

    Anyways, thank you all for the help!

  7. Default Re: PHP/MySQL warnings about results

    bruno_guedesav wrote:
    > On Nov 21, 8:06 pm, Lars Eighner <use...@larseighner.com> wrote:
    >> In our last episode,
    >> <e3845f5f-57a0-4e47-b43d-403060cec...@i37g2000hsd.googlegroups.com>,
    >> the lovely and talented bruno_guedesav
    >> broadcast on comp.lang.php:
    >>
    >>
    >>
    >>> I've made a function to fetch all results as an array of result-
    >>> arrays. Getting the result arrays is easy, via mysql_fetch_array, and
    >>> function itself is quite simple, as follows:
    >>> function db_query($db, $query)
    >>> {
    >>> $result = mysql_query($query, $db);
    >>> $res_array = array();
    >>> if ($result) //it is a search
    >>> {
    >>> while($data = mysql_fetch_array($result,MYSQL_ASSOC))
    >>> array_push($res_array,$data);
    >>> }
    >>> return $res_array;
    >>> }
    >>> But there's a slight problem: when the query in question is an INSERT,
    >>> UPDATE or DELETE query, PHP will show me a warning saying:

    >> These queries do not return a result resource. See the manual. You have to
    >> test $result before attempt to fetch, or you have to use you function only
    >> for queries that do return result resources. (See the manual.)
    >>
    >>> Warning: mysql_fetch_array(): supplied argument is not a valid MySQL
    >>> result resource in /home/grad/ccomp/06/guedesav/public_html/PbBlog/inc/
    >>> db.php on line 28
    >>> (line 28 is "while($data = mysql_fetch_array($result,MYSQL_ASSOC))")
    >>> and havingthis warning on the HTML output leads to be impossible to
    >>> use header() to, for instance, go back to the post removal page. Is
    >>> there any way to know prior to fetching if my result is of and INSERT/
    >>> UPDATE/DELETE instead of a SELECT query?

    >> Yes. Sort of. You can read the manual to determine which mysql queries
    >> return results. Of course in some cases even those that do have result
    >> returns will have an empty result. You need to test it.
    >>

    >
    > I've tried it, I read the manual, but anyway it will go into the loop,
    > so it's just useles...
    >
    > I guess I'll try the "@" to supress error messages(that's what I was
    > looking for, by the way); if it doesn't go well, there's always time
    > to spli the function in two.
    >
    > Anyways, thank you all for the help!


    You need to split this function... for UPDATE, DELETE, etc.
    mysql_query returns TRUE on success and FALSE on error. You get your
    loop because your query was performed successfully. Re-read the page at:
    http://us.php.net/manual/en/function.mysql-query.php
    under 'RETURNED VALUES'

    Norm

  8. Default Re: PHP/MySQL warnings about results


    > Warning: mysql_fetch_array(): supplied argument is not a valid MySQL
    > result resource in /home/grad/ccomp/06/guedesav/public_html/PbBlog/inc/
    > db.php on line 28
    > (line 28 is "while($data = mysql_fetch_array($result,MYSQL_ASSOC))")
    >
    > and havingthis warning on the HTML output leads to be impossible to
    > use header() to, for instance, go back to the post removal page. Is
    > there any way to know prior to fetching if my result is of and INSERT/
    > UPDATE/DELETE instead of a SELECT query?


    withour talking abut the SQL problem - see the other guys it should be
    possible to prvent the error message with


    $data = @mysql_fetch_array($result,MYSQL_ASSOC))

    I do not know whether it is working with
    mysql_fetch_array($result,MYSQL_ASSOC))
    but it is working with @mysql_query and @mysql_num_rows

    Regards Knut

  9. Default Re: PHP/MySQL warnings about results

    On 22 nov, 14:57, Knut Krueger <knut.krue...@usa.com> wrote:
    > > Warning: mysql_fetch_array(): supplied argument is not a valid MySQL
    > > result resource in /home/grad/ccomp/06/guedesav/public_html/PbBlog/inc/
    > > db.php on line 28
    > > (line 28 is "while($data = mysql_fetch_array($result,MYSQL_ASSOC))")

    >
    > > and havingthis warning on the HTML output leads to be impossible to
    > > use header() to, for instance, go back to the post removal page. Is
    > > there any way to know prior to fetching if my result is of and INSERT/
    > > UPDATE/DELETE instead of a SELECT query?

    >
    > withour talking abut the SQL problem - see the other guys it should be
    > possible to prvent the error message with
    >
    > $data = @mysql_fetch_array($result,MYSQL_ASSOC))
    >
    > I do not know whether it is working with
    > mysql_fetch_array($result,MYSQL_ASSOC))
    > but it is working with @mysql_query and @mysql_num_rows


    Oh, yes, it worked, indeed. There's no problem for me to have a single
    entrance on the loop because of the first obligatory fetch as long as
    it goes away cleanly so I can use headers afterwards, so this thread
    is ended for me.

  10. Default Re: PHP/MySQL warnings about results

    ..oO(Knut Krueger)

    >withour talking abut the SQL problem - see the other guys it should be
    >possible to prvent the error message with
    >
    >
    >$data = @mysql_fetch_array($result,MYSQL_ASSOC))
    >
    >I do not know whether it is working with
    >mysql_fetch_array($result,MYSQL_ASSOC))
    >but it is working with @mysql_query and @mysql_num_rows


    Using '@' is really bad style in most cases. It suppresses the error
    message instead of fixing it.

    Micha

+ Reply to Thread
Page 1 of 3 1 2 3 LastLast

Similar Threads

  1. Help with displaying MySQL query results
    By Application Development in forum PHP
    Replies: 10
    Last Post: 11-14-2007, 04:15 PM
  2. Replies: 3
    Last Post: 08-20-2007, 07:34 PM
  3. parsing MySQL query results
    By Application Development in forum PHP
    Replies: 2
    Last Post: 07-27-2007, 10:33 PM
  4. Urgent!!! DIfferent results with MYSQL and VB
    By Application Development in forum basic.visual
    Replies: 1
    Last Post: 02-24-2004, 12:53 PM
  5. DBI queries with same results (mysql)
    By Application Development in forum Perl
    Replies: 1
    Last Post: 07-31-2003, 09:39 AM