SunQuest
 
           MySQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesMySQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old February 14th, 2007, 02:17 PM
mcgibbon mcgibbon is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2007
Posts: 7 mcgibbon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 30 m 42 sec
Reputation Power: 0
Partly Working

I am trying to do a query on a table an present the results. I have got it working just fine for the first set of results but the anything after that doesn't appear to be working. Here is the code:

PHP Code:
<?php
$connect 
mysql_connect("localhost","ID","PW");

$db mysql_select_db("industrialodorcontrol",$connect);

$sql "SELECT * FROM links WHERE category = 'Contractors'";

$result mysql_query($sql);


$num=mysql_numrows($result);
while (
$i $num) {
$user_array = @mysql_fetch_array($result);

echo 
'<B>';
echo 
$user_array["Name"];
echo 
'</B>';
echo 
"<BR>";
echo 
$user_array["Description"];
echo 
"<BR>";
echo 
'<a href="';
echo 
$user_array["URL"];
echo 
'">';
echo 
$user_array["URL"];
echo 
'</a>';
echo 
"<P>";
$i++;
}
mysql_close ($connect);

?>






<H1>Engineers</H1>
<?php
$connect 
mysql_connect("localhost","ID","PW");

$db mysql_select_db("industrialodorcontrol",$connect);

$sql "SELECT * FROM links WHERE category = 'Engineers'";

$result mysql_query($sql);


$num=mysql_numrows($result);
while (
$i $num) {
$user_array = @mysql_fetch_array($result);

echo 
'<B>';
echo 
$user_array["Name"];
echo 
'</B>';
echo 
"<BR>";
echo 
$user_array["Description"];
echo 
"<BR>";
echo 
'<a href="';
echo 
$user_array["URL"];
echo 
'">';
echo 
$user_array["URL"];
echo 
'</a>';
echo 
"<P>";
$i++;
}
mysql_close ($connect);
?>



<H1>Plumbing Related</H1>
<?php
$connect 
mysql_connect("localhost","ID","PW");

$db mysql_select_db("industrialodorcontrol",$connect);

$sql "SELECT * FROM links WHERE category = 'Plumbing Related'";

$result mysql_query($sql);


$num=mysql_numrows($result);
while (
$i $num) {
$user_array = @mysql_fetch_array($result);

echo 
'<B>';
echo 
$user_array["Name"];
echo 
'</B>';
echo 
"<BR>";
echo 
$user_array["Description"];
echo 
"<BR>";
echo 
'<a href="';
echo 
$user_array["URL"];
echo 
'">';
echo 
$user_array["URL"];
echo 
'</a>';
echo 
"<P>";
$i++;
}
mysql_close ($connect);
?>


Does anyone have any idea why the 2nd and 3rd iteration of my request are not processing? There are records in there with those categories.

Thanks in advance for your help.

Andy

Reply With Quote
  #2  
Old February 14th, 2007, 03:47 PM
CHornJr's Avatar
CHornJr CHornJr is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Location: New York City
Posts: 233 CHornJr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 50 m 19 sec
Reputation Power: 6
Send a message via AIM to CHornJr Send a message via MSN to CHornJr Send a message via Yahoo to CHornJr
you may want to try:

PHP Code:
while($user_array=@mysql_fetch_array[$result
__________________
CHornJr
"One day I'll know what I am doing"
My Blog
Suanhacky Lodge #49
Rebel Squadrons

Reply With Quote
  #3  
Old February 14th, 2007, 04:13 PM
mcgibbon mcgibbon is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2007
Posts: 7 mcgibbon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 30 m 42 sec
Reputation Power: 0
Could you please be more specific in how you are suggesting I apply this? Thank you so much for your time.

Andy

Quote:
Originally Posted by CHornJr
you may want to try:

PHP Code:
while($user_array=@mysql_fetch_array[$result

Reply With Quote
  #4  
Old February 14th, 2007, 10:36 PM
CHornJr's Avatar
CHornJr CHornJr is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Location: New York City
Posts: 233 CHornJr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 50 m 19 sec
Reputation Power: 6
Send a message via AIM to CHornJr Send a message via MSN to CHornJr Send a message via Yahoo to CHornJr
basically your dropping the $num=mysql_num_rows($result), $i<$num and the @ symbol from in front of the array creation function

PHP Code:
<H1>Plumbing Related</H1>
<?
php
$connect 
mysql_connect("localhost","ID","PW");
$db mysql_select_db("industrialodorcontrol",$connect);
$sql "SELECT * FROM links WHERE category = 'Plumbing Related'";
$result mysql_query($sql);

while (
$user_array=mysql_fetch_array($result)) {
echo 
"<b>".$user_array['name']."</b><br />".$user_array['description']."<br /><a href=\"".$user_array["URL"].">".$user_array["URL"]."</a>";
}
mysql_close ($connect);
?> 


Also, double check the spelling of the column names compared to how you have them when you call them in the variables. I know I have many times misspelled in one or the other, or capitalized when I didn't want to

Three more suggestions
  • Since you are using name associations already you may want to consider using the mysql_fetch_assoc($review) instead. fetch_array creates both user_array['col_name'] and user_array[0]. Fetch_assoc only gives you the column name. Its 50% less work for PHP to do if you are using only the named arrays.
  • Why are you closing the connection to the database? Keeping it open normally makes the website work a little quicker. Yes closing it will make it a little more secure, but normally not enough to be really useful
  • You should consider making the results into a function because there is no reason to have them three times separately since it is exactly the same thing all three times

Reply With Quote
  #5  
Old February 15th, 2007, 07:55 AM
mcgibbon mcgibbon is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2007
Posts: 7 mcgibbon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 30 m 42 sec
Reputation Power: 0
OK, I have tried using the mysql_fetch_assoc function and my code looks like this:

PHP Code:
<?php 
$connect 
mysql_connect("localhost","industri","cFEX6vk8"); 
$db mysql_select_db("industrialodorcontrol",$connect); 
$sql "SELECT * FROM links WHERE category = 'Engineers'"
$result mysql_query($sql); 

while (
$user_array=mysql_fetch_assoc($result)) { 
echo 
"<b>".$user_array['Name']."</b><br>".$user_array['Description']."<br><a href=\"".$user_array["URL"].">".$user_array["URL"]."</a><BR><BR>"

mysql_close ($connect); 
?>


The problem is, on the last iteration it is not printing out the </a> tag and you can see how it is being displayed at:

(URL address blocked: See forum rules)/html/links3.php

Also, can you enlighten me as to how I would leave the connection open and also how I would put the results into a function.

Thanks for your help and patience. I am coming back to this out of necessity after a long time of not using it and it is a little frustrating.

Thanks,
Andy

Reply With Quote
  #6  
Old February 15th, 2007, 09:22 AM
mcgibbon mcgibbon is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2007
Posts: 7 mcgibbon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 30 m 42 sec
Reputation Power: 0
OK, found the source of the problem. Got it up and running perfectly now.

Thanks for your help. If you could answer the last couple of questions regarding connecting and putting the results into a function I would appreciate it.

Andy

Reply With Quote
  #7  
Old February 16th, 2007, 10:20 PM
CHornJr's Avatar
CHornJr CHornJr is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Location: New York City
Posts: 233 CHornJr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 50 m 19 sec
Reputation Power: 6
Send a message via AIM to CHornJr Send a message via MSN to CHornJr Send a message via Yahoo to CHornJr
I hope that isn't the actual username and password for the database, if it is I would recommend changing it now as well as any other passwords that are similar to it

to keep the connection open just erase the mysql_close() function

the next question is a PHP question so the mods may end up moving the thread

there are several ways to make it into a function (or class) but the simplest way would be:

PHP Code:
//function_file.php
$connect mysql_connect("localhost","UN","PWD");  
$db mysql_select_db("database",$connect);

function 
function_name($sql){
     
$result=mysql_query($sql);
     while (
$user_array=mysql_fetch_assoc($result)) {
     echo 
"<b>".$user_array['Name']."</b><br>".$user_array['Description']."<br><a href=\"".$user_array["URL"].">".$user_array["URL"]."</a><BR><BR>";  
}
//Closes loop 
}//Closes function

//Engineers.php
include (function_file.php);
$sql="SELECT * FROM links WHERE category = 'Engineers'";
function_name($sql);

//Plumbing.php
include (function_file.php);
$sql="SELECT * FROM links WHERE category = 'Plumbing Related'";
function_name($sql); 


There are some other ways of setting up the function, but using your example this seems to be the simplest way of setting it up.

Reply With Quote
  #8  
Old February 17th, 2007, 08:00 AM
mcgibbon mcgibbon is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2007
Posts: 7 mcgibbon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 30 m 42 sec
Reputation Power: 0
Thanks so much for your help on this. That indeed was my actual password and it has been changed.

Reply With Quote
  #9  
Old February 17th, 2007, 08:49 AM
mcgibbon mcgibbon is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2007
Posts: 7 mcgibbon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 30 m 42 sec
Reputation Power: 0
If you look at:
http://industrialodorcontrol.com/html/links2.php you will see that as I have set this up it is not working. I have the function at the top in a seperate file named function_file.php, is that correct? I then have put php tags around the rest. It is not working properly and I'm at a loss as to why. It appears to be looking for a file called function_filephp however there is not reference to that file here anywhere. Here is the code from my page:

[PHPNET]
<?php //Engineers.php
include (function_file.php);
$sql="SELECT * FROM links WHERE category = 'Engineers'";
function_name($sql);?>
</span></td>
</tr>
<tr>
<td colspan="2"><div align="left"><span class="style2"><a name="Wastewater" id="Wastewater"></a>Wastewater </span></div></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><span class="style1">
<?php //Wastewater.php
include (function_file.php);
$sql="SELECT * FROM links WHERE category = 'Wastewater'";
function_name($sql);
?>
</span></td>
</tr>
<tr>
<td colspan="2"><span class="style2"><a name="Contractors" id="Contractors"></a>Contractors</span></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><span class="style1">
<?php //Contractors.php
include (function_file.php);
$sql="SELECT * FROM links WHERE category = 'Contractors'";
function_name($sql);
?>
</span></td>
</tr>
<tr>
<td colspan="2"><div align="left"><span class="style2">Buyer's Guides</span></div></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><span class="style1">
<?php //Buyers Guides.php
include (function_file.php);
$sql="SELECT * FROM links WHERE category = 'Buyers Guides'";
function_name($sql);?>
</span></td>
</tr>
<tr>
<td colspan="2" class="style2">General</td>
</tr>
<tr>
<td>&nbsp;</td>
<td><span class="style1">
<?php //General.php
include (function_file.php);
$sql="SELECT * FROM links WHERE category = 'General'";
function_name($sql);?>
</span></td>
</tr>
<tr>
<td colspan="2"><div align="left"><span class="style2">Plumbing Related</span></div></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><span class="style1">
<?php //Plumbing Related.php
include (function_file.php);
$sql="SELECT * FROM links WHERE category = 'Plumbing'";
function_name($sql);?>
</span></td>
</tr>

<tr>
<td colspan="2"><div align="left"><span class="style2">Plumbing Contractors </span></div></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><span class="style1">
<?php //Plumbing Contractors.php
include (function_file.php);
$sql="SELECT * FROM links WHERE category = 'Plumbing Contractors'";
function_name($sql);?>[/PHPNET]

Reply With Quote
  #10  
Old February 17th, 2007, 09:59 AM
Humanetainit's Avatar
Humanetainit Humanetainit is offline
Beyond help
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2007
Location: The Netherlands
Posts: 223 Humanetainit User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 1 h 32 m 30 sec
Reputation Power: 2
1. Looks like you need to add quotes:
Code:
include ('function_file.php');

2. You only need to include the file once, that the nifty thing about functions: it reduces double code.
3. Did you use <?php ?> tags in the include? You must, otherwise it will be handled as plain text and not parsed.
4. A lot more...

Do you want to toy with it yourself or do you want us to rewrite it?
__________________
One World... Humanetainit | Program secure

Reply With Quote
  #11  
Old February 17th, 2007, 01:11 PM
CHornJr's Avatar
CHornJr CHornJr is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Location: New York City
Posts: 233 CHornJr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 50 m 19 sec
Reputation Power: 6
Send a message via AIM to CHornJr Send a message via MSN to CHornJr Send a message via Yahoo to CHornJr
Quote:
Warning: main(function_filephp): failed to open stream: No such file or directory in /home/sites/site78/html/html/links2.php on line 32


PHP Code:
<?php //Engineers.php 
include ('function_file.php'); 
$sql="SELECT * FROM links WHERE category = 'Engineers'"
function_name($sql);?>


Don't forget in the function file to put at the very top <?php and at the very end ?>

Reply With Quote
  #12  
Old February 17th, 2007, 03:40 PM
mcgibbon mcgibbon is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2007
Posts: 7 mcgibbon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 30 m 42 sec
Reputation Power: 0
OK, I have added the quotes and am still getting errors. I do have the <PHP tags in the include.

The current errors I'm getting are:

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/sites/site78/html/html/function_file.php on line 16

and

Fatal error: Cannot redeclare function_name() (previously declared in /home/sites/site78/html/html/function_file.php:14) in /home/sites/site78/html/html/function_file.php on line 14

Thank you everyone for all your help on this. It is most appreciated. My skills are rusty