CodeIgniter - continue on SQL error? - sql

Basically I have a table with a couple of columns marked Unique. I have a script that dumps a bunch of values into the table with a command like this:
$this->db->query("INSERT INTO `table` (`col1`, `col2`, `col3`) VALUES (`val1`, `val2`, `val3`)");
Every so often my script will try to insert a row which would violate the uniqueness of one of the columns. However, instead of causing the script to abort with a database error, I'd like it to continue, possible outputting a little message. Basically I'm looking for the codeigniter equivalent of
mysql_query("INSERT blah blah blah") or print("fail");
Thanks!
Mala

Yeah, took me a while too and annoyed the hell out of me:
$db['default']['db_debug'] = FALSE;
... in config/database.php - disables the error page.
After a query ran, use this to check for an error:
if (!empty($this->db->_error_message())) {
echo "FAIL";
}

I know you already have a solution, but thought this might be useful for others viewing this question as well.
Let the database do the work for you:
$this->db->query("INSERT IGNORE INTO `table` (`col1`, `col2`, `col3`) VALUES (`val1`, `val2`, `val3`)");
When you use INSERT IGNORE, things like duplicate key errors become warnings instead of errors, which let your queries run without interrupting the flow of your script.
You could then do a
SHOW WARNINGS;
after all the queries have run to see what warnings occurred.
http://dev.mysql.com/doc/refman/5.1/en/insert.html

Related

SQL Selection Query getting corrupted

Ive come across a very unusual problem (for me at least) and I have no idea how to solve it.
Essentially I made a really simple selection query to search our clients in a table (dbo_t_Person) and return their records. I needed them to be searchable even if we only have an email address, or phone number for some clients on hand. Therefore I wrote the criteria to either ignore a field if no data was entered, or to search similar (via 'Like') if only partial details were entered into any given field. See the SQL below, apologies for how repetitive it is.
This is all well and good, it works perfectly and is fast enough for our uses.
However.
I can run the query as many times as I wish with new data entered and it works fine, but if I close the query and reopen it, the SQL goes haywire and it runs out of memory and crashes access, this is crashing just opening the SQL as well as running it. By haywire I mean that if i manage to luck out and reopen the SQL, lines of SQL are suddenly copied endlessly on the page.
This happens every time I rewrite the SQL from scratch, how the hell do I stop this happening?
Here is the working clean code:
SELECT dbo_t_Person.PersonID
,dbo_t_Person.FullName
,dbo_t_Person.Address1
,dbo_t_Person.Address2
,dbo_t_Person.City
,dbo_t_Person.Zip
,dbo_t_Person.STATE
,dbo_t_Person.Country
,dbo_t_Person.Mobile
,dbo_t_Person.Phone
,dbo_t_Person.Email
FROM dbo_t_Person
WHERE (
(
(dbo_t_Person.PersonID) = [Forms]![from MICHAEL TEST WORKING]![OwnerIDEntry]
OR [Forms]![from MICHAEL TEST WORKING]![OwnerIDEntry] IS NULL
)
AND (
(dbo_t_Person.FullName) LIKE "*" & [Forms]![from MICHAEL TEST WORKING]![NameEntry] & "*"
OR [Forms]![from MICHAEL TEST WORKING]![NameEntry] IS NULL
)
)
And so on for the remaining entry fields
However if I can get the SQL back open again it it appears thousands of lines of
Or [Forms]![from MICHAEL TEST WORKING]![NameEntry] Is Null
for all entry fields is endlessly repeated.
Something is making the code copy end on end, how do I stop it?
Consider an adjusted WHERE clause with NZ() to handle if controls are empty or not.
WHERE dbo_t_Person.PersonID = NZ([Forms]![from MICHAEL TEST WORKING]![OwnerIDEntry],
dbo_t_Person.PersonID)
AND dbo_t_Person.FullName = LIKE "*" & NZ([Forms]![from MICHAEL TEST WORKING]![NameEntry],
dbo_t_Person.FullName) & "*"
Try changing your criteria to be more efficient and clean, like this:
IIF(ISNULL([Forms]![from MICHAEL TEST WORKING]![OwnerIDEntry]),TRUE,PersonID=[Forms]![from MICHAEL TEST WORKING]![OwnerIDEntry])
Since you are only dealing with a single table you can also do away with dbo_t_Person. from everywhere, like this:
SELECT PersonID,FullName,Address1,Address2,City,Zip,STATE,Country,Mobile,Phone,Email
FROM dbo_t_Person
Maybe the simplified version of the SQL will stop Access from corrupting it.

