473,507 Members | 3,112 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Multiple queries in one script

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");

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

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

echo various fields here

This part works fine.

----------------------------------

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");

echo query2; (returns the correct query, including the NSN)
echo $num_results (returns nothing)

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

echo various fields here (returns nothing)

$i++;
}

Can anyone tell me what I'm missing?


Feb 8 '06 #1
1 2450
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
Feb 8 '06 #2

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

Similar topics

2
10635
by: scott | last post by:
Hi, I'm having some trouble with something that should be relatively easy. I want to update multiple rows in one of my database tables simultaneously. In my table I have these values: ...
7
6169
by: Rick Caborn | last post by:
Does anyone know of a way to execute sql code from a dynamically built text field? Before beginning, let me state that I know this db architecture is built solely for frustration and I hope to...
4
16735
by: DG | last post by:
Hi, Can anyone advise how to execute multiple statements in a single query batch. For example- update customers set customer_name = 'Smith' where customer_name = 'Smyth'; select * from...
2
2028
by: Jenny Zhang | last post by:
Hi, I am running OSDL-DBT3 test against PostgreSQL. I found performance difference between the runs even though the data and queries are the same. I tried to study this problem by getting...
0
8748
by: MHenry | last post by:
Hi, I know virtually nothing about creating Macros in Access. I would appreciate some help in creating a Macro or Macros that automatically run(s) 14 Queries (three Make Table Queries, and 11...
8
12604
by: Bob Sanderson | last post by:
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...
7
43122
by: Mintyman | last post by:
Hi, I'm working on a system migration and I need to combine data from multiple rows (with the same ID) into one comma separated string. This is how the data is at the moment: Company_ID ...
4
8852
by: dreaken667 | last post by:
I have a MySQL database containing 16 tables of data. Each table has a different number of columns and there are few common field names accross tables. I do have one master table with which I connect...
4
2547
by: Akhenaten | last post by:
I am currently using enterprise manager to run multiple queries on a single table in a DB. I refresh these queries every few minutes. Due to the huge number of them I was looking for a better way...
0
7105
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...
0
7308
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,...
1
7023
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7479
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...
0
5617
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,...
1
5037
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...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3188
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
757
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.