Hey guys i need to get the database column into a 1 mentionable array.

Here is my code.

$spreadsql = "SELECT `categories_id` FROM `".$TABLES['CATEGORIES_SPREAD']."` WHERE `products_id` = '".$_REQUEST['pid']."'";
$spreadquery = mysql_query($spreadsql, $connection) or die ('Could not execute SQL query:<br />'.$sql.'<br /><strong>'.mysql_error().'</strong>');
$spread = mysql_fetch_assoc($spreadquery);

$array = array();

foreach ($spread as $value) {
	$array[$x] = $value;	

<?php do { ?>
            <input name="category[]" type="checkbox" value="" <?php print (in_array(readFromDB($categories['categories_id']), $categoriesspread)) ? 'checked="checked"' : '';?> /><?php print readFromDB($categories['category_title']); ?>  | <?php print readFromDB($categories['categories_id']); ?><br />
<?php } while ($categories = mysql_fetch_assoc($catquery)); ?>

I wan to then do a check for if the categories id is in the array then check the check box.


Recommended Answers

All 3 Replies

I think this php method will help to sort out your problem

function getColoumn($table) {
     $result = mysql_query("SHOW COLUMNS FROM ". $table);
      if (!$result) {
        echo 'Could not run query: ' . mysql_error();
      if (mysql_num_rows($result) > 0) {
        while ($row = mysql_fetch_assoc($result)) {
          $fieldnames[] = $row['Field'];

      return $fieldnames;
Member Avatar

If you stop using deprecated code and start using mysqli or PDO, then this is easy. For PDO (from the manual):

$sth = $dbh->prepare("SELECT `categories_id` FROM `{$TABLES['CATEGORIES_SPREAD']}` WHERE `products_id` = ?");
$array = $sth->fetchAll(PDO::FETCH_COLUMN, 0);

Also using user input in the query is a big no-no. You are wide open to SQL Injection. For this reason, use a prepared statement.

It is not only that it is more logical or better , it is easier and faster coding as well , I can't understand why anyone would like to make its life harder implementing code from PHP past that was never safe (or had a secure layer at least) and was not easy (bad functional habbits).

Yes PHP is growing in OOP bad habbits as well but at least there is PDO out there that makes everything easy. (PDO has itself issues but nothing comparing to what we have read)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.