Re: Hi I am new to PHP

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Thu, 15 Apr 2004 14:58:09 -0700 (PDT)
andy amol <andy_amol_2003@xxxxxxxxx> wrote:

> Hi,
>    I have a problem with the second file. I am not able to update the 
> data from the php program. Any help on that would be appreciated. 
> Any modification which would be reduce the code like directly using
> the Update command would be appreciated.
>
> thanks in advance.
>  
> My exact problem with second file is as follows:
> 
> Warning: mysql_num_rows(): supplied argument is not a valid MySQL
> result resource on line 15

Andy, 
This is probably not the entire error message. The above error message would have shown the name of the php script that is broken. This would have made it easier for whoever
> 
> 
> Here are the 3 files.
> ////////////////////
> File1
Looks okay...

> ////////////////////
> File 2
> ---------------------
> <html><head><title>Book change form</title></head>
> <body>
> <?
> $id=$_POST['id'];
> $db="project";
> //mysql_connect(localhost,$_POST['username'],$_POST['pass']);
> $link = mysql_connect("localhost","name","passwd");
> if (! $link)
> die("Couldn't connect to MySQL");

A better way to write this is 
> $link = mysql_connect("localhost","name","passwd") 
	or die("Couldn't connect to MySQL");

> mysql_select_db($db , $link)
> or die("Couldn't open $db: ".mysql_error());
> $query=" SELECT * FROM books WHERE id='$id'";
> $result=mysql_query($query);
> $num=mysql_num_rows($result);
> //echo $num;
> $i=0;
> while ($i < $num)
> {     

This could be rewritten as
while ($row = mysql_fetch_assoc($result);) {
The reason wil soon become apparent.

Okay, using mysql_result() functions calls is expensive as far as resources go.

> $isbn=mysql_result($result,$i,"isbn");
Each of these lines change into similar to the following example line
echo $row["isbn"];

> $title=mysql_result($result,$i,"title");
echo $row["title"];
> $author=mysql_result($result,$i,"author");
echo $row["author"];
> $edition=mysql_result($result,$i,"edition");
echo $row["edition"];
> $course_id=mysql_result($result,$i,"course");
echo $row["course"];
> $quantity=mysql_result($result,$i,"quantity");
echo $row["quantity"];
> $stack_no=mysql_result($result,$i,"stack_no");
echo $row["stack_no"];

The reason for doing this is that each call to mysql_result() requires PHP to ask the MySQL server for the cell specified. Even if your web server and MySQL server is on the same machine, you will still notice a speed increase if you do just one fetch per row rather than a fetch per field in the row. You would use mysql_result() if you are wanting only one cell from a query result. 

Otherwise this the rest of the looks pretty good.

> ?>
> </body>
> </html>
> //////////////
> File 3
> ------------------------------------
> <html><head><title>Book Change Record</title></head>
> <body>
> <?
> $user=$_POST['username'];
> $password=$_POST['password'];
Move this line up to the other database connection values.

The rest of the file looks okay...

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux