3

I have a database table (ff_projections) that contains the following fields:

ID  Player  Position    Team    Pass_Yds    Pass_TDs    Int_Thrown  Rush_Yds    Rush_TDs    Rec_Yds Rec_TDs Receptions  Fumbles Extra_Pts   FG  Sacks   Int_Caught  Def_TD  ST_TD   Shutouts    Overall_Pts Total_Fantasy_Pts

What I want is to display all rows where Position = QB. Only certain fields would appear in the rows though.

Like this:

SELECT Player, Team, Pass_Yds, Pass_TDs, Int_Thrown, Rush_Yds, Rush_TDs, Overall_Pts, Total_Fantasy_Pts  FROM ff_projections WHERE Position = 'QB';

and then display the results in a table on the web page.

user229044
  • 232,980
  • 40
  • 330
  • 338
Cynthia
  • 5,273
  • 13
  • 42
  • 71
  • it would be helpful to post a couple sample rows, what you think it should be showing up and what it is actually showing up. – phpmeh May 18 '12 at 05:04
  • 1
    @phpmeh My interpretation is that the query is fine, but the OP doesn't know how to execute the query and output the data in PHP. – Michael Mior May 18 '12 at 05:10
  • Michael - that is it exactly :) I generally know the logic of what I want to do, but not necessarily the correct syntax needed to execute it. – Cynthia May 18 '12 at 05:39
  • Here is a more general solution, that will print the result of any query: http://stackoverflow.com/questions/2970936/how-to-echo-out-table-rows-from-the-db-php – ToolmakerSteve Jul 15 '15 at 21:57

1 Answers1

9
<?php
$con = mysql_connect("localhost","user","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("database", $con);

$result = mysql_query("SELECT Player, Team, Pass_Yds, Pass_TDs, Int_Thrown, Rush_Yds, Rush_TDs, Overall_Pts, Total_Fantasy_Pts FROM ff_projections WHERE Position = 'QB' ORDER BY Pass_Yds DESC;");

while($row = mysql_fetch_array($result))
  {
  echo $row['Player'];
  echo $row['Team'];
  ....
  }

mysql_close($con);
?>
Ahatius
  • 4,777
  • 11
  • 49
  • 79
  • This is exactly what I need! Now how would I establish a default SORT BY? For example, when those rows display, I want them to be in order of the highest # of passing yards (Pass_Yds) first. – Cynthia May 18 '12 at 05:37
  • I figured it out :) I added ORDER BY Pass_Yds DESC and it worked perfectly! – Cynthia May 18 '12 at 05:53
  • @user1255168 This example has a static filter value in it. Be careful if you pass trough user input (escape it properly). – Ahatius May 18 '12 at 06:04
  • FWIW, here is an example using newer `mysqli` interface: http://www.w3schools.com/php/php_mysql_select.asp It also shows a nicer output per row. – ToolmakerSteve Jul 15 '15 at 21:55
  • Here is a more general solution, that will print the result of any query: http://stackoverflow.com/questions/2970936/how-to-echo-out-table-rows-from-the-db-php – ToolmakerSteve Jul 15 '15 at 21:57