[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 230
  • Last Modified:

Displaying one result instead of two in a "simple" mysql query

The following query yields two results instead of one, how can I fix this? If you have any more hints they are welcom, thx

$link = mysql_connect("$mysqlhost", "$mysqluser", "$mysqlpass")
  or die ("Could not connect to MySQL");

mysql_select_db ($mysqldbase)
  or die ("Could not select database");

  //SELECT * FROM `filedownloads` WHERE filename='{$filename}'")
 $sql = 'SELECT downloads FROM `filedownloads` WHERE id = 1 LIMIT 0, 30 ';
 $result = mysql_query ($sql);

// printing HTML result

print "<table>\n";
while ($line = mysql_fetch_array($result)) {
  print "\t<tr>\n";
  while(list($col_name, $col_value) = each($line)) {
    print "\t\t<td>$col_value</td>\n";
  }
  print "\t</tr>\n";
}
print "</table>\n";

  mysql_close($link);
0
PeterdeB
Asked:
PeterdeB
1 Solution
 
nizsmoDeveloperCommented:
Normally I use echo, so I have changed your prints to echos, but there should be no difference. Apparently echos give a slight performance advantage as they say, I'm not sure.
Is your inner while loop necessary? how bout something like this?



$link = mysql_connect("$mysqlhost", "$mysqluser", "$mysqlpass")
  or die ("Could not connect to MySQL");

mysql_select_db ($mysqldbase)
  or die ("Could not select database");

  //SELECT * FROM `filedownloads` WHERE filename='{$filename}'")
 $sql = 'SELECT downloads FROM `filedownloads` WHERE id = 1 LIMIT 0, 30 ';
 $result = mysql_query ($sql);

// printing HTML result

echo "<table>\n";
while ($line = mysql_fetch_array($result)) {
  echo "\t<tr>\n";
  echo $line[0];
  echo "\t</tr>\n";
}
echo "</table>\n";

  mysql_close($link);
0
 
jericotolentinoCommented:
nizsmo's solution is actually simpler, but should you still like to use print, here's something you can just add to it. I've included the <td></td> tags in the script below.
--------------------
$link = mysql_connect("$mysqlhost", "$mysqluser", "$mysqlpass") or die ("Could not connect to MySQL");

mysql_select_db ($mysqldbase) or die ("Could not select database");

//SELECT * FROM `filedownloads` WHERE filename='{$filename}'")
 $sql = 'SELECT downloads FROM `filedownloads` WHERE id = 1 LIMIT 0, 30 ';
 $result = mysql_query ($sql);

// printing HTML result

print "<table>\n";
while ($line = mysql_fetch_array($result)) {
  print "<tr>";
  print "\t\t<td>".$line['downloads']."</td>\n";
  print "\t</tr>\n";
}
print "</table>\n";

mysql_close($link);
0
 
MasonWolfCommented:
Change this:
 $sql = 'SELECT downloads FROM `filedownloads` WHERE id = 1 LIMIT 0, 30 ';

To this:
 $sql = 'SELECT downloads FROM `filedownloads` WHERE id = 1 LIMIT 0, 1';
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
PeterdeBAuthor Commented:
nizsmo: thank you, you have made an end to many hours of hard thinking
0
 
PeterdeBAuthor Commented:
jericotolentino: thank you too for your help it is greatly appreciated
0
 
PeterdeBAuthor Commented:
MasonWolf: wow, thank you very much too
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now