orientdb sql update edge?

I have been messing around with orientdb sql, and I was wondering if there is a way to update an edge of a vertex, together with some data on it.
assuming I have the following data:
Vertex: Person, Room
Edge: Inside (from Person to Room)
something like:
UPDATE Persons SET phone=000000, out_Inside=(
select #rid from Rooms where room_id=5) where person_id=8
obviously, the above does not work. It throws exception:
Error: java.lang.ClassCastException: com.orientechnologies.orient.core.id.ORecordId cannot be cast to com.orientechnologies.orient.core.db.record.ridbag.ORidBag
I tried to look at the sources at github searching for a syntax for bag with 1 item,
but couldn't find any (found %, but that seems to be for serialization no for SQL).
(1) Is there any way to do that then? how do I update a connection? Is there even a way, or am I forced to create a new edge, and delete the old one?
(2) When writing this, it came to my mind that perhaps edges are not the way to go in this case. Perhaps I should use a LINK instead. I have to say i'm not sure when to use which, or what are the implications involved in using any of them. I did found this though:
https://groups.google.com/forum/#!topic/orient-database/xXlNNXHI1UE
comment 3 from the top, of Lvc#, where he says:
"The suggested way is to always create an edge for relationships"
Also, even if I should use a link, please respond to (1). I would be happy to know the answer anyway.
p.s.
In my scenario, a person can only be at one room. This will most likely not change in the future. Obviously, the edge has the advantage that in case I might want to change it (however improbable that may be), it will be very easy.
Solution (partial)
(1) The solution was simply to remove the field selection. Thanks for Lvca for pointing it out!
(2) --Still not sure--
CREATE EDGE and DELETE EDGE commands have this goal: avoid the user to fight with underlying structure.
However if you want to do it (a little "dirty"), try this one:
UPDATE Persons SET phone=000000, out_Inside=(
select from Rooms where room_id=5) where person_id=8
update EDGE Custom_Family_Of_Custom
set survey_status = '%s',
apply_source = '%s'
where #rid in (
select level1_e.#rid from (
MATCH {class: Custom, as: custom, where: (custom_uuid = '%s')}.bothE('Custom_Family_Of_Custom') {as: level1_e} .bothV('Custom') {as: level1_v, where: (custom_uuid = '%s')} return level1_e
)
)
it works well

inserting variables into MySQL

I am trying to insert 2 scores into Mysql for two photos for a particular user that already exists in the database. The scores and the photos are both POST variables from a form. I am having great difficulty with the syntax - I am fairly certain the error is related to the position of quotes but despite searching here and finding similar questions I can't seem to get it working. Loathed to bother people with this but need some executive assistance.
$imageT=$_POST[randomimage]."T" ;
$imageH=$_POST[randomimage]."H" ;
$observerid=$_POST[scoreid];
$traction=$_POST[gradeT];
$honeycomb=$_POST[gradeH];
$sql="INSERT INTO scorers ('$imageT', '$imageH')
VALUES ('$imageT', '$imageH') WHERE id=$observerid ";
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
} else {
header("Location: testform.php");
} '
$imageT and $imageH are both integers with either T or H appended to them, for example 12T or 14H therefore I assumed they would be treated as strings and I put quotes around them. $traction, $honeycomb and $observerid are all integers. When I echo $imageT, $imageH, $traction, $honeycomb and $observerid the correct values are shown so I am assuming that there is no error in the these, just they way I am placing them within SQL code.
Very much appreciate any help (been learning PHP and My SQL for only 4 weeks so apologies).
At least three main problems at glance
You aren't using prepared statements
You are using WHERE clause in INSERT statement which is useless and erroneous. Either remove WHERE part or change your query to UPDATE.
You didn't post the error with your question. Which you always have to. Error messages is a cornerstone of troubleshooting.

web.py db.insert() doesn't work

I use db.insert() to insert data to database, the code is something like this,
db.insert('categories', name=cate_name, description=desc, _test=True)
but it doesn't work, the data can't not be found in table 'categories' after the code is execute, and no exceptions by the way.
Anybody know why this happened?
_Test variable stands for debug purposes.
It lets you get SQL statement instead of executing one.
It means that your command
result = db.insert('categories', name=cate_name, description=desc, _test=True)
will not execute anything on your DB. It will only return a string:
"INSERT INTO categories (name, description) VALUES ('cate_name value', 'desc value')"
If you want to make a real query, you need to remove it:
db.insert('categories', name=cate_name, description=desc)
It should work.
remove _test=True or set _test=False

