Jump to content

chiprivers

Members
  • Posts

    468
  • Joined

  • Last visited

    Never

Everything posted by chiprivers

  1. Sorted I was using private instead of protected on my properties. I said I was new
  2. I am tackling my first major PHP application using OOP and I am getting a little stuck with extending classes. I want to be able to create a parent class which contains a number of properties and methods, and be able to extend this class with the ability to access the parent properties and methods. The specific problem I am having at the moment is with a property holding a MySQL connection. ie. class DatabaseManager { private $mysqli; public function DatabaseManager() { $this->mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE); } // various generic database methods declared here } class AnotherClass extends DatabaseManager { public function AnotherClass() { $this->DatabaseManager(); } public function methodUsingMysqli($query) { $result = $this->mysqli->prepare($query); // do something with result blah blah } } When I try an run my application using the above construct and trying to access the $mysqli property declared in the parent class, it says that it does not exist.
  3. How do I create alternative contractor methods for my class. Like when you create a DateTime object you can create it using an alternative time passed by a string using DateTime::createFromFormat(). I want to have a default constructor method but a couple of extra contractor methods that can be called when the instance is created.
  4. It does have permission - it works when using mysql_connect()
  5. I have just set up MAMP on my new MacBook Pro and I am having trouble getting my MySQL connection working. When I use the following code: $mysqli = new mysqli('localhost', 'application', 'application', 'dorset'); if ($mysqli->connect_errno) { echo "Failed to connect to MySQL: (".$mysqli-errno.") ".$mysqli->connect_error; } I get this error: Failed to connect to MySQL: () Access denied for user 'application'@'localhost' (using password: YES) However if I use the old mysql_connect() function, it doesnt throw back any errors?! Any ideas?
  6. I am writing a function to update an existing record and I want to be able to return the values from the updated record prior to the update. ie. A record in table 'employee' has values id = 1, first_name = 'Joe' and last_name = 'Bloggs' The function is passed employee id 1 and new name Tom Cobley so the function will update record 1 with the respective name entries. Easy enough! However, I want the function to be able to return the old name, Joe Bloggs. Can this be done with a single query which updates the record with the specified values but also returns the old values at the same time or do I have to first query for the values of record 1 and then run a second update query?
  7. I'm not actually using PHP and MySQL for this query, I am using MS Access which is querying an Oracle database. I have posted here as I usually work with PHP and MySQL and as I understand it SQL is pretty much SQL which ever application I am using. I think the problem with the query is due to Access not liking the comparison between INT and VARCHAR so although it would probably normally work in SQL I need to try and work around the MS Access issue.
  8. I am struggling with a table join where the comparitive table fields are in different formats; one int and one var. I know that this is sloppy database construction but unfortunately it is not my database and I do not have any control over the table construction. In my query I have a join as below: SELECT ... ... FROM tableA LEFT JOIN tableB ON tableA.varField = tableB.intField Unfortunately this is not running because the datatypes in the fields that I am joining on are not the same datatype. Is there a function I can use to change the var value to an int before comparing it, or vice versa? I think from googling that I should be using cast() or convert() but I cannot find a clear explanation of these functions that has enabled me to workout if and how I could use either of these functions. Any help would be very much appreciated.
  9. thank you - that looks like it will work - will give it a go
  10. That looks good but do I also need to specify the year also to ensure that it only returns entries with a matching month in the current year and not say all records with a review date in October every year?
  11. Please could somebody help me with this, I assume, pretty simple SQL where clause... I have a DATE column in my table (field is called reviewdate) and I would like to return all records where the value in this column is within the current month. Could somebody help with the WHERE clause? Something like... WHERE functionformonthandyearof(reviewdate) = functionforcurrentmonthandyear() Many thanks in advance.
  12. Thanks. All sorted now. I did resolve the issue but had not worked out why it wasn't working originally. Simple now you point it out!!
  13. I am trying to create a loop that will work through the dates of the current month; I have the following piece of code for the loop: $monthFloor = DateTime::createFromFormat('j H:i:s', '1 0:0:0'); $monthCeiling = DateTime::createFromFormat('j H:i:s', $monthFloor->format('t').' 0:0:0'); for ( $monthDate = DateTime::createFromFormat('Y-m-d H:i:s', $monthFloor->format('Y-m-d H:i:s')); $monthDate->format('d') <= $monthCeiling->format('d'); $monthDate->modify('+ 1 day') ) { echo $monthDate->format('Y-m-d H:i:s')."<br />"; } But when I run this, it loops through the dates of the current month twice!? why is it doing this? and how can I rectify the loop so it only goes through the month once?
  14. I am not sure of what aspect of programming is best used for achieving my objective so I will start by posting this question here. Mods, please feel free to reposition this thread if you feel appropriate. I want to produce an interactive map upon which there will be a number of elements that can be repositioned on the map, and depending on the positions where they are put, various aspects of the map will change. I intend to have a <div> that contains this whole map which will have a background image which will display the map outline. I will use several smaller <div>s as interative elements on the map which will moveable. I will use jquery's draggable feature to enable this. So far so good. The bit I am unsure on how to do is how to produce some irregular shaped polygon sectors on the map which I can manipulate. What I need to do is divide the map up in to a number of sectors. I then need to be able to control the appearance of these sectors using CSS as if they were an element. For example, if one of the draggable elements is repositioned within one of these sectors, I need to be able to run a function in jquery that will change the background colour of the sector. Is there a way of creating an irregular shaped div? Please let me know if I need to explain this further.
  15. In my current application, I am working with dates in the format 'YYYY-MM-DD HH:MM:SS'. The main reason I am using this format is because it is the format stored in my MYSQL database. I have had no problems working with this format in the PHP elements of my script, however I need some assistance with the javascript! I need to be able to output a datetime string in the above format from my javascript date object. In PHP it woudld be simple, $date->format('Y-m-d H:i:s'); but from what I can see with Javascript, you have to extract each portion of the date and time. This wouldn't be such a problem if the numbers for months and days were prefixed with 0s where applicable! Is there a straight forward function for this that I have missed somewhere?
  16. I have a function that receives two datetime values attached to the variables $start and $end. These will be in the format 'Y-m-d H:i:s'. For each of these values, I create a DateTime object to make the handling of them easier: $startDT = DateTime::createFromFormat('Y-m-d H:i:s', $start);$endDT = DateTime::createFromFormat('Y-m-d H:i:s', $end); What I need to do now is ensure that the seconds of the $startDT value are always set to '00' and the seconds of the $endDT value are always set to '59'. Is there an easy way to do this without having to extract the individual values for hours, minutes and seconds before reapplying them using the setTime function, adjusting the values accordingly?
  17. I appreciate that any select query gives a snapshot in time and this could be out of date in a multi user enviroment, however, if a snapshot is taken without the full set of updates I talk about being completed, it will not even be a valid snapshot. The displaying page will also be reliant on calculations based on the data taken from the returned rows and if the full set of updates are not complete, it will mess up the calculations. Im sorry if I am not explaining this very well.
  18. OK, errors aside, what if another user is viewing a page which calls a SELECT query on this table right in the middle of my script which is inserting a new record aswell as updating any existing conflicting entries. I cant show you the code as I have not completed it yet but to give you an idea of what I am doing... Each 'status' entry will have a 'start' and 'end' time and a 'statusCode'. If the time period of the new entry overlaps any time period of any existing records, the existing records need to be updated. What I intend to do is first query the database to return any records where there is an overlap in the time periods and where the recordStatus is set to 1. I will loop through each of these returned records and see where the overlap is, creating further new entries for any period at either the start or end of the new period. All returned records will then be updated with recordStatus = 0. Then the new record will be created. This should explain further my concern that if the table is queried at any time during the above list of operations, it will not give the desired result. It could return multiple overlapping time periods or the new record may not be created but the old ones set to 0 so no records would be returned. I appreciate that it would have to be pretty unlucky but as this will be a mutli-user application with potentially several updates to this particular table being made at any one time, whilst also other multiple users reading the data from this table, I would like to make sure the coding is appropriate from the start.
  19. In my database, I have a table which contains 'status' records. Each new entry made to the table may superceed one or more of the previous entries in the table. Any records which are being superceeded need to be updated with the colum 'recordStatus' being set to 0. All new records will have a 'recordStatus' default to 1. I have recently moved to mysqli for my mysql database interaction and I have been using prepared statements. The current function I have produced to insert new records into the above table, and also update any existing entries at the same time, is using prepared statements and executing the relevant insert or update statement for each query as required. Elsewhere in the script, this same status table will be queryied to return all records where the recordStatus value is 1. My concern is that this SELECT query may return spurious results if either: i) the select query is processed in between the insert and update queries in the above function, at which point there may be multiple conflicting status records with their recordStatus value still set to 1 or ii) if there is an error in processing the above function so that all of the required update or insert statements are not completed successfully. First of all, is this an unneccessary concern? Or should I use the multi_query function to submit all required queries at once, which I believe will lock the table until all necessary updates are complete? I have not used the multi_query function before so could someone give me some quick guidance on this? can I still used prepared statements with the multi-query or should I use some other checks on any input values to ensure that there is no SQL injection?
  20. Not sure what I have done but it is not working!? Just retyped it in and it seems to be fine. I did check them all but it is possible that I missed a spelling mistake on a column name!
  21. I have tried echoing $mysqli->connect_error but nothing is returned. Error reporting is set to all, except notices. I had not used constants before and the tutorial I found when googling was using the constant() function to reference the constant values. I have changed this now and use the constant name itself. All above considered, it is still not working!
  22. Modified code: $mysqli = new mysqli(constant('DB_HOST'), constant('DB_USERNAME'), constant('DB_PASSWORD'), constant('DB_DATABASE')); $insertStatus = $mysqli->prepare(" INSERT INTO status (contractId, start, end, statusCodeId, resourceId, recordCreated, recordCreatedBy) VALUES (?, ?, ?, ?, ?, ?, ?) "); echo $mysqli->error; if ($insertStatus) { $insertStatus->bind_param('issiisi', $contractId, $start, $end, $statusCodeId, $resourceId, $recordCreated, $recordCreatedBy); } However there is no error being returned!
  23. I am getting the following error on my mysqli prepare statement / bind parameters script: Fatal error: Call to a member function bind_param() on a non-object in... The script is: $mysqli = new mysqli(constant('DB_HOST'), constant('DB_USERNAME'), constant('DB_PASSWORD'), constant('DB_DATABASE')); $insertStatus = $mysqli->prepare(" INSERT INTO status (contractId, start, end, statusCodeId, resourceId, recordCreated, recordCreatedBy) VALUES (?, ?, ?, ?, ?, ?, ?) "); $insertStatus->bind_param('issiisi', $contractId, $start, $end, $statusCodeId, $resourceId, $recordCreated, $recordCreatedBy); I can't work out what the error is? I have tested the database connection and that is fine, as is the query statement!
  24. ChrisMarsden, could you explain a little more about what you are trying to do?
  25. chiprivers

    indexes

    Other than applying a primary index to my id column, I have not used indexes in my tables before. After reading an article on top tips for mysql basics, I understand that it is best to apply an index to any column on which you will be searching on regularly. I have several tables from which I extract data from using JOINs in my queries. Do I need to apply an index to both columns used within the JOIN or just, say the right table on a LEFT JOIN? ie. table1 has a column 'table2Ref' and table 2 has column 'id'. SELECT * FROM table1 LEFT JOIN table2 ON table1.table2Ref = table2.id The id column in table2 will already have a primary index on it, but should I apply an index to the table2Ref column in table1?
×
×
  • 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.