I would like to find a way to check if my query result doesn't return a value. Example:
If in my tableExample on database there isn' t the id that I'm passing , the method should return an exception or a simple echo that indicate me the not presence in the table
My code below:
try{
DB::table('tableExample')
->where('id', "2")
->update(['update' => "1"]);
return $result= array("result" => "true" );
}catch(QueryException $e){
return $result= array("result" => "false" );
echo " - ".$e;
}
}
update method return integer value (affected rows) if success, try like this
try{
$update = DB::table('tableExample')
->where('id', "2")
->update(['update' => "1"]);
if($update){
$result = array("result" => true );
}else{
$result = array("result" => false,"message"=>"Not Found" );
}
}catch(QueryException $e){
$result = array("result" => false,"message"=>$e->getMessage() );
}
return $result;
Use findOrFail() helper method. That way you do not need to wrap the action in a try catch since if findOrFail does not find the row then it will throw an exception.
$resultData = DB::table('tableExample')->findOrFail(2);
$update = $resultData->update(['update' => "1"]);
if(!$update){
return response(['results'=>false]);
}
return response(['results'=> true]);
}
update() method returns a boolean true for success on update and versa.
You can do this by using the whereExists clause:
https://laravel.com/docs/5.5/queries#where-exists-clauses
https://laravel.com/api/5.5/Illuminate/Database/Query/Builder.html#method_exists
I need to create a drcode using the last insert id in the prescribed format,
previously I have used cor php to get the code but now in codeigniter am not able to get the code as the previous one. How can i do this? I am providing my controller and model
Controller
public function newdoctor_post() {
$employee_id = $this->post('EMPLOYEE_ID');
$doctorname = $this->post('DOCTOR_NAME');
$mobilenumber = $this->post('DRMOBILE');
$users_data = $this->Rest_user_model->doctor_exists($mobilenumber);
if ($users_data == 1) {
$message = ['status' => 2,
// 'result' => array(),
'message' => 'Doctor already registered'];
} else {
$speciality = $this->post('SPECIALITY');
$longitude = $this->post('LONGITUDE');
$latitude = $this->post('LATITUDE');
$drcode = $this->post('DRCODE');
$createdon = date('Y-m-d H:i:s');
$insert_array = array('EMPLOYEE_ID' => $employee_id, 'DOCTOR_NAME' => $doctorname, 'DRMOBILE' => $mobilenumber, 'SPECIALITY' => $speciality, 'LONGITUDE' => $longitude, 'LATITUDE' => $latitude, 'CREATEDON' => $createdon);
$users_data = $this->Rest_user_model->doctorregistration($insert_array);
$message = ['status' => 1, // 'result' => $users_data,
'message' => 'Doctor Registered Successfully'];
}
$this->set_response($message, REST_Controller::HTTP_OK);
}
Model
public function doctorregistration($data) {
if (!empty($data)) {
$this->db->insert('DOCTORDETAILS', $data);
return $this->db->insert_id();
}
return 0;
}
Code Generation
$sql1="SELECT DRCODE FROM DOCTORDETAILS ORDER BY DRCODEDESC LIMIT 1";
$query=mysql_query($sql1);
if (!$sql1) { // add this check.
die('Invalid query: ' . mysql_error());
}
$output_array2=array();
while($row=mysql_fetch_assoc($query))
{
$ids=$row['DRCODE'];
}
// echo $ids;
if($ids){
$su=1;
$num =$num = 'DR' . str_pad($su + substr($ids, 3), 6, '0', STR_PAD_LEFT);;
$unique=$num;
}else{
$unique='DR000001';
}
Try this - replace this code below with your Code Generation code.
$sql1="SELECT DRCODE FROM DOCTORDETAILS ORDER BY DRCODEDESC LIMIT 1";
$query=$this->db->query($sql1);
if (!$sql1) { // add this check.
die('Invalid query');
}
$output_array2=array();
foreach($query->result_array() as $row)
{
$ids=$row['DRCODE'];
}
// echo $ids;
if($ids){
$su=1;
$num =$num = 'DR' . str_pad($su + substr($ids, 3), 6, '0', STR_PAD_LEFT);;
$unique=$num;
}else{
$unique='DR000001';
}
You should make a different column fr drcode, leave it blank.
Now make a trigger on insert, it should be run after insert.
I am assuming DOCTORDETAILS as table name and drcode as column name
DELIMITER $$
CREATE TRIGGER trigger_after_insert
AFTER INSERT ON `DOCTORDETAILS` FOR EACH ROW
begin
UPDATE `DOCTORDETAILS` set drcode=CONCAT('DR',new.id);
END$$
DELIMITER ;
The trigger create a new string with your new auto generated id
Trigger in MySQL:
TechonTheNet
MySQL Trigger on after insert
I'm trying to build a script where I need to read a txt file and execute some process with the lines on the file. For example, I need to check if the ID exists, if the information has updated, if yes, then update the current table, if no, then insert a new row on another temporary table to be manually checked later.
These files may contain more than 20,30 thousand lines.
When I just read the file and print some dummie content from the lines, it takes up to 40-50ms. However, when I need to connect to the database to do all those verifications, it stops before the end due to the timeout.
This is what I'm doing so far:
$handle = fopen($path, "r") or die("Couldn't get handle");
if ($handle) {
while (!feof($handle)) {
$buffer = fgets($handle, 4096);
$segment = explode('|', $buffer);
if ( strlen($segment[0]) > 6 ) {
$param = [':code' => intval($segment[0])];
$codeObj = Sql::exec("SELECT value FROM product WHERE code = :code", $param);
if ( !$codeObj ) {
$param = [
':code' => $segment[0],
':name' => $segment[1],
':value' => $segment[2],
];
Sql::exec("INSERT INTO product_tmp (code, name, value) VALUES (:code, :name, :value)", $param);
} else {
if ( $codeObj->value !== $segment[2] ) {
$param = [
':code' => $segment[0],
':value' => $segment[2],
];
Sql::exec("UPDATE product SET value = :value WHERE code = :code", $param);
}
}
}
}
fclose($handle);
}
And this is my Sql Class to connect with PDO and execute the query:
public static function exec($sql, $param = null) {
try {
$conn = new PDO('mysql:charset=utf8mb4;host= '....'); // I've just deleted the information to connect to the database (password, user, etc.)
$q = $conn->prepare($sql);
if ( isset($param) ) {
foreach ($param as $key => $value) {
$$key = $value;
$q->bindParam($key, $$key);
}
}
$q->execute();
$response = $q->fetchAll();
if ( count($response) ) return $response;
return false;
} catch(PDOException $e) {
return 'ERROR: ' . $e->getMessage();
}
}
As you can see, each query I do through Sql::exec(), is openning a new connection. I don't know if this may be the cause of such a delay on the process, because when I don't do any Sql query, the script run within ms.
Or what other part of the code may be causing this problem?
First of all, make your function like this,
to avoid multiple connects and also o get rid of useless code.
public static function getPDO() {
if (!static::$conn) {
static::$conn = new PDO('mysql:charset=utf8mb4;host= ....');
}
return static::$conn;
}
public static function exec($sql, $param = null) {
$q = static::getPDO()->prepare($sql);
$q->execute($param);
return $q;
}
then create unique index for the code field
then use a single INSERT ... ON DUPLICATE KEY UPDATE query instead of your thrree queries
you may also want to wrap your inserts in a transaction, it may speed up the inserts up to 70 times.
I'm a newbie programmer trying to find my way in the world. I've got my hands on JSON data that I'm trying to parse out into SQL statements to populate multiple database tables. I would like to loop through each dimension of the array and pull out specific parts of it to create an INSERT statement I can just pass to MySQL. I'm not sure if this is the best way to populate separate tables with data from one JSON file but it's the only thing I can think of. MySQL tables are separated so there is a table for a person, a table for address type, a table for address, a table for phone, a table for email etc. This is to account for a person record having numerous phone numbers, email addresses etc.
I have been able to decode the JSON from an external URL. Here is the code and a sample of the output using print_r.
$json_string = 'http://....';
$jsondata = file_get_contents($json_string);
$data = json_decode($jsondata, TRUE);
1 Record Sample:
Array ( [objects] => Array ( [0] => Array ( [first_name] => Anthony [last_name] => Perruzza [name] => Anthony Perruzza [elected_office] => City councillor [url] => http://www.toronto.ca/councillors/perruzza1.htm [gender] => [extra] => Array ( ) [related] => Array ( [boundary_url] => /boundaries/toronto-wards/york-west-8/ [representative_set_url] => /representative-sets/toronto-city-council/ ) [source_url] => http://www.toronto.ca/councillors/perruzza1.htm [offices] => Array ( [0] => Array ( [tel] => 416-338-5335 ) ) [representative_set_name] => Toronto City Council [party_name] => [district_name] => York West (8) [email] => councillor_perruzza#toronto.ca [personal_url] => [photo_url] => ) ) [meta] => Array ( [next] => /representatives/?limit=1&offset=1 [total_count] => 1059 [previous] => [limit] => 1 [offset] => 0 ) )
JSON Code Sample:
{"objects": [
{"first_name": "Keith",
"last_name": "Ashfield",
"name": "Keith Ashfield",
"elected_office": "MP",
"url": "http://www.parl.gc.ca/MembersOfParliament/ProfileMP.aspx?Key=170143&Language=E",
"gender": "",
"extra": {},
"related": {
"boundary_url": "/boundaries/federal-electoral-districts/13003/",
"representative_set_url": "/representative-sets/house-of-commons/"
},
"source_url": "http://www.parl.gc.ca/MembersOfParliament/MainMPsCompleteList.aspx?TimePeriod=Current&Language=E",
"offices": [
{ "type": "legislature",
"fax": "613-996-9955",
"postal": "House of Commons\nOttawa, Ontario\nK1A 0A6",
"tel": "613-992-1067"
},
{ "type": "constituency",
"fax": "506-452-4076",
"postal": "23 Alison Blvd (Main Office)\nFredericton, New Brunswick\nE3C 2N5",
"tel": "506-452-4110"
}
],
"representative_set_name": "House of Commons",
"party_name": "Conservative",
"district_name": "Fredericton",
"email": "keith.ashfield#parl.gc.ca",
"personal_url": "",
"photo_url": "http://www.parl.gc.ca/MembersOfParliament/Images/OfficialMPPhotos/41/AshfieldKeith_CPC.jpg"
}
],
"meta": {
"next": "/representatives/house-of-commons/?limit=1&offset=1",
"total_count": 307,
"previous": null,
"limit": 1,
"offset": 0
}
}
Any help you can offer would be greatly appreciated. I've been pulling my hair out for the last few days trying to figure it out.
I've tried customizing code like the following to make it work but I haven't been able to hit the sweet spot. Please not, this code doesn't reference my data or variables. I deleted what didn't work for me. I'm just including it to give you an idea what I've tried.
foreach ($data as $item) {
echo $item->{'first_name'} . "<br/>";
echo $item->{'last_name'};
}
If you could point me in the direction of being able to parse out data from any level of the array it would be greatly appreciated.
Best,
S
AFAIK, it is not possible to insert into several tables with one insert. Moreover, you need to preserve data integrity, so related tables would have right foreign keys.
The general idea is to iterate through the data, insert records and remember inserted ids, then write them as corresponding foreign keys.
You iterate thru your objects, insert all primitive properties as fields, then get an id using mysql_last_insert_id, then while saving offices (or their details) put that id as their related object id.
E.g. we have the following JSON.
{"authors": [
{"first_name": "John",
"last_name": "Doe",
"books": [{
"title": "Capture the flag",
"ISBN": "123-456789-12345",
},{
"title": "Deathmatch",
"ISBN": "123-456789-12346",
}]
]}
Then we insert that data with the following code:
foreach ($data as $author) {
mysql_query("INSERT INTO `authors` (`first_name`, `last_name`), VALUES('{$author->first_name}', '{$author->last_name}') ");
$author_id = mysql_last_insert_id();
foreach ($author->books as $book) {
mysql_query("INSERT INTO `books` (`title`, `isbn`, `author_id`), VALUES('{$book->title}', '{$book->isbn}', '{$author_id}') ");
}
}
This is for case you have auto-increment for id's in tables.
Of course, you'll need to validate and escape data before insertion etc.
Here is something that you can use to get the structure of a json response. It works recursively so that it will create an entry to for each object as well as an entry in a separate table for each property of each object. I hope to get others feedback/enhancement on this as well to turn it into create sql statements.
class DataDriller {
var $ext_obj_to_parse;
var $ext_type_name;
var $data;
var $recurse;
var $ext_type_id;
var $ext_related_id;
var $type_id;
var $auto_create;
var $sql;
var $error;
var $controller;
var $ExtType;
var $ExtStructure;
var $link;
var $ext_source_id;
function init($ExtType, $ExtStructure) {
$this->ExtType = $ExtType;
$this->ExtStructure = $ExtStructure;
}
function setup($ext_obj_to_parse, $ext_type_name, $ext_type_id = false, $ext_related_id = false, $auto_create = true, $ext_source_id) {
$this->ext_obj_to_parse = $ext_obj_to_parse;
$this->ext_type_name = $ext_type_name;
$this->ext_type_id = $ext_type_id;
$this->auto_create = $auto_create;
$this->error = false;
$this->ext_related_id = $ext_related_id;
$this->ext_source_id = $ext_source_id;
if ($this->get_ext_type_data() === false) {
if ($this->type_handling() === false) {
$this->error_data();
}
}
if (gettype($this->ext_obj_to_parse) == "object" || gettype($this->ext_obj_to_parse) == "array") {
$this->to_struct();
} else {
//single variable and data
$this->data[$this->ext_type_name] = gettype($this->ext_obj_to_parse);
$this->sql = "replace into ext_structures (name, data_type, ext_type_id) values ('$this->ext_type_name', '" . gettype($this->ext_obj_to_parse) . "', " . $this->ext_type_id . ")";
$this->sql_it();
}
}
function get_ext_type_data() {
if (is_numeric($this->ext_type_id)) {
return true;
} else if (strlen($this->ext_type_name) > 0) {
$this->sql = "select id From ext_types where name = '" . $this->ext_type_name . "' limit 1";
$this->ext_type_id = $this->sql_it('id');
return $this->ext_type_id;
} else {
return false;
}
}
function type_handling() {
if ($this->auto_create == true && gettype($this->ext_type_name) === "string") {
//$this->sql = "replace into types (name) values ('$this->ext_type_name')";
//
//$this->type_id = $this->sql_it();
//if ($this->type_id !== 0) {
//if ($this->ext_related_id) {
$this->sql = "insert into ext_types (name, ext_source_id, parent_id) values ( '$this->ext_type_name', $this->ext_source_id, '$this->ext_related_id')";
$this->ext_type_id = $this->sql_it();
$this->sql = "replace into ext_type_rel (ext_type_id_1, ext_type_id_2) values ($this->ext_type_id, $this->ext_related_id)";
$this->sql_it();
/*} else {
$this->error = "Unable to obtain typeid from insert";
$this->error_data();
return false;
}*/
}
//}
}
function to_struct() {
//keys are not objects but values can be
//always display keys, when value object - increase spacer - call self - reiterate
// if value is not object complete
foreach ($this->ext_obj_to_parse as $key => $value) {
if (gettype($value) == "object" || gettype($value) == "array") {
//check to see if object exists within the database with the data definitions and methods
//there are no existing data structure insert
//recurse into the drill-down again if it does not exist
if (is_numeric($key) || $key == "data" || $key == "statuses") {
$this->recurse = new DataDriller();
if (!$this->ext_related_id > 0){ $this->ext_related_it = $this->ext_type_id; }
$this->recurse->setup($value, $this->ext_type_name, $this->ext_type_id, $this->ext_related_id, true, $this->ext_source_id);
} else {
$this->recurse = new DataDriller();
$this->recurse->setup($value, $key, false, $this->ext_type_id, true, $this->ext_source_id);
}
$this->data[$key] = $this->recurse->data;
unset($this->recurse);
//this is where we insert the relationship between objects here
} else {
//not an ojbect just a field of the existing object
$this->data[$key] = gettype($value);
$this->sql = "replace into ext_structures (name, data_type, ext_type_id) values ('$key', '" . gettype($value) . "', " . $this->ext_type_id . ")";
$this->sql_it();
}
}
}
function sql_it($field_name = false) {
$VARDB_server = '192.168.10....';
$VARDB_port = '3306';
$VARDB_user = 'user';
$VARDB_pass = 'pass';
$VARDB_database = 'db_name';
$this->link = mysql_connect("$VARDB_server:$VARDB_port", "$VARDB_user", "$VARDB_pass");
if (!$this->link) {
echo 'MySQL connect ERROR: ' . mysql_error();
die();
}
$res = mysql_select_db("$VARDB_database");
if (!$res) {
echo mysql_error();
}
$res = mysql_query($this->sql, $this->link);
if (mysql_error()) {
$this->error = mysql_error() . " MYSQL reported an error " . $this->sql;
CakeLog::write('datadriller', $this->sql . " error? " . mysql_error());
die();
}
if ($field_name === false) {
if (strpos($this->sql, 'insert') !== false || strpos($this->sql, 'replace') !== false) {
$id = mysql_insert_id();
return $id;
} else {
$this->error = "field name is requeired for getting results";
$this->error_data();
return false;
}
} else {
if (mysql_num_rows($res) > 0) {
$r = mysql_fetch_array($res);
mysql_free_result($res);
if (array_key_exists($field_name, $r)) {
return $r[$field_name];
} else {
$this->error = "field name does not exist in result set";
$this->error_data();
return false;
}
} else {
$this->error = "select statement returned no data ";
return false;
}
}
}
function error_data() {
echo "<B> $this->error MySQL error? <font color=red>" . mysql_error() . " </font> SQL: $this->sql </b><BR><BR>\n";
echo "DUMP DATA\n";
echo "<pre>";
var_dump($this->data);
echo "RECURSED OBJECT \n\n";
var_dump($this->recurse);
echo "</pre>";
}
function JSONTOInsertSQL($table,$obj){
$keys = implode('`,`', array_map('addslashes', array_keys($obj)));
$values = implode("','", array_map('addslashes', array_values($obj)));
return "INSERT INTO `$table` (`$keys`) VALUES ('$values')";
}
This is my controller code which save data in table but when I put more than max length column data , it is not retuning me STATUS false; nothing is happening. please help
function saveImproveUs(){
$status =array("STATUS"=>"false");
try{
$improveUs = array(
'NAME' => trim($this->input->post('name')),
'EMAIL' => trim($this->input->post('email')),
'LOCATION' => trim($this->input->post('location')),
'MESSAGE_TYPE' => trim($this->input->post('messageType')),
'COMMENTS' => trim($this->input->post('comments'))
);
// Save improve us
$this->db->insert('trn_improve_us', $improveUs);
if ($this->db->affected_rows() > 0){
$status = array("STATUS"=>"true");
}
}catch(Exception $ex) {
//show_error($ex);
echo "I am in exception";
exit;
}
echo json_encode (array($status)) ;
}
You have to throw the exception, it won't do this for you.
if ($this->db->affected_rows() > 0){
$status = array("STATUS"=>"true");
}
else {
throw new Exception("Could not insert data");
}
Also inserting more data than a column can hold will automatically get cut-off in MySQL, the insert won't actually fail. You should use strlen to check the length of a string and validate it manually.