Jump to content

[SOLVED] Using multiple field types


tgavin

Recommended Posts

I'm thinking of putting together a table that will contain template information. Since there's always the possibility of new things being added in the future, I don't want to create a seperate field for each item. Instead, I'll just add a new record for each item. Easy enough.

 

Now, I also don't want to have to have a varchar field for a record that really only needs a char field. Or a text field for 50 characters of text. So I've thought about putting different field types into the table, and then only using the appropriate field type for each record.

 

CREATE TABLE `templates` (
  `temp_name` varchar(20) NOT NULL default '',
  `temp_char` char(1) NOT NULL default '',
  `temp_varchar` varchar(255) NOT NULL default '',
  `temp_datestamp` datetime NOT NULL default '0000-00-00 00:00:00',
  PRIMARY KEY  (`temp_name`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

 

So, here's where my question lies. I'm still learning arrays. Looking at the code below, is their an easier way to do what I'm seeking?

 

$query = "SELECT temp_name,temp_char,temp_varchar FROM templates WHERE temp_name IN ('banner','link_txt')";
$sql = mysql_query($query,$conn) or die(mysql_error());
$row = array();
while($row = mysql_fetch_assoc($sql)) {
$row_char[$row['temp_name']] = $row['temp_char'];
$row_varchar[$row['temp_name']] = $row['temp_varchar'];
}

echo $row_char['banner']."<br />";
echo $row_varchar['link_txt'];

Link to comment
Share on other sites

Sure is

 

$query = "SELECT temp_name,temp_char,temp_varchar FROM templates WHERE temp_name IN ('banner','link_txt')";
$sql = mysql_query($query,$conn) or die(mysql_error());

while(list($temp_name, $temp_char, $temp_varchar) = mysql_fetch_row($sql)) {
  echo $temp_name . '<br />';
  echo $temp_char . '<br />';
  echo $temp_varchar . '<br />';
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.