Jump to content

[SOLVED] Unexpected Output using "while ($row = mysql_fetch_array($result, MYSQL_ASSOC))"


Recommended Posts

Hi,

 

Thank you for reading.

 

I have a php script that I'm using that has

been used before on a different site, but now,

when attempting to use the following code,

it is not displaying the data that I expect for

it to.

<?php

	// connecttodb($servername,$dbname,$dbuser,$dbpassword)
	$query="SELECT * FROM directory WHERE active='1' 
	AND location='L' AND type='paid' ORDER BY sortorder ASC";
	$result = mysql_query ($query) or die(mysql_error());

	$num=mysql_num_rows($result);

	// mysql_close();


	$id=mysql_result($result,$i,"id");
	$title=mysql_result($result,$i,"title");
	$url=mysql_result($result,$i,"url");
	$sortorder=mysql_result($result,$i,"sortorder");
	$type=mysql_result($result,$i,"type");
	$location=mysql_result($result,$i,"location");
	$class=mysql_result($result,$i,"class");
	$active=mysql_result($result,$i,"active");

	while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
		echo "<b>$class<br><a href=\"$url\">$title</a><hr>";
	}
	mysql_free_result($result);

	mysql_close();

?> 

 

The above displays 1 record when there should be 2.

 

The code below displays 12 rows of the same record

when there should be 12 different records displayed:

<p><h3>Available</h3><hr>
<?php

	connecttodb($servername,$dbname,$dbuser,$dbpassword);

	$query="SELECT * FROM directory WHERE active='1'
	AND location='L' AND type='order' ORDER BY sortorder ASC";
	// $result=mysql_query($query);
	$result = mysql_query ($query) or die(mysql_error());

	$num=mysql_num_rows($result);


	$id=mysql_result($result,$i,"id");
	$title=mysql_result($result,$i,"title");
	$url=mysql_result($result,$i,"url");
	$sortorder=mysql_result($result,$i,"sortorder");
	$type=mysql_result($result,$i,"type");
	$location=mysql_result($result,$i,"location");
	$class=mysql_result($result,$i,"class");
	$active=mysql_result($result,$i,"active");

	while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
		echo "<b>$class<br><a href=\"$url\">$title</a><br>";
	}

	mysql_close();

?> 

 

So, I'm guessing that either:

a.  The "while" statement is in the wrong place

(of which, I don't think so because this is how it is used on a different site)

OR

b.  There is nothing to count and then display the next record, but, again,

(I don't think so because this is how it is used on a different site)

 

So, what am I missing here?

 

Any Ideas?

 

Thank you in advance for your help.

 

Thanks,

FirstBorn

 

MySQL Ver:

5.0.51a-community

 

phpMyAdmin:

2.11.9.1

MySQL client version: 4.1.22

 

the code should be:

 

   <?php

      // connecttodb($servername,$dbname,$dbuser,$dbpassword)
      $query="SELECT * FROM directory WHERE active='1' 
      AND location='L' AND type='paid' ORDER BY sortorder ASC";
      $result = mysql_query ($query) or die(mysql_error());
      
      while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
         echo "<b>{$row['class']}<br><a href=\"{$row['url']}\">{$row['title']}</a><hr>";
      }
      mysql_free_result($result);

      mysql_close();

   ?> 

the code should be:

 

   <?php

      // connecttodb($servername,$dbname,$dbuser,$dbpassword)
      $query="SELECT * FROM directory WHERE active='1' 
      AND location='L' AND type='paid' ORDER BY sortorder ASC";
      $result = mysql_query ($query) or die(mysql_error());
      
      while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
         echo "<b>{$row['class']}<br><a href=\"{$row['url']}\">{$row['title']}</a><hr>";
      }
      mysql_free_result($result);

      mysql_close();

   ?> 

 

Dear Aaron,

 

Thank you VERY MUCH!!!

 

That worked!

 

How do I get this Thread marked as 'resolved?'

 

Thanks,

Christopher

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.