Page 1 of 1

logic related question

Posted: Wed Jun 21, 2006 7:39 pm
by Milan
i have defined a database connection

Code: Select all

mysql_select_db($database_pickageek, $pickageek);
$query_Recordset3 = sprintf("SELECT NDA FROM awapr WHERE `session` = '%s'", $colname_Recordset3);
$Recordset3 = mysql_query($query_Recordset3, $pickageek) or die(mysql_error());
$row_Recordset3 = mysql_fetch_assoc($Recordset3);
$totalRows_Recordset3 = mysql_num_rows($Recordset3);
but when i try this :

Code: Select all

$nnda= $row_Recordset3['NDA'];
echo $nnda;
i get a blank field.

My question is: Is it posible that the value of the $row_Recordset3['NDA'] is populated on page load and not verified after that, or PHP will check ig the value has been updated every time i call $row_Recordset3['NDA']

?

Posted: Wed Jun 21, 2006 7:41 pm
by Weirdan
or PHP will check ig the value has been updated every time i call $row_Recordset3['NDA']
it will not

Posted: Wed Jun 21, 2006 7:43 pm
by Milan
thanks, that explained a lot.

Do you have any idea why it returns a blank value?

thanks.

Posted: Wed Jun 21, 2006 7:51 pm
by Weirdan
It could be because query returned no records at all. Try to check $totalRows_Recordset3 before you attempt to use mysql_fetch_assoc (though mysql_fetch_assoc should have complained if you attempted to fetch a row from empty result set).

shorter

Posted: Wed Jun 21, 2006 7:55 pm
by Milan
Wnen i enter a hard coded value it works. weird.

In short, this is what i am trying to do, i have a field "neednda" in the table, and i need to display a HTML section if the value is 1 and hide it if the value is 0.

Something is wrong in the code i have:

Code: Select all

if ($row_Recordset3['NDA']!='0')
{
print "<table width='450' border='0' cellspacing='0' cellpadding='0'><tr><td width='305' valign='top'><span class='title1'>Attach NDA ! </span><span class='text3'></table>";
}
else
{
}
Is there an alternative way to do this?

Also can someone recomend a good MySQL book?

Posted: Wed Jun 21, 2006 8:01 pm
by Weirdan
Is there an alternative way to do this?
For debugging purposes I would recommend to use var_dump($row_Recordset3);
This way you would see entire record contents (which will help you to spot logical flaws in your script).
Also can someone recomend a good MySQL book?
Nothing short of their online manual and The Mighty Google :)

Posted: Wed Jun 21, 2006 8:03 pm
by Milan
Nothing short of their online manual and The Mighty Google Smile
True but i read on the bus :)

Re: shorter

Posted: Wed Jun 21, 2006 9:42 pm
by bdlang
Milan wrote: In short, this is what i am trying to do, i have a field "neednda" in the table, and i need to display a HTML section if the value is 1 and hide it if the value is 0.

Something is wrong in the code i have:

Code: Select all

if ($row_Recordset3['NDA']!='0')
{
print "<table width='450' border='0' cellspacing='0' cellpadding='0'><tr><td width='305' valign='top'><span class='title1'>Attach NDA ! </span><span class='text3'></table>";
}
else
{
}
Is there an alternative way to do this?
Well, as Weirdan mentioned, you might want to look at your data prior to trying script logic. One question I have, what type of field is this? Is it a BOOL or TINYINT type field with a 0 or 1 switch? Or is it a VARCHAR type field? Or ENUM? Once you decide specifically what data belongs in the field, then you can more easily make the comparison.

I have found it useful to use NULL field values whenever applicable. This should be your comparison for an empty field. For example, you define a table as such:

Code: Select all

CREATE TABLE IF NOT EXISTS `Users` (
UserID INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
UserName VARCHAR(50) NOT NULL,
Bio TEXT NULL,
UNIQUE (UserName)
);
Now, the first field `UserName` is defined as being 'NOT NULL', or in layman's terms, it has to have a value when you perform an INSERT on the table. The other field, `Bio` is defined as NULL, and can be NULL when performing an INSERT. Essentially, if the field is NULL, it means 'the Bio data is unknown', vs 'the Bio data is 0 or an empty string'. It literally has no value. This isn't the best explanation, but hopefully you get the gist. You can then use the PHP function is_null() on the resultset data to definitively test for NULL values.

Some further reading:
MySQL Manual: 'CREATE TABLE syntax'
MySQL Manual: 'NULL Values'
MySQL Manual: 'Working with NULL values'
MySQL Manual: 'Problems with NULL Values'


Milan wrote: Also can someone recomend a good MySQL book?
I have found the Paul Dubois book 'MySQL' (SAMS, ISBN: 0672326736) to be excellent (although I don't own it, I do own his O'Reilly book, 'MySQL Cookbook'). The other book I might look at is by Welling & Thomson, well known for their writing in the field, 'MySQL Tutorial' (SAMS, ISBN: 0672325845).

Other than that, the MySQL manual online is wonderful, and I it would appear as though the print edition of the online manual has been condensed into several books, the abovementioned 'MySQL Tutorial' one of them.

Posted: Thu Jun 22, 2006 12:20 pm
by Milan
thanks for all your help!