=====SQL===== Create the SQL as a text variable, using in-line variable where required: $sql = 'SELECT field FROM table WHERE 1'; =====DELETE or UPDATE===== if ($mysqli->query($sql) === false) { $log = $_SERVER['PHP_SELF'].' SQL ERROR: '.$mysqli->error.' ** SQL: '.$sql; f_log($log); } else { $rows = $mysqli->affected_rows; } =====INSERT===== if ($mysqli->query($sql) === false) { $log = $_SERVER['PHP_SELF'].' SQL ERROR: '.$mysqli->error.' ** SQL: '.$sql; f_log($log); } else { $newkey=$mysqli->insert_id; } =====SELECT===== if (($result = $mysqli->query($sql)) === false) { $errmsg .= '

TRX DESC: ' . $mysqli->error . '

' . $sql . '

'; echo $errmsg; } elseif (!$result->num_rows) { //no records } else { $row = $result->fetch_assoc(); while ($row = $result->fetch_assoc()) { } }
Bootstrap Version if (($result = $mysqli->query($sql)) === false) { $log = $_SERVER['PHP_SELF'].' SQL ERROR: '.$mysqli->error.' ** SQL: '.$sql; f_log($log); } elseif (!$result->num_rows) { //no records } else { $row = $result->fetch_assoc(); while ($row = $result->fetch_assoc()) { } } OLD Version if (($result = mysql_query($sql)) == FALSE) { $error = mysql_error(); } elseif (!mysql_num_rows($result)) { } else { //$id = mysql_insert_id(); while ($row = mysql_fetch_assoc($result)) { } } =====TRANSACTION===== try { $mysqli->autocommit(false); //start the transaction //first SQL $sql = 'UPDATE tablename SET ' . 'field=1 ' . 'WHERE field=' . $target; if ($mysqli->query($sql) === false) { throw new Exception($mysqli->error); } //second SQL $sql = 'UPDATE tablename SET ' . 'field=2 ' . 'WHERE field=' . $target; if ($mysqli->query($sql) === false) { throw new Exception($mysqli->error); } //catch any errors } catch (Exception $e) { //roll back the changes thus far $mysqli->rollback(); //optionally terminate the page die($e); } //no errors found, so turn on autocommit which will also process all of the pending transactions $mysqli->autocommit(TRUE); =====TRANSACTION (Alternate)===== //first SQL $sql1 = 'UPDATE tablename SET ' . 'field=4 ' . 'WHERE field=' . $target; $sql2 = 'UPDATE tablename SET ' . 'field=5 ' . 'WHERE field=' . $target; try { $mysqli->autocommit(false); //start the transaction if ($mysqli->query($sql1) === false) { throw new Exception('

TRX1: ' . $mysqli->error . '

' . $sql . '

'); } //a select transaction if (($result = $mysqli->query($sql)) === false) { throw new Exception('

TRX2: ' . $mysqli->error . '

' . $sql . '

'); } //catch any errors } catch (Exception $e) { //roll back the changes thus far $mysqli->rollback(); //optionally terminate the page die($e); } //no errors found, so turn on autocommit which will also process all of the pending transactions $mysqli->autocommit(TRUE);
=====Copy Record===== newtable also has an idfield that is auto-increment, primary key but different from the idfield of the original table //copy the record to the deleted load table $sql = 'INSERT INTO newtable (`idfield`, `field2`, `field3`) '. 'SELECT `idfield`, `field2`, `field3` FROM originaltable WHERE idfield = '.$target; ===== Table Format ===== $page = '
'.chr(13). '
'.chr(13). ''.chr(13). '
'.chr(13). '
'; $page .= ''.chr(13). ''.chr(13). ''.chr(13). ''.chr(13). ''.chr(13). ''.chr(13). ''.chr(13). ''.chr(13). ''.chr(13); while ($row = $result->fetch_assoc()) { $page .= ''.chr(13). '' . chr(13) . '' . chr(13) . '' . chr(13) . '' . chr(13) . chr(13); } $page .= '' . chr(13) . '
'.$row[''].''.$row[''].''.$row[''].'
' . chr(13) . chr(13);