Lithium PHP Framework - SQL Insert Ignore / If not exists - php

is there any way to execute Lithium Model::save() functions with INSERT IGNORE and/or IF NOT EXISTS statements?
The only way to do so is executing RAW-SQL-Statements. But this would be a design violation...so any help would be great.
Thanks in advise.
/creality

There isn't a way to do that with Lithium unless you do the raw SQL. You can see that only "INSERT INTO ..." is supported with the lithium\data\source\Database object.
The reason that this isn't supported is that saving a new object will automatically populate the primary key (id) of the object and an INSERT IGNORE would really complicate the issue.
The best strategy is to run a SELECT based on the key values and then toss out any values that have matching records.

Related

Using Php PDO Insert statement

Someone told me that when you are working with PDO, you cannot use "INSERT INTO .... SET" to insert data into database, because it will not work on databases other than MySQL. I'm not sure what exactly he means, maybe he means I should use the other method of inserting like,
INSERT INTO table (column1, column2) VALUES (?, ?)
I tried searching on the internet for this, but I couldn't find anything. Please let me know about this.
Thank you.
You should use the INSERT INTO table (column1, column2) VALUES (?, ?) statement instead of the INSERT INTO table SET column1=? statement, because that's the correct syntax for SQL based database languages. Although MySQL accepts it, others may not.
The INSERT INTO ... SET syntax is not part of the ANSI SQL standard and therefore is not supported as widely across different RDBMS implementations. If you are designing your application such that it is tightly coupled to MySQL, using this syntax would be OK. If you are trying to design such that your application is not tightly coupled with the RDBMS implementation, then you should use the more standard INSERT INTO table (columns) VALUES (values) syntax.
Positional parameters in PHP PDO is just fine. The other option is named parameters. If I remember correctly from what I've seen in the PDO C code, it is PHP and not the DBM that does the replacements.
http://php.net/manual/en/pdo.prepared-statements.php
As stated in this the only metioned difference, b/n mysql driver and others is stated below. Suppose this is the a simple, query:
<?php
$stmt = $db->query('SELECT * FROM table');
$row_count = $stmt->rowCount();
echo $row_count.' rows selected';
Now, let's read how the documentation states how/why that query can return count of affected_rows only when the mysql driver
NOTE: Though the documentation says this method is only for returning
affected rows from UPDATE, INSERT, DELETE queries, with the PDO_MYSQL
driver (and this driver only) you can get the row count for SELECT
queries. Keep this in mind when writing code for multiple databases.
This is because MySQL's protocol is one of the very few that give this
information to the client for SELECT statements. Most other database
vendors don't bother divulging this information to the client as it
would incur more overhead in their implementations.
So, there is that little difference, as far as I know.

Insert into Table 1, get incremented value, and insert into table 2

In php, I'm trying to insert a value into one table, return an auto-incremented value, and then insert that value along with other values into a second table.
I'm running into a few problems. First, while there's a lot of ways of doing this in SQL, I have to do this with php's mysql functions. I'm afraid of weird errors if I combine multiple statements together. Second, like I mentioned, I need this to be done in one query, as it'll be used for a web application.
My current query is like this
INSERT INTO TABLE1 VALUES(*);
INSERT INTO TABLE2
SELECT max(AutoIncrementedColumn)
FROM TABLE1;
The problem I'm having is that mysql_query() doesn't support multi queries. Also, I believe mysql_escape_string() removes anything it believes to be a multi query, so even if I could somehow get mysql_query to believe my query is not a multi query, I'm still out of luck unless I write my own escape method.
Does anyone have any ideas on how to deal with this problem?
EDIT: Forgot to mention that I can't use mysql_insert_id because the column that's autoincrementing is of type Bigint.

php zend db profiler INSERT and UPDATE queries

I am trying to use the zend db profiler to record the queries. I read the documentation from the zend website and it works fine except that I am not able to get the values for the insert and the update queries. I just see the query as below with the ? for values. I tried using the getQueryParams to atleast see if I can get the values in the array and it works only with getLastQueryProfile and not the getQueryProfile to get it for all queries. Can some one please let me know how I can get the "?" with the actual values? I am not concerned about the Array as anyways it doesn't do the purpose.
INSERT INTO `myTable` (`id`, `lname`, `fname`) VALUES (?, ?, ?)
thanks.
I don't think it's possible.
According to php's PDO Manual there's no way to retrieve a query after a prepare() statement. In the examples it's easy to understand because using the PDO manually you know the order of the parameters and have the statement. Looking after Zend clases (Zend_Db_Table_Abstract) i see that the order is maintained and that's the proper way because it uses question mark parameters.
Reading Zend_Db_Adapter_Pdo_* clases you should get it clearer

Problem with PHP/MySQL Insert

How to insert data into two or more tables?
Currently I am using this trick. (Let just assume that the syntax is correct)
$Sql1 = "insert into user";
$Sql2 = "insert into files";
$Sql3 = "insert into messages";
query $Sql1
query $$ql2
query $Sql3
So I am using this and I need to optimize.
Also, I think I need to use Foreign key? But I don't know how to do it on phpmyadmin.
and lastly:
This is all I want.
To insert data on the "user" table and then automatically the table "messages" and "files"
will have also an ID which is the same with the user.. That is why I use to 3 queries in my code.
Please help me.
Thanks in ADVANCE!
myb
It is perfectly fine to use multiple queries.
However, you should wrap the 3 queries into one transaction. That is, if one query fails for some reason, the effects of all previous queries (if any) can be rolled back so that your database stays in a consistent state.
Note: Transactions are not possible with MySQL's MyISAM tables. Additionally you have to use PHP's Mysqli (or PDO) to be able to use transactions. (I assume that you're not using some high-level data base abstraction layer.)
As to your question regarding foreign keys: As far as I know, it makes no difference when you're using the MyISAM engine for the tables in your database (which I presume), because MyISAM doesn't even support the explicit modeling of foreign keys. Other table engines like InnoDB, however, can use foreign keys to check certain constraints like "a message must always be associated with an existing person".
Here are some nice examples regarding foreign keys: http://www.postgresql.org/files/documentation/books/aw_pgsql/node131.html
This is the answer, I think : http://dev.mysql.com/doc/refman/5.1/en/example-foreign-keys.html
With mysql u can use LAST_INSERT_ID() or in php mysql_insert_id() function. Checkout this manual entries:
http://dev.mysql.com/doc/refman/5.0/en/getting-unique-id.html
http://php.net/manual/en/function.mysql-insert-id.php

how can i insert the array of id into a single field of data base

hi how can i insert the array of service id into table into a single field like 2,3,4 in database i am confuse please help followingis my code...i am using this but it is inserted only single id..
$service=implode(",",$_POST['service']);
$sqlQuery="INSERT INTO ".DBPROMOTION." SET
promotion_service_id='".$service."',
promotion_user_id='".$user."',
promotion_discount='".$_POST['discount'].$_POST['type']."',
promotion_title='".$_POST['title']."',
promotion_start_date='".$startDate."',
promotion_end_date='".$endDate."',
promotion_code='".$_POST['code']."',
promotion_description='".$_POST['desc']."'"
You should use two database tables with relation one-to-many to link more than one element with table columnt. That would be better design.
You may insert all id's into db in form of string, but I don't think, that is what you aiming for.
Also, you should sanitize your input from $_POST before entering int to db, to prevent sql injection.
you database-model seem to be not normalized - correct that first before programming an application that will cause some realy big problems in the future.
also, you should never insert posted data directly to avoid sql-injection. use mysql_real_escape_string or, better, PDO for prepared statements.
to your problem directly: i think 'promotion_service_id' is an integer-field - '2,3,4' is a string, so (fortunately) you have no chance to insert it.

Categories