Jump to content

RedRocky

Members
  • Posts

    13
  • Joined

  • Last visited

Profile Information

  • Gender
    Not Telling

RedRocky's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. Should this line be: $approved = ($value == 1) ? true : false;
  2. The data type is boolean, which in MySQL administrator is tinyint(1). The values stored in it are 1 for true and 0 for false. PFMaBiSmAd, the query isn't failing, but if mysql_result can return false then don't I want to make sure I can tell the difference between false for the field value and false in case the query fails?
  3. What is the correct way to check for true or false when returning a value from a boolean field using mysql_result? I have been using the following to check for false: if (mysql_result($MyDataset,0,'APPROVED') == 0) However, mysql_result returns FALSE if the function fails, which would also be equal to 0 in the above code. Is the correct way to check for false in the boolean field as follows: if (mysql_result($MyDataset,0,'APPROVED') === '0') and the correct way to check for true as follows: if (mysql_result($MyDataset,0,'APPROVED') === '1') Thanks in advance for your help.
  4. I wish to use the code from the following page that can be used to check if an uploaded image is an animated GIF: http://stackoverflow.com/questions/280658/can-i-detect-animated-gifs-using-php-and-gd Here is the function: function is_ani($filename) { if(!($fh = @fopen($filename, 'rb'))) return false; $count = 0; //an animated gif contains multiple "frames", with each frame having a //header made up of: // * a static 4-byte sequence (\x00\x21\xF9\x04) // * 4 variable bytes // * a static 2-byte sequence (\x00\x2C) // We read through the file til we reach the end of the file, or we've found // at least 2 frame headers while(!feof($fh) && $count < 2) $chunk = fread($fh, 1024 * 100); //read 100kb at a time $count += preg_match_all('#\x00\x21\xF9\x04.{4}\x00\x2C#s', $chunk, $matches); fclose($fh); return $count > 1; } Am I right in saying that there are curly brackets missing after the while line? The indenting indicates that both of the 2 following lines should be included in the while loop, but the lack of curly brackets mean that only the line beginning with $chunk is included in the loop. The code actually seems to work with and without the curly brackets. Does anyone understand this code well enough to know whether or not the curly brackets should be included? Thanks in advance.
  5. What is the best way to display the next record on a web page, from a list of records, without executing the query over and over again each time you display the next record? For example, imagine my user has a list of messages displayed on a web page, and the user filters that list to display all messages with the title "Test", and let's say that returns 12 records. He then clicks on record 1 to read that message. Once he is done reading it, he then wants to read msg 2, and then msg 3 etc. He could hit the Back button in his browser and click on the next record from the list, but it would be more useful to just click on a link that says "Next" which displays the next message in the filtered list of messages (without having to go back to the list). A lot of web sites do this sort of thing, but what is the best way to do it? Do you have to run the original query again each time a new message is loaded, so you know which message is next, or can you somehow store the query to save the server from having to run the same query over and over again? If a user is viewing a list of pictures for example, they could be flicking through the pictures quite quickly, and I would not like to have the server running the same query over and over again every few seconds when it doesn't need to. Advice? (Thanks.)
  6. What I have done is to create queries that check each table that has a foreign key for the field in question. I then join those queries using UNION, and do a left outer join with the table containing the primary key. I am hoping that this solution will not be too inefficient.
  7. Hi Mchl, why are you pointing me here? Any comments on my question? Thanks.
  8. Hey thanks for the replies. I'm not sure you fully understand what I am trying to do however. I know the point of FKs, but my issue is that I want the user to know whether a record can be deleted or not, before they attempt to delete it. If a record cannot be deleted, I want to remove the option to delete it. If I have a list of records, I only want to give the option to delete each record IF that record can be deleted. If the record cannot be deleted, I would rather display something like "in use" instead of a delete button or delete checkbox. A solution that I have been made aware of would be to use left outer joins to the tables that contain the foreign keys. In my system however, there could be 10 or so foreign key fields in various tables that I need to check, so I am not sure how efficient this would be. Here is example code of how this would work: select p2.person_id, p2.surname, p2.count1, count(table2_id) as count2 from (select p1.person_id, p1.surname, count(table1_id) as count1 from people1 p1 left outer join table1 t1 on p1.person_id = t1.person_id group by p1.person_id) p2 left outer join table2 t2 on p2.person_id = t2.person_id group by p2.person_id Any suggestions on how I can make this SQL better? Thanks.
  9. Is there a way, when you have a list of records, to check if each of these records have foreign key references before you attempt to delete any of these records? As an example, if I have a list of borrowers and a list of books, you should not be able to delete a borrower from the system if he still has books on loan. (My actual system is much more complicated than that however - a lot more tables.) I would like to remove the delete option from any borrowers that have books on loan (in that example). If I try to delete a record with foreign key references, I get an error to the effect of: Database access failed: Cannot delete or update a parent row: a foreign key constraint fails (`dbname`.`tablename`, CONSTRAINT `fkfieldid` FOREIGN KEY (`fieldid`) REFERENCES `tablename` (`fieldid`)) One solution is to write a query to check if each record, in a list of records, has any foreign key references in any of the possible tables it could be referenced. However, if I wish to display a list of 100 records from a table in my content management system, and I have to run 100 sub-queries in order to display that list, it is obviously very inefficient! End users become confused when they try to delete a record but they can't because that data is 'in use' elsewhere, so I would rather remove the option to delete to avoid confusion. Any ideas on what to do? This must be a problem that others come across. [MySQL version: 5.1 (on my development PC).]
  10. I am using Ajax to sort a table without having to reload the entire page. However, how can I rewrite the URL each time the page is sorted by a different column so that if the user reloads the page, or booksmarks the page, it will load up still sorted by the last column they sorted the data by? For example, at the moment, if the data table is sorted by forename, but the user clicks on the surname header to sort it by surname, and then the user hits the reload button of the browser, the data will once again be sorted by forename. I want it to reload the page still sorted by surname. How can I do this? Thanks, Stephen
  11. I have a list of records from a database displayed in a table on my web page (using PHP/MySQL). I want to be able to dynamically do the following without having to access the database again: 1) Filter the records (e.g. you enter the surname Smith and only records containing Smith in the surname are displayed), and 2) Sort the records (e.g. by surname, forename etc). I can work out how to do this using Ajax, but even if I am using Ajax, in the background I am still having to access the database again. Is it possible to access the database just once, get all of the records, display the default list of records sorted by a default field, and then allow the user to filter and sort that data without having to reload the data from the database each time? Is it possible to create some sort of dynamic XML file and use that, or is there a better way to do it? Thanks, Stephen
  12. Yeah there's definitely only one \ in the record in the table. I'm just developing the system at the moment and using test data, so I'm using phpMyAdmin quite a bit to test and change data.
  13. I am currently working on using the mysql_real_escape_string function in PHP to make sure that values entered in a web page form for a search query are properly escaped. However, if I have a record with a field value containing single backslash in it, I need to convert the single backslash to 4 backslashes in order to get the field value when using LIKE. For example: If I have a record with a forename field value of: Pa\ul The following SQL does not return this record: SELECT * FROM LISTOFNAMES WHERE FORENAME LIKE 'Pa\\ul' but the following does return this record: SELECT * FROM LISTOFNAMES WHERE FORENAME LIKE 'Pa\\\\ul' While writing this I did another search on Google and I seem to have found an explanation for this here: http://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html#operator_like Scroll down to this note: Because MySQL uses C escape syntax in strings (for example, "\n" to represent a newline character), you must double any "\" that you use in LIKE strings. For example, to search for "\n", specify it as "\\n". To search for "\", specify it as "\\\\"; this is because the backslashes are stripped once by the parser and again when the pattern match is made, leaving a single backslash to be matched against. On other hand, if I use = instead of LIKE, I only need to use two backslashes to represent a single backslash. And if I use the mysql_real_escape_string function, it only converts a single backslash to two backslashes. If I am using LIKE, which I am, I will need to manually change this to 4 backslashes. It all seems a bit confusing, although I think I understand it now. Any thoughts? Stephen
×
×
  • 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.