PDA

View Full Version : Adding A Boolean To My Column Type



tomyknoker
04-13-2007, 10:52 AM
I need to add a column in my database, which will be called 'MemberPackSent' basically I want to send out a pack to members, just wondering would I make this a Boolean? Do I need to specify the 'Length' or is a Boolean simply always 1 or 0?

mburt
04-13-2007, 02:23 PM
Boolean can be 1 or 0, but you can make it "true" or "false" as well. I know in PHP true and false boolean tags are read as 1 and 0 anyways, if you try to echo them.
As long as you don't get too confused, 1 and 0 are fine.

tomyknoker
04-14-2007, 06:58 AM
Ahhhhh ok is there a way in php that if it was a '1' value I could convert it to 'Yes'?

mburt
04-14-2007, 01:16 PM
if ($value == 1) {
$value = "Yes";
} else {
$value = "No";
}

Twey
04-14-2007, 01:22 PM
$value = $value ? "Yes" : "No";

mburt
04-14-2007, 01:23 PM
Huh?
Shouldn't:

$value = $value ? : "Yes" : "No";
Be

$value = $value ? "Yes" : "No";

Twey
04-14-2007, 01:26 PM
Yes, sorry, typo. Corrected.

realmetrics.com
04-16-2007, 04:35 AM
Hi tomy,

Yes, sure you can assign the value using simple “if” condition before updating database.

Here is the basic code to do this..




<?php
$con = @mysql_connect(“Ip Address”, “username”, “password”) or die(mysql_error());

$db = @mysql_select_db(“emp”, $con) or die(mysql_error());

$packets = true;
If ($packets = true)
{
$Val = 1;
}
Else
{
$val = 0;
}

?>



After converting the value you can simply insert it into the database using SQL statements.

Twey
04-16-2007, 07:50 AM
Some versions of MySQL will accept "true" and "false" as keywords and convert them automatically.

$con = @mysql_connect(“Ip Address”, “username”, “password”) or die(mysql_error());Watch the "smart quotes" -- that won't even parse as PHP.

mburt
04-16-2007, 08:54 PM
Off the topic of connecting, but mysql_query allows it:

SELECT * FROM blah WHERE test=“foo”
so in PHP, you could do

mysql_query('SELECT * FROM blah WHERE test=“foo”');

tomyknoker
04-17-2007, 02:17 AM
Guys would ENUM be better for this?

Twey
04-17-2007, 06:15 PM
It would work, but like I said, several versions have it built in already.

tomyknoker
04-17-2007, 07:22 PM
Ok guys I got it too work for one page but for some reason can't get it to happen on this page... It outputs 'No' for all the users, even though 2 are definitely a 'Yes'... Any ideas?


<?php
if ($superuser == 1) {
$superuser = "Yes";
} else {
$superuser = "No";
}

/* connect to the mysql database and use different queries for the count of members */

include 'library/config.php';
include 'library/opendb.php';

if (isset($_POST['insert'])) {
// do any error checking here -> empty fields etc...
// assign basic variables
$firstname = $_POST['firstname'];
$username = $_POST['username'];
$lastname = $_POST['lastname'];
$password = scramble($_POST['password']);
$superuser = $_POST['superuser'];

// update
$sql = mysql_query("INSERT INTO `tbladministrators` (`id`,`firstname`,`lastname`,`username`,`password`,`superuser`) VALUES ('null','$firstname','$lastname','$username','$password','$superuser')") or die(mysql_error());

echo '<script type="text/javascript"> window.location.href=\''.$_SERVER["PHP_SELF"].'\';</script>';
}

else {

$info = mysql_query("SELECT * FROM tbladministrators");
?>

<form name="addAdmin" method="POST" action="<?php echo $_SERVER['PHP_SELF'];?>">

<script type="text/javascript">
function confDelete() {
if (confirm("Are you sure you want to delete this user from the database? This cannot be undone!")) {
return true;
}

else {
return false;
}
}
</script>

<?php
echo '<table class="main" width="700" border="0" cellspacing="0" cellpadding="0">
<thead>
<tr>
<th scope="col" class="twenty">LAST NAME</th>
<th scope="col" class="twenty">FIRST NAME</th>
<th scope="col" class="twenty">USERNAME</th>
<th scope="col" class="twenty">PASSWORD</th>
<th scope="col" class="twenty">SUPERUSER</th>
<th scope="col" class="twenty">&nbsp;</th>
<th scope="col" class="twenty">&nbsp;</th>
</tr>
</thead>';

if (mysql_num_rows($info) < 1) {
echo '<tr valign="top">
<td colspan="4">There are no members that match the query. Please go back and try again</td>
</tr>';
}

else {
while ($qry = mysql_fetch_array($info)) {
$i++;

//create the layout
?>

<tr valign="top" class="<?php echo $i &#37; 2 ? 'odd' : 'even'; ?>">
<td><?php echo $qry['firstname']; ?></td>
<td><?php echo $qry['lastname']; ?></td>
<td><?php echo $qry['username']; ?></td>
<td><?php echo unscramble($qry['password']); ?></td>
<td><?php echo $superuser;?></td>
<td><a href="showAdminEdit.php?record_id=<?php echo $qry['id']; ?>">Edit</a></td>
<td><a href="adminDelete.php?type=admin&admin_id=<?=$qry['id'];?>&location=showAdmin.php?id=<?=$qry['id'];?>" onclick="return confDelete();">Delete</a></td>
</tr>
<?php
}
}
?>
<tr valign="top" class="total">
<td><input type="text" name="firstname" /></td>
<td><input type="text" name="lastname" /></td>
<td><input type="text" name="username" /></td>
<td><input type="text" name="password" /></td>
<td><select name="superuser">
<option value="1">Yes</option>
<option value="0">No</option>
</select></td>
<td>&nbsp; </td>
<td><input type="hidden" name="insert" value="1"><a href="#" onClick="document.forms['addAdmin'].submit(); return false;">Insert</a></td>
</tr>