473,768 Members | 7,620 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_arr ay, and
function itself is quite simple, as follows:

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

if ($result) //it is a search
{
while($data = mysql_fetch_arr ay($result,MYSQ L_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_arr ay(): 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($dat a = mysql_fetch_arr ay($result,MYSQ L_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?
Nov 21 '07 #1
21 3704
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_arr ay, and
function itself is quite simple, as follows:

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

if ($result) //it is a search
{
while($data = mysql_fetch_arr ay($result,MYSQ L_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_arr ay(): 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($dat a = mysql_fetch_arr ay($result,MYSQ L_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_arr ay() 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_ar ray(...)). 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
Nov 21 '07 #2
"bruno_guedesav " <br************ @yahoo.com.brwr ote in message
news:e3******** *************** ***********@i37 g2000hsd.google groups.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_arr ay, and
function itself is quite simple, as follows:

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

if ($result) //it is a search
{
while($data = mysql_fetch_arr ay($result,MYSQ L_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_arr ay(): 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($dat a = mysql_fetch_arr ay($result,MYSQ L_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?

Nov 21 '07 #3
In our last episode,
<e3************ *************** *******@i37g200 0hsd.googlegrou ps.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_arr ay, and
function itself is quite simple, as follows:
function db_query($db, $query)
{
$result = mysql_query($qu ery, $db);
$res_array = array();
if ($result) //it is a search
{
while($data = mysql_fetch_arr ay($result,MYSQ L_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_arr ay(): 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($dat a = mysql_fetch_arr ay($result,MYSQ L_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/us****@larseigh ner.com
Countdown: 425 days to go.
Nov 21 '07 #4
On Nov 21, 8:06 pm, Lars Eighner <use...@larseig hner.comwrote:
In our last episode,
<e3845f5f-57a0-4e47-b43d-403060cec...@i3 7g2000hsd.googl egroups.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_arr ay, and
function itself is quite simple, as follows:
function db_query($db, $query)
{
$result = mysql_query($qu ery, $db);
$res_array = array();
if ($result) //it is a search
{
while($data = mysql_fetch_arr ay($result,MYSQ L_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_arr ay(): 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($dat a = mysql_fetch_arr ay($result,MYSQ L_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!
Nov 22 '07 #5
bruno_guedesav wrote:
On Nov 21, 8:06 pm, Lars Eighner <use...@larseig hner.comwrote:
>In our last episode,
<e3845f5f-57a0-4e47-b43d-403060cec...@i3 7g2000hsd.googl egroups.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_arr ay, and
function itself is quite simple, as follows:
function db_query($db, $query)
{
$result = mysql_query($qu ery, $db);
$res_array = array();
if ($result) //it is a search
{
while($data = mysql_fetch_arr ay($result,MYSQ L_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_arr ay(): 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($dat a = mysql_fetch_arr ay($result,MYSQ L_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
Nov 22 '07 #6
Warning: mysql_fetch_arr ay(): 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($dat a = mysql_fetch_arr ay($result,MYSQ L_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_ar ray($result,MYS QL_ASSOC))

I do not know whether it is working with
mysql_fetch_arr ay($result,MYSQ L_ASSOC))
but it is working with @mysql_query and @mysql_num_rows

Regards Knut
Nov 22 '07 #7
On 22 nov, 14:57, Knut Krueger <knut.krue...@u sa.comwrote:
Warning: mysql_fetch_arr ay(): 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($dat a = mysql_fetch_arr ay($result,MYSQ L_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_ar ray($result,MYS QL_ASSOC))

I do not know whether it is working with
mysql_fetch_arr ay($result,MYSQ L_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.
Nov 22 '07 #8
..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_ar ray($result,MYS QL_ASSOC))

I do not know whether it is working with
mysql_fetch_ar ray($result,MYS QL_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
Nov 23 '07 #9
Michael Fesser schrieb:
.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_ar ray($result,MYS QL_ASSOC))

I do not know whether it is working with
mysql_fetch_ar ray($result,MYS QL_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
but it is better to supress one error instead of setting error_reporting
off, isn`t it?
Regards Knut
Nov 23 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3847
by: Reply via newsgroup | last post by:
Folks, When performing an update in mysql (using PHP), can I find out how many records were matched? mysql_affected_rows() won't work... and I have the following problem that I thought I could resolve with a simple function: Example: I have 50records - I want to update a selection of the recods - some,
0
6460
by: Gordon | last post by:
I have 2 tables t and t1. In this case, t1 is a copy of t. I want to delete rows from t1 based on criteria on the t table and a relationship between t ad t1 (in this case the id column). In the results below, I would think that the delete should have deleted row 1 {1 5 me) and not row 3 (1 5 they) when I run this statement delete t1 from t, t1 where t.id = t1.id and t.id=1 and t.name = 'me'; Any ideas on why row 2 is deleted?
0
3527
by: Lenz Grimmer | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, MySQL 4.0.14, a new version of the popular Open Source/Free Software Database, has been released. It is now available in source and binary form for a number of platforms from our download pages at http://www.mysql.com/downloads/ and mirror sites.
4
867
by: Ka | last post by:
I install a mysql server in default installation with latin charset, but I want to use GBK(a chinese charset), so that I can store and search chinese words directly. so, I download, unpack and then rewrite the mysql.spec and then recompile the package mysql-3.23.56-1.80.src.rpm. first, this file contains some files: # rpm2cpio mysql-3.23.56-1.80.src.rpm |cpio -t
0
3264
by: I.P. | last post by:
Hi, it's my story. I have two 4.0.14 mysql server on one machine with win XP Professional polish version. First acts as master: on port 3300 Second acts as slave: on port 3301 below my configuration:
0
2146
by: I.P. | last post by:
No one has replied to my post. ----- Original Message ----- From: "I.P." <jancio_wodnik@wp.pl> To: <mysql@lists.mysql.com> Sent: Monday, August 18, 2003 1:01 PM Subject: mysql 4.0.14 + replication + windows XP PROF Hi, it's my story.
13
1962
by: miker2 | last post by:
HI, I'm having trouble writing to a MySql db using python and the MySQLdb module. Here is the code: import MySQLdb base = MySQLdb.connect(host="localhost", user="blah", passwd="blah", db="test_py") cursor = base.cursor() cursor.execute("INSERT INTO table (field) VALUES (int)")
1
15389
by: Ike | last post by:
Recently, I began using a different MySQL verver (i.e. different machine as well as different version#, going from 4.12a to 4.1.9 max). The following query used to work: select firstname, lastname, from associates where username like 'nancianne' but now fails with: "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'from associates
1
5285
by: PowerLifter1450 | last post by:
I've been having a very rough time installinig mySQL on Linux. I have been following the instructions form here: http://www.hostlibrary.com/installing_apache_mysql_php_on_linux Everytime I get to #./configure it goes through all of the preparing tables and starting mysqlServer and daemon, but than immediaetly says "mysql ended" -- I try to do #make right after anyway, but I get the error "No targets specified and no makefile found" -- Any...
0
9407
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10171
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10015
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9842
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8840
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7384
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5425
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3931
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2808
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.