Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #3807

Re: i getting this warning

From Jerry Stuckle <jstucklex@attglobal.net>
Newsgroups comp.lang.php
Subject Re: i getting this warning
Date 2011-11-16 06:56 -0500
Organization A noiseless patient Spider
Message-ID <ja08dc$qc0$1@dont-email.me> (permalink)
References <616578.124.1321442274513.JavaMail.geo-discussion-forums@prmf13>

Show all headers | View raw


On 11/16/2011 6:17 AM, sri kanth wrote:
> mysql_fetch_row(): supplied argument is not a valid MySQL result
> below my code:
> <?php
> $qs=$_REQUEST['id'];
> mysql_connect("localhost","root","");
> mysql_select_db("test");
> $data=mysql_query("select * from tbl_porduct where pid=$qs");
> $rec=mysql_fetch_row($data);
> $pname=$rec[1];
> $price=$rec[3];
> $sid=session_id();
> mysql_query("insert into tbl_spro values('$sid','$pname','$price')");
> ?>
>

Three things.

First of all, you probably have an error in your SQL some place - it 
could be in the connect, selecting the database or the query itself. 
But you aren't checking the results of any of your MySQL calls, so you 
don't know which might be failing.

Never assume a call works; always check the return values - see the PHP 
manual for return values.  Then if the call fails, print out a message 
and log the error (see mysql_error()).  On a development system you can 
display the error, but you do not want to do so on a production system.

Second, are you really using "root" with no password?  If you are, 
change it immediately.  This is a huge security whole and can leave your 
database open to all kinds of bad stuff.

Third, never do "SELECT *" from a database.  Always specify the columns 
you wish to fetch.  There are two reasons for this.  First of all, what 
if you (or someone else) adds a 5 MB BLOB column to the table?  You'll 
be returning a lot of information that you're not interested in for this 
request.  Specifying the columns means you won't return that BLOB unless 
you specifically ask for it.

More importantly, a change in the database can cause very difficult to 
locate problems.  For instance, lets say you have a column "name" which 
contains both first and last names.  Later you find you need to change 
this to "first_name, last_name".  Your SELECT * will still work, but 
later in your code (maybe much later) you won't have the information you 
expect in the places you expect it - this can be a very hard problem to 
troubleshoot.  Specifying the column names in your query will cause the 
query to fail in the above case, making the problem obvious.

And as a side note, if you use mysql_fetch_assoc() instead of 
mysql_fetch_row, you can use the column names as the index, i.e. 
$row['name'] instead of $row[1].  It will make your code easier to read 
and understand (plus the returned columns are not position dependent).

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

i getting this warning sri kanth <sritullimilli@gmail.com> - 2011-11-16 03:17 -0800
  Re: i getting this warning The Natural Philosopher <tnp@invalid.invalid> - 2011-11-16 11:26 +0000
    Re: i getting this warning tony@mountifield.org (Tony Mountifield) - 2011-11-16 11:37 +0000
  Re: i getting this warning Jerry Stuckle <jstucklex@attglobal.net> - 2011-11-16 06:56 -0500
    Re: i getting this warning Denis McMahon <denismfmcmahon@gmail.com> - 2011-11-16 15:11 +0000
      Re: i getting this warning Jerry Stuckle <jstucklex@attglobal.net> - 2011-11-16 10:41 -0500
      Re: i getting this warning Arno Welzel <usenet@arnowelzel.de> - 2011-11-17 14:31 +0100
        Re: i getting this warning Arno Welzel <usenet@arnowelzel.de> - 2011-11-17 14:34 +0100
          Re: i getting this warning Denis McMahon <denismfmcmahon@gmail.com> - 2011-11-18 08:02 +0000
        Re: i getting this warning The Natural Philosopher <tnp@invalid.invalid> - 2011-11-17 13:39 +0000
        Re: i getting this warning Jerry Stuckle <jstucklex@attglobal.net> - 2011-11-17 09:01 -0500
          Re: i getting this warning Arno Welzel <usenet@arnowelzel.de> - 2011-11-17 15:15 +0100
            Re: i getting this warning Jerry Stuckle <jstucklex@attglobal.net> - 2011-11-17 09:44 -0500
              Re: i getting this warning Arno Welzel <usenet@arnowelzel.de> - 2011-11-21 14:59 +0100
                Re: i getting this warning Jerry Stuckle <jstucklex@attglobal.net> - 2011-11-21 09:12 -0500

csiph-web