Oh no! Where's the JavaScript?
Your Web browser does not have JavaScript enabled or does not support JavaScript. Please enable JavaScript on your Web browser to properly view this Web site, or upgrade to a Web browser that does support JavaScript.
Not a member yet? Click here to register.
Forgot Password?

How to print user-friendly message on dbquery error

Asked Modified Viewed 2,409 times
M
mawe4585
M
  • Member, joined since
  • Contributed 84 posts on the community forums.
  • Started 34 threads in the forums
  • Started this discussions
asked
Member

Hello!

I want to enter data into database with something like this:


dbquery("insert into ".DB_MYTABLE."(unival,val2,val3)values('test','test','test')");


The column "unival" has a Unique-Key on it, which means i can't insert the same value twice.
Now if i do insert the same value a second time, i get this message:
"Duplicate entry 'test' for key 'unival'"

So i tried some things, but can't figure out how this works:

if(!dbquery(...)){
  echo "unival already exists";
}


try{
  dbquery(...);
}catch(Exception $e){
  if(stripos($e->getMessage(),"Duplicate")){
     echo "unival already exists";
  }
}


Both don't work. So how do i do it right?
0 replies

1 post

Y
Yodix
Y
Yodix 10
Happy Hunger Games! And may the odds be ever in your favor.
  • Member, joined since
  • Contributed 62 posts on the community forums.
  • Started 20 threads in the forums
answered
Member

Try this way
<?php
if (dbcount("(*)", DB_MYTABLE, "unival='test' AND val2='test' AND val3='test'") == 0) {
   dbquery("INSERT INTO ".DB_MYTABLE."(unival,val2,val3) VALUES ('test','test','test')");
} else {
   echo "unival already exist";
}
?>
0 replies

Category Forum

Panels and Infusions

Labels

None yet

Statistics

  • Views 0 views
  • Posts 1 post
  • Votes 0 votes
  • Topic users 2 members

2 participants

M
M
  • Member, joined since
  • Contributed 84 posts on the community forums.
  • Started 34 threads in the forums
  • Started this discussions
Y
Y
Yodix 10
Happy Hunger Games! And may the odds be ever in your favor.
  • Member, joined since
  • Contributed 62 posts on the community forums.
  • Started 20 threads in the forums

Notifications

Track thread

You are not receiving notifications from this thread.

Related Questions

Not yet