How can I get a null field from the Twitter API to play nice with my database?

Forgive me if I could have any sort of fundamental error here. I'd imagine there's something simple I'm missing. I'm looking to store Twitter updates in a database with only a few fields: an auto-increment index, the time posted, the actual status update & the user id the update is in reply to.
I'm simply storing this last field so I can provide a method of filtering out replies.
But it appears that my SQL code is throwing an error. As of this writing, the SQL properly inserts the two most recent updates, which are both replies to another user and, therefore, have data in the in_reply_to_user_id field. But on the third update, which is not in reply to anyone, I get the following error:
Error Number: 1064
You have an error in your SQL syntax;
check the manual that corresponds to
your MySQL server version for the
right syntax to use near ')' at line 1
INSERT IGNORE INTO updates (time,
status, postid, reply) VALUES
(1260070319, 'I guess Johnny Cash knew
what he was talking about in that \"A
Boy Named Suh\" song. That guy is both
fast and mean.', '6389320556', )
Twitter's API states no default value for this parameter. I tried the same query with the "favorited" parameter, and it correctly labeled each row with "false" in my database. So I'm assuming my problem is with inserting an empty string.
For what it's worth, here is my CodeIgniter method:
function insert_tweet($tweet){
foreach($tweet as $t) {
$when = strtotime($t->created_at);
$status = $t->text;
$postid = $t->id;
$reply = $t->in_reply_to_user_id;
$sql = 'INSERT IGNORE INTO updates (time, status, postid, reply) VALUES (?, ?, ?, ?)';
$this->db->query($sql, array($when, $status, $postid, $reply));
}
}
Any help you could give would be great! I hope I've provided enough information!
More info: I should also note that CodeIgniter is throwing the following error:
A PHP Error was encountered
Severity: 4096
Message: Object of class stdClass
could not be converted to string
Filename: database/DB_driver.php
Line Number: 598
In the off chance that this proves some sort of CI idiosyncrasy to be at fault.
I don't know php but in asp.net style pseudo code i would do this...
$this->db->query($sql, array($when, $status, $postid, string.IsNullOrEmpty($reply) ? null : $reply));
Hopefully you can adapt, just need to deal with the empty parameter.
I don't speak codeigniter, but I can tell you how to fix your problem.
You have two options
Check is the string empty, if so, instead of having a blank value, put in the null keyword.
Check is the value empty, if so, leave that column out of the insert statement's cloumns and values.
Hope this helps
I'm not sure about codeigniter (never used it) but I know that you will need to get the null value to appear as NULL
function insert_tweet($tweet){
foreach($tweet as $t) {
$when = strtotime($t->created_at);
$status = $t->text;
$postid = $t->id;
$reply = (is_null($t->in_reply_to_user_id) ? 'NULL' : $t->in_reply_to_user_id);
$sql = 'INSERT IGNORE INTO updates (time, status, postid, reply) VALUES (?, ?, ?, ?)';
$this->db->query($sql, array($when, $status, $postid, $reply));
}
}
I'm not sure if that will produce the desired SQL:
INSERT IGNORE INTO updates (time, status, postid, reply) VALUES (1260070319, 'I guess Johnny Cash knew what he was talking about in that \"A Boy Named Suh\" song. That guy is both fast and mean.', '6389320556', NULL)
edit Also make sure that the field in the database can be null.
Hope this helps..
Why not alter the last column and make it default to NULL?
When you want to insert some record without this field you just ignore it in the 'INSERT' statement.
And be sure not to put a pending comma between the last column value and the right parenthisis.
:)
You're coming from the CodeIgniter forums right? I think the issue is coming from your Twitter library. Can you post that code? For somereason, it's returning a NULL status as an Object, which it shouldn't do.
Hey guys, the answers here seemed to be close, but no cigar. I got some help from the great folks on the CodeIgniter forums, and here's what I came up with:
$reply = (is_null($t->in_reply_to_user_id) || is_object($t->in_reply_to_user_id) ? ‘NULL’ : $t->in_reply_to_user_id);
Essentially, like Zack said, it is returning the empty field from the Twitter API as an empty object instead of null. So I needed to check if this is either null or an object, then return NULL. And if not, return the user_id string.
Thanks a ton for your help!