473,546 Members | 2,244 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SQL Query - Results from another PHP Page ?

Can I store this PHP code in another page, then refernece it on
multiple pages using Require or Include ???

//results from sql query
for ($i=0; $i<$number; $i++) {
$ID = mysql_result($r esult,$i,"ID");
$CompanyName = mysql_result($r esult,$i,"1");
$ContactName = mysql_result($r esult,$i,"2");
$Phone = mysql_result($r esult,$i,"3");
$HomePhone = mysql_result($r esult,$i,"4");
$MobilePhone = mysql_result($r esult,$i,"5");
$AltPhone = mysql_result($r esult,$i,"6");
$SecondContact = mysql_result($r esult,$i,"7");
$SecondPhone = mysql_result($r esult,$i,"8");
$ThirdContact = mysql_result($r esult,$i,"9");
$ThirdPhone = mysql_result($r esult,$i,"10");
$Notes = mysql_result($r esult,$i,"11")
This will save me having to have all that on the pages its needed if I
can be used from another page !

Jul 16 '05 #1
4 5751
On Tue, 15 Jul 2003 08:05:27 -0500, Frank wrote:
Can I store this PHP code in another page, then refernece it on multiple
pages using Require or Include ???

//results from sql query
for ($i=0; $i<$number; $i++) {
$ID = mysql_result($r esult,$i,"ID");
$CompanyName = mysql_result($r esult,$i,"1"); $ContactName =
mysql_result($r esult,$i,"2"); $Phone = mysql_result($r esult,$i,"3");
$HomePhone = mysql_result($r esult,$i,"4"); $MobilePhone =
mysql_result($r esult,$i,"5"); $AltPhone =
mysql_result($r esult,$i,"6"); $SecondContact =
mysql_result($r esult,$i,"7"); $SecondPhone =
mysql_result($r esult,$i,"8"); $ThirdContact =
mysql_result($r esult,$i,"9"); $ThirdPhone =
mysql_result($r esult,$i,"10"); $Notes = mysql_result($r esult,$i,"11")
This will save me having to have all that on the pages its needed if I
can be used from another page !


Why not just use mysql_fetch_arr ay() and access
the fields by name? Try this:

$myrow = mysql_fetch_arr ay ($result, MYSQL_BOTH);

Now all of the fields will be in the $myrow array.
You can access the values by number or name. So this
code will print the company name.

echo $myrow['1'];

HTH

Jul 16 '05 #2
<Frank @ MyPlace.Com (Frank)> wrote in message
news:3f******** ********@news.b tclick.com...
Can I store this PHP code in another page, then refernece it on
multiple pages using Require or Include ???

//results from sql query
for ($i=0; $i<$number; $i++) {
$ID = mysql_result($r esult,$i,"ID");
$CompanyName = mysql_result($r esult,$i,"1");
$ContactName = mysql_result($r esult,$i,"2");
$Phone = mysql_result($r esult,$i,"3");
$HomePhone = mysql_result($r esult,$i,"4");
$MobilePhone = mysql_result($r esult,$i,"5");
$AltPhone = mysql_result($r esult,$i,"6");
$SecondContact = mysql_result($r esult,$i,"7");
$SecondPhone = mysql_result($r esult,$i,"8");
$ThirdContact = mysql_result($r esult,$i,"9");
$ThirdPhone = mysql_result($r esult,$i,"10");
$Notes = mysql_result($r esult,$i,"11")
This will save me having to have all that on the pages its needed if I
can be used from another page !


Not only can you, but you should. Code duplication should be avoided at all
costs. It will save you so much time later if you consolidate repeated code
into a single file. Just think of include() as a way of inserting an entire
file in-line with your current file.

Also, read the PHP manual about the mysql_result() function. It says:

"When working on large result sets, you should consider using one of the
functions that fetch an entire row (specified below). As these functions
return the contents of multiple cells in one function call, they're MUCH
quicker than mysql_result(). "

"Recommende d high-performance alternatives: mysql_fetch_row (),
mysql_fetch_arr ay(), and mysql_fetch_obj ect()."

HTH,
Zac
Jul 16 '05 #3

Tony, I'm new to this, can you point me in the right direction ??

Thanks !

I would strongly recommend that you use a function which returns the
result set as an associative array so that you can access field values
using the field name rather than its sequence number in the array. Not
only does it make it easier to code, it also gets around the problem
that can be caused if the fields get returned in a different sequence.

That's just my opinion.

Tony Marston
http://www.tonymarston.net/


Jul 16 '05 #4
Take a look in the PHP documentation for the MySQL functions at
http://www.php.net/manual/en/ref.mysql.php

http://www.php.net/manual/en/functio...etch-array.php - Fetch a
result row as an associative array, a numeric array, or both.

http://www.php.net/manual/en/functio...etch-assoc.php - Fetch a
result row as an associative array.

http://www.php.net/manual/en/functio...tch-object.php - Fetch a
result row as an object.

Tony Marston
Frank @ MyPlace.Com (Frank) wrote in message news:<3f******* *********@news. btclick.com>...
Tony, I'm new to this, can you point me in the right direction ??

Thanks !

I would strongly recommend that you use a function which returns the
result set as an associative array so that you can access field values
using the field name rather than its sequence number in the array. Not
only does it make it easier to code, it also gets around the problem
that can be caused if the fields get returned in a different sequence.

That's just my opinion.

Tony Marston
http://www.tonymarston.net/

Jul 16 '05 #5

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

Similar topics

3
3951
by: Mike Cocker | last post by:
Hello, I'm quite weak at PHP, so I was hoping to get some help understanding the below code. First off, I'm trying to create a "query form" that will allow me to display the results on my screen. I grabbed this code from the net hoping that I could tweak it for my needs. I'm using MySQL, PHP and IIS and they all are running fine. As the...
6
1705
by: Bob Bedford | last post by:
I've a query quite important to execute. I've to fill an array with all result, so the query is called once wich returns all ID's Then I call the same query a second time as I shown only 10 results in my page. How can I avoid to call the query twice ?
2
3418
by: jaysonsch | last post by:
Hello! I am having some problems with a database query that I am trying to do. I am trying to develop a way to search a database for an entry and then edit the existing values. Upon submit, the new values are updated in all corresponding tables (the function of the pages in question). However, on the page that does the DB update, I also...
0
2799
by: david liu | last post by:
access 2000 query: here's what i want to do. from an asp page, perform a search on a table in access. i have used sql code in the asp page itself, but i'd rather execute a query in access. i have success in running any query (basic SELECT, SELECT with conditions _other_ than LIKE, etc..) for some reason, when i execute the query below from...
2
2381
by: Mattyboy | last post by:
Guys I have built a database with saved queries that runs fine in Access but when I call it from the web using ASP, an exception occurs. I have tried multiple ways of testing the databases with the following results. www.brinkster.com has a tool on their free asp hosting where you can dump an access .mdb database on there and then run SQL...
6
4829
by: jjturon | last post by:
Can anyone help me?? I am trying to pass a Select Query variable to a table using Dlookup and return the value to same select query but to another field. Ex. SalesManID SalesManName AT Alan Time
2
2320
by: webhead74 | last post by:
Hi, I'm having intermittent problems with queries from my php script to a postgresql database. I have a form where I can enter a search query - for instance a last name. This leads to a results page with a brief bit of information about each of the matching results. From there, I can click a link associated with any of the results...
2
1938
by: milo1955 | last post by:
I have a form that passes a single variable via the GET function to a mysql databse query with the results paginated. The first page of the results work fine, but the link to the second page yields the page 2 results of the entire table. I'm guessing that the variable hasn't passed to page2 query? Please excuse my ignorance. Here's my code: ...
7
4607
bugboy
by: bugboy | last post by:
Hi, I'm just learning PHP. I want to redirect the user, after hitting submit, either to the same page displaying the results (as it is now) or if no results are found then send them to a new page passing on the original form data there instead. Below is a simplified version of what i'm trying to do. The user sumits a word. If it's in the DB...
10
2633
by: Lloyd Harold | last post by:
I'm very new to PHP and attempting to put together a simple script for retrieving MySQL data of personal records. The MySQL table I'm using consists of: 0: id 1: name 2: location (an integer relating to a separate table of locations). 3: details
0
7435
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7947
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7461
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7794
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5080
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3492
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3472
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1046
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
747
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.