d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Anyone Experienced With Smf Functions? > Smcfunc Db_insert
Add Reply New Topic New Poll
Member
Posts: 299
Joined: Aug 17 2012
Gold: 200.00
Aug 31 2012 04:52am
my mysql code:

Code
INSERT INTO `table` (Username)
SELECT 'name' FROM `table`
WHERE NOT EXISTS (SELECT * FROM `table`
WHERE Username = 'name')
LIMIT 1



Quote
db_insert
Usage $smcFunc['db_insert'] (method, table, columns, data, keys, disable_trans, connection)

Method tells how to change the data. Accepts "replace" "ignore" or "insert".


i thought about using replace - however i don't need to replace data. i just need to check if it exists, and if it does then skip it.

so my question is if it's possible to conditionally insert data into mysql using the smf function db_insert, how would i go about doing it?




e.g.
Code
}elseif($mode == "insert"){
$smcFunc['db_insert']('insert', 'table',
        array('Username' => 'string'),
 array($account),
 array());

,

This post was edited by Targe on Aug 31 2012 05:01am
Member
Posts: 299
Joined: Aug 17 2012
Gold: 200.00
Aug 31 2012 07:06am
If anyone's interested i mde a little work around for the time being lol
Code
 $exists = 0;
     $resultaa = $smcFunc['db_query']('', "SELECT * FROM `table` WHERE `Username` = '".$account."'", array());
      while($rowyy = mysql_fetch_array($resultaa)){
       echo $rowyy['Username'];
       if ($rowyy['Username'] == $account){
        $exists = 1;
       }
      }
      if ($exists == 0){
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Aug 31 2012 11:02am
generally you either first select to see if it's there (like your example) or you use an INSERT-SELECT clause
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll