0

I have a table with par_id and columns (par1-5) 1 up to 5, but I'm trying to have my php function below to echo any number of columns

//-------------------------------------- get about ----------------------------------------------
function getAbout($option){
    $div = array();
    $sql_st = "undefined";

    if($option == "about")
        $sql_st = "SELECT * FROM  `about` where par_id='1'";

    // connect to database
    mysql_connect($_SESSION['dbSever'],$_SESSION['dbUser'],$_SESSION['dbPass']) or die(mysql_error());
    mysql_select_db($_SESSION['tblName']) or die(mysql_error());

    $result = mysql_query($sql_st) or die(mysql_error()."<br/>".$sql_st);
    $num_rows =  mysql_num_rows($result);

    while ($row = mysql_fetch_array($result) ){     
        // not sure what to do here
    }
    // disconnect
    mysql_close();

    return $div;
}
Roe
  • 425
  • 1
  • 7
  • 14
  • 1
    [**Please, don't use `mysql_*` functions in new code**](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://php.net/manual/en/mysqlinfo.api.choosing.php) will help you decide which. If you choose PDO, [here is a good tutorial](http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers). – misterManSam Aug 28 '14 at 13:01

2 Answers2

1

A really simple one would be to have a flag like so:

$skippedFirstRow = false;
while ($row = mysql_fetch_array($result)){     
    if(!$skippedFirstRow) {
    //do whatever you want here
    }
    $skippedFirstRow = true;
}

However, keep in mind that this is just a quick hack for what you want and considering your code, but it is not an elegant solution.

cgf
  • 3,369
  • 7
  • 45
  • 65
1

From your question title I'm assuming you have an ID maybe in your first column and you want to keep that from being printed. You could try something like:

$numberOfColumns = mysql_num_fields($result);
while ($row = mysql_fetch_array($result) ){     
     for ($i = 1; $i < $numberOfColumns; $i++){
         echo $row[$i];
     }
}

The $i iterator in the for loop is initiated with value of 1 because PHP handles (like many languages) arrays as zero-based, which means that the first element is referenced by $row[0] (as in this example) - by starting the loop at one and going for as many elements there are in the array, you're effectively grabbing all elements except for the first one.

Gabor
  • 550
  • 3
  • 6