Re: i getting this warning [message #176017 is a reply to message #176014] |
Wed, 16 November 2011 11:56 |
Jerry Stuckle
Messages: 2598 Registered: September 2010
Karma:
|
Senior Member |
|
|
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(at)attglobal(dot)net
==================
|
|
|