0
<?php $theplayer = ruthbabe01; ?>
<?php
    mysql_connect("server", "user", "password");
    mysql_select_db("database");

    $sql = mysql_query("SELECT playerID,games FROM HittingStreaks WHERE playerID = '$theplayer' ORDER BY games DESC ");

    echo "<h3>Hitting Streaks</h3>";

    while($row = mysql_fetch_array($sql)){
      $playerID = $row['playerID'];
      $games = $row['games'];

    echo "$playerID ... $games<br />";

}
?>

A simple little SELECT query, but I DO NOT want to see the echo "<h3>Hitting Streaks</h3>";or the other echo "$playerID ... $games<br />"; if there is not a record that matches the query. In other words, if that player ($theplayer) does not have a record in the table, do nothing. Don't echo any HTML.

Any help would be greatly appreciated.

Phil
  • 157,677
  • 23
  • 242
  • 245
BooneDig
  • 3
  • 1
  • Simple method: CSS. Add a class `.hidden { display:none; }` quick and dirty. – Funk Forty Niner Feb 28 '14 at 04:06
  • Please read [Why shouldn't I use mysql_* functions in PHP?](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – Phil Feb 28 '14 at 04:15
  • @Fred-ii --- I think you misunderstood my question, but I appreciate you taking the time to answer. I don't want to hide it all the time, only if the table does not have a result. – BooneDig Feb 28 '14 at 04:30
  • Seems like I misunderstood it; my bad ;-) (and you're welcome). Maybe some of the answers given will be of help. – Funk Forty Niner Feb 28 '14 at 04:31

3 Answers3

0

Switching to MySQLi because I'm not going to recommend a solution using a deprecated library, simply use the mysqli_stmt::$num_rows property. You should also use a prepared statement and bind your $theplayer value as a parameter. For example...

<?php
// remove these two lines once you've finished development
ini_set('display_errors', 'On');
error_reporting(E_ALL);

$theplayer = 'ruthbabe01';

$con = new mysqli('server', 'user', 'password', 'database');
if ($con->errno) {
    throw new Exception($con->error, $con->errno);
}
$con->set_charset('utf8');

$stmt = $con->prepare('SELECT playerID, games FROM HittingStreaks WHERE playerID = ? ORDER BY games DESC');
if (!$stmt) {
    throw new Exception($con->error, $con->errno);
}

$stmt->bind_param('s', $theplayer); // FYI VARCHAR keys are a terrible idea

if (!$stmt->execute()) {
    throw new Exception($stmt->error, $stmt->errno);
}

// need to call this to buffer the result in order to count the rows
$stmt->store_result(); 

$stmt->bind_result($playerID, $games);

if ($stmt->num_rows) : ?>

    <h3>Hitting Streaks</h3>

    <?php while ($stmt->fetch()) : ?>
        <?= htmlspecialchars($playerID) ?> ... <?= htmlspecialchars($games) ?><br>
    <?php endwhile ?>

<?php endif ?>
Phil
  • 157,677
  • 23
  • 242
  • 245
  • I can't vote, as my reputation isn't high enough. I tried this code on my server and I get nothing ... an empty result whether I try a query where I know there should be results or a query where I know there shouldn't be. – BooneDig Feb 28 '14 at 04:28
  • @user3363231 Sounds like you've got errors hidden. See my updated answer. I'm curious to hear what didn't work here so if you could try it out, that would be great (and you wouldn't be using a deprecated library) – Phil Feb 28 '14 at 04:33
0

USE BELOW CODE :

<?php
  mysql_connect("server", "user", "password");
  mysql_select_db("database");
  $sql = mysql_query("SELECT playerID,games FROM HittingStreaks WHERE playerID = '$theplayer' ORDER BY games DESC ");
  $rows = mysql_num_rows($sql);
  if($rows>0) {
     echo "<h3>Hitting Streaks</h3>";
     while($row = mysql_fetch_array($sql)){
        $playerID = $row['playerID'];
        $games = $row['games'];
        echo "$playerID ... $games<br />";
     }
  }
?>
Sushil Kandola
  • 870
  • 2
  • 9
  • 22
  • The mysql extension is deprecated. This will trigger `E_DEPRECATED` level errors – Phil Feb 28 '14 at 04:23
  • @Phil. Its not deprecated completely. Its optional. – Sushil Kandola Feb 28 '14 at 04:29
  • @SushilKandola Are you kidding me?! See any `mysql_*` manual page - *"This extension is deprecated as of PHP 5.5.0, and will be removed in the future."*. Also, that's a real nice SQL injection vulnerability you've got there – Phil Feb 28 '14 at 04:34
  • Ahhhh, I checked that. Its deprecated in 5.5, I am currently using 5.3, that's why its working :) – Sushil Kandola Feb 28 '14 at 04:37
  • @SushilKandola Of course deprecated code *works*. Depreation just means that the library is old, unmaintained, subject to many flaws and will most likely be removed in an upcoming release. Recommending somebody use deprecated code (and in such an unsafe way) is a terrible thing to do – Phil Feb 28 '14 at 04:38
  • Yeah right. Even I came to know the new thing here with you, Thanks @Phil – Sushil Kandola Feb 28 '14 at 04:41
  • Why do people keep referring to this issue as a *new thing*? Have you all been living under a rock for the last 10 years? Because that's how long this has been an issue. PHP has advised developers to **not** use the mysql extension for at least that long – Phil Feb 28 '14 at 04:42
  • Mr. Phil, peoples has different habits. Some may be under rock and will do something new, when they are in need. I am same kind of person. – Sushil Kandola Feb 28 '14 at 04:49
-1

Simplly you can try this if do not want to get the row_num etc etc..

$needToPrint=true;
while($row = mysql_fetch_array($sql)){
  if($needToPrint)
  {
    echo "<h3>Hitting Streaks</h3>";
    $needToPrint = false;
  }
  $playerID = $row['playerID'];
  $games = $row['games'];

  echo "$playerID ... $games<br />";
}
Deepak Sharma
  • 4,124
  • 1
  • 14
  • 31