0

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;	
	$x++;
}

<?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.

Thanks,
Marais

4
Contributors
3
Replies
11
Views
6 Years
Discussion Span
Last Post by jkon
0

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();
      }
      $fieldnames=array();
      if (mysql_num_rows($result) > 0) {
        while ($row = mysql_fetch_assoc($result)) {
          $fieldnames[] = $row['Field'];
        }
      }

      return $fieldnames;
}

Edited by parvez alam

1

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` = ?");
$sth->execute([$_REQUEST['pid']]);
$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.

0

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)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.