473,466 Members | 1,324 Online
Bytes | Software Development & Data Engineering Community
Create 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
8 12602
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
Sorry, there's a couple of typos there (in the message, not in the script).
Here's what the actual script reads, not including the for-next loop:

$query2="select * from jobs where NSN like $NSN";
$result=mysql_query($query2);
$num_results=mysql_num_rows($result);

$NSN=mysql_result($result,$i,"NSN");

echo "query = $query2<br>";
echo "num_results = $num_results<br>";

Again, the query is exactly what I want, but $num_results returns nothing
(I'm sure that there are more than 1 records meeting the criteria).

< What is $i here?

As I said, I'm new to MySql. I took an example from a book and the $i was
included. It appears that, in this case, it's unnecessary but it doesn't
seem to matter if it's there or not.

Feb 8 '06 #3
Sorry, there's a couple of typos there (in the message, not in the script).
Here's what the actual script reads, not including the for-next loop:

$query2="select * from jobs where NSN like $NSN";
$result=mysql_query($query2);
$num_results=mysql_num_rows($result);

$NSN=mysql_result($result,$i,"NSN");

echo "query = $query2<br>";
echo "num_results = $num_results<br>";

Again, the query is exactly what I want, but $num_results returns nothing
(I'm sure that there are more than 1 records meeting the criteria).

< What is $i here?

As I said, I'm new to MySql. I took an example from a book and the $i was
included. It appears that, in this case, it's unnecessary but it doesn't
seem to matter if it's there or not.

Feb 8 '06 #4
Sorry, there's a couple of typos there (in the message, not in the script).
Here's what the actual script reads, not including the for-next loop:

$query2="select * from jobs where NSN like $NSN";
$result=mysql_query($query2);
$num_results=mysql_num_rows($result);

$NSN=mysql_result($result,$i,"NSN");

echo "query = $query2<br>";
echo "num_results = $num_results<br>";

Again, the query is exactly what I want, but $num_results returns nothing
(I'm sure that there are more than 1 records meeting the criteria).

< What is $i here?

As I said, I'm new to MySql. I took an example from a book and the $i was
included. It appears that, in this case, it's unnecessary but it doesn't
seem to matter if it's there or not.
Feb 9 '06 #5
Forgive me if this is posted more than once, but it is not showing up in my
news reader.

===============

Sorry, there's a couple of typos there (in the message, not in the script).
Here's what the actual script reads, not including the for-next loop:

$query2="select * from jobs where NSN like $NSN";
$result=mysql_query($query2);
$num_results=mysql_num_rows($result);

$NSN=mysql_result($result,$i,"NSN");

echo "query = $query2<br>";
echo "num_results = $num_results<br>";

Again, the query is exactly what I want, but $num_results returns nothing
(I'm sure that there are more than 1 records meeting the criteria).

< What is $i here?

As I said, I'm new to MySql. I took an example from a book and the $i was
included. It appears that, in this case, it's unnecessary but it doesn't
seem to matter if it's there or not.
Feb 9 '06 #6
Hi Bob,

What you are missing are quotes. Your second query should read:

$query2="select * from jobs where NSN like '$NSN' ";

My recommendation is to print the query out when you debugging your
code - and then do a copy/paste into something like phpMyAdmin or Mysql
Command Center or at the Mysql command line - basically test the query
out in an app that will let you make quick changes and retest. When
you are able to get the query working correctly, then copy it back into
your code and (replacing vars as needed).

-CF

Feb 9 '06 #7
Hi Bob,

If $i is not perviously set, then it will equal "". It should equal an
integer value of 0 or greater. This probably belongs on the PHP
newgroup, but the PHP/Mysql community tends to be pretty tight.

mysql_result is a function that is defined as:
mysql_result ( resource result, int row [, mixed field] )

A VERY handy resource (one of the best programmer API documentation
emplemetation I have come across) is PHP.net. Specificially see this
link:

http://us3.php.net/manual/en/function.mysql-result.php

-CF

Feb 9 '06 #8
Sorry, there's a couple of typos there (in the message, not in the script).
Here's what the actual script reads, not including the for-next loop:

$query2="select * from jobs where NSN like $NSN";
$result=mysql_query($query2);
$num_results=mysql_num_rows($result);

$NSN=mysql_result($result,$i,"NSN");

echo "query = $query2<br>";
echo "num_results = $num_results<br>";

Again, the query is exactly what I want, but $num_results returns nothing
(I'm sure that there are more than 1 records meeting the criteria).

< What is $i here?

As I said, I'm new to MySql. I took an example from a book and the $i was
included. It appears that, in this case, it's unnecessary but it doesn't
seem to matter if it's there or not.
Feb 10 '06 #9

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

Similar topics

2
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
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
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
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
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...
1
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...
18
by: Gleep | last post by:
I've searched google intensely on this topic and it seems noone really knows how to approch this. The goal I don't want clients to give out their usernames and passwords to friends, since the site...
7
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
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
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
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
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
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,...
0
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
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.