Reply to Re: PHP/MySQL warnings about results

Your name:

Reply:


Posted by Norman Peelman on 11/22/07 14:27

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

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация