You are here: Re: Multiple queries in one script « PHP Programming Language « IT news, forums, messages
Re: Multiple queries in one script

Posted by Erwin Moller on 10/13/47 11:39

Bob Sanderson wrote:

Hi Bob,

> I am relatively new to PHP and MySQL. This is the first time I've tried
> to use multiple queries in a single script.
>
> I have the following PHP script which gets a Job Number from a search
> form and generates a web page which displays the record for that job:
>
> $username="root";
> $password="";
> $database="foobar";
>
> $Searchterm = $_GET['JobNumber'];
>
> mysql_connect('localhost',$username,$password);
> @mysql_select_db($database) or die( "Unable to select database");
>

Ok so far.

> $query="select * from jobs where JobNumber like $Searchterm";

This is very dangerous.
NEVER EVER thrust input originating from a form that is filled in by some
user.
You are wide open to the SQL-Injection attack this way.

If you have magic_quotes on, you are a lot safer, but please be sure what
you are doing...

>
> $result=mysql_query($query);
> $num_results=mysql_num_rows($result);
> $JobNumber=mysql_result($result,$i,"JobNumber");
> $NSN=mysql_result($result,$i,"NSN");

What is $i here?
$i defines the row to be retrieved, but you didn't give it any value.

>
> echo various fields here
>
> This part works fine.

good. :-)

Suprisingly because you didn't define $i.....

>
> ----------------------------------
>
> I now want to list other jobs which use the same NSN on the same page
> under that display. This is the script I'm using:
>
> $query2="select * from jobs where NSN like $NSN";
> $result=mysql_query($query2);
> $num_results=mysql_num_rows($result);
> $JobNumber=mysql_result($result,$i,"JobNumber");

What is $i here?

>
> echo query2; (returns the correct query, including the NSN)

That should be:
echo $query2;

You forgot the $

> echo $num_results (returns nothing)

should return something if you fix the previous code. :-)

>
> $i=0;
> while ($i < $num) {

What is $num?
Do you mean $num_results???

>
> echo various fields here (returns nothing)
>
> $i++;
> }
>
> Can anyone tell me what I'm missing?

Fix the various mistakes. :-)
Good luck!

Regards,
Erwin Moller

 

Navigation:

[Reply to this 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

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