Page 1 of 1

really confused ?

Posted: Fri Apr 24, 2009 11:32 am
by nicademus
I have been trying to figure out why I cannot use my result form a query in a while loop. I posted a similar problem solved it and tried to apply the solution to this problem but t won't work. I have used the same syntax in all instances and each has given me a different solution.

This code works:

Code: Select all

 
<?php  mysql_connect($hostname,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT link, text FROM $collection WHERE co_name='Playful Promises' ORDER BY text ASC";
$result=mysql_query($query);
 
 while ($row = mysql_fetch_array($result)) 
  
   { 
    $link = $row[0];
    $text = $row[1];
     ?> 
     
     <li><a href="<?php echo $link ?>"><?php echo $text ?></a></li> 
   
   <?php
   } 
   ?>
 
This code does not:

Code: Select all

 
<?php
require 'scripts/db-data.php';
$con = mysql_connect($hostname, $username, $password);
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
 
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT co_name,city,coutry,home_link,blog_link,h2_img,logo_large FROM main_info WHERE co_name='Playful Promises'";
$result = mysql_query($query);
while ( $row = mysql_fetch_array($result, MYSQL_BOTH) )
 { 
  $co_name = $row[0];
  $city = $row[1];
  $country = $row[2];
  $homelink = $row[3];
  $bloglink = $row[4];
  $h2_img = $row[5];
  $logo = $row[6];
  
 
?>
 
 
 
<h1><img src="<?php echo $h2_img ?>" /></h1>
</div><!-- end introduction div -->
<div id="company_content">
<div id="company_content_top">
<img src="<?php echo $logo ?>" />
<ul>
   <li><?php echo $city ?> <?php echo $country ?></li>
   <li><a href="<?php echo $home_link ?>"><?php echo $co_name ?> Home</a></li>
   <li><a href="<?php echo $blog_link ?>"><?php echo $co_name ?> Blog</a></li>
</ul> 
<?php
}
?>
 
I am really confused as the second bit of code produces a "mysql_fetch_array() is not a valid result resource" and it is written just as the first bit of code is. What am I missing? Why does the first code work and not the second? Is this a glitch?

Please Help!
Thanx!

Re: really confused ??????????

Posted: Fri Apr 24, 2009 11:38 am
by liljester
is your database query returning rows?

you can use this to see, put it just above your while loop.

Code: Select all

print mysql_num_rows($result);

Re: really confused ??????????

Posted: Fri May 01, 2009 2:35 pm
by nicademus
NO, the database is not returning any rows. Why would that be happening?

Re: really confused ?

Posted: Fri May 01, 2009 2:45 pm
by Benjamin
Please use descriptive subjects related to the issue you are facing.