Jump to content

montyonthebonty

New Members
  • Posts

    5
  • Joined

  • Last visited

Everything posted by montyonthebonty

  1. Hi I have the following code: protected function getSample($numLocations) { $statement = $this->connect()->prepare(' SELECT * from tblRouteQuiz ORDER BY RAND() LIMIT 4'); if (!$statement->execute()) { header('location: ../sqlerror.php'); exit(); } else { $results = $statement->fetchAll(PDO::FETCH_ASSOC); $statement = null; return($results); } What do I need to do to use $numLocations as the Limit? Thanks Chris
  2. Thanks, that's perfect - that's the bit I was missing! Really appreciate your help. Chris.
  3. Good afternoon I did look there, but I can't work out what I'm doing wrong - to be fair, I got the basic code from a tutorial on building a login system, so I'm a bit sketchy on what the different objects are. I'm pretty sure lastInsertId() is the right one, but most examples use something like $conn->lastInsertId() and I can't see a connection object in my code, if that makes sense? I'm not very new to PHP, but quite new to using PHP for OOP. Good point about passing the database name in though - I will modify that. Thanks Chris
  4. Hi. I'm building a project using OOP and PHP to write to a database. I have a dbHandler class: class dbHandler { protected function connect() { try { $usrName = 'username'; $password = 'password'; $dbh = new PDO('mysql:host=localhost;dbname=dbname', $usrName, $password); return $dbh; } catch (PDOException $e) { print 'Error: ' . $e->getMessage() . '<br />'; die(); } } } This is called from an update class: class update extends dbHandler { protected function addUpdate($success) { $statement = $this->connect()->prepare(' INSERT INTO tblScheduleUpdates (updSuccess) VALUES (?)'); if (!$statement->execute(array($success))) { header('location: ../sqlerror.php'); exit(); } $last_id = $dbh->lastInsertId(); echo 'Record ID: ' . $last_id . '<br />'; $statement = null; } } But I need to find the ID (Autonumber) of the last record inserted. How do I get this? Thanks Chris
  5. I'm trying to create a PHP file to download some information which I then want to write to a MySQL Database. The raw information, after a gzdecode, looks a bit like this (there are many, many lines): {"JsonAssociationV1":{"transaction_type":"Create","main_train_uid":"G31259","assoc_train_uid":"G32783","assoc_start_date":"2021-12-13T00:00:00Z","assoc_end_date":"2022-05-13T00:00:00Z","assoc_days":"1111100","category":"NP","date_indicator":"S","location":"SHEFFLD","base_location_suffix":null,"assoc_location_suffix":null,"diagram_type":"T","CIF_stp_indicator":"P"}} {"JsonAssociationV1":{"transaction_type":"Create","main_train_uid":"G33347","assoc_train_uid":"G33111","assoc_start_date":"2021-12-13T00:00:00Z","assoc_end_date":"2022-05-13T00:00:00Z","assoc_days":"1111100","category":"NP","date_indicator":"S","location":"LEEDS","base_location_suffix":null,"assoc_location_suffix":null,"diagram_type":"T","CIF_stp_indicator":"P"}} {"JsonAssociationV1":{"transaction_type":"Create","main_train_uid":"G31259","assoc_train_uid":"G34472","assoc_start_date":"2021-12-18T00:00:00Z","assoc_end_date":"2022-05-14T00:00:00Z","assoc_days":"0000010","category":"NP","date_indicator":"S","location":"SHEFFLD","base_location_suffix":null,"assoc_location_suffix":null,"diagram_type":"T","CIF_stp_indicator":"P"}} After trying repeatedly and failing to read this directly, I decided to try converting it to an array, using a newline as a delimeter ($newArray = explode("\n", $json), which does work. To take one line as an example, this gives me an array for each line above. I've then done a json_decode on each line ($newLine = json_decode($your_array[1], true);), to produce this: Array ( [JsonAssociationV1] => Array ( [transaction_type] => Create [main_train_uid] => G31259 [assoc_train_uid] => G32783 [assoc_start_date] => 2021-12-13T00:00:00Z [assoc_end_date] => 2022-05-13T00:00:00Z [assoc_days] => 1111100 [category] => NP [date_indicator] => S [location] => SHEFFLD [base_location_suffix] => [assoc_location_suffix] => [diagram_type] => T [CIF_stp_indicator] => P ) ) However, try as I might, I can't access the information in $newLine. What I want to is find out what type it is (in this case JsonAssociationV1) and then use the information in the nested array to write to the database. Any help would be appreciated. Thanks Chris
×
×
  • 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.