function getDBCon($force = 0) { if ($force || !isset($this->a['db_con'])) { if (!$this->createDBCon()) { return false; } } if (!$force && !@mysql_thread_id($this->a['db_con'])) { return $this->getDBCon(1); } return $this->a['db_con']; }
function sql_disconnect_slave() { global $opt, $db; if ($db['dblink_slave'] === false) { return; } if ($opt['db']['pconnect'] == true && $db['dblink'] !== false) { if (count($db['temptables']) > 0) { foreach ($db['temptables'] as $k => $table) { if (isset($db['temptables_slave'][$table])) { sqlf_slave("DROP TEMPORARY TABLE IF EXISTS &tmpdb.`&1`", $table); unset($db['temptables_slave'][$table]); unset($db['temptables'][$k]); } } if (count($db['temptables_slave']) > 0) { sqlf_slave("DELETE FROM &db.`sys_temptables` WHERE `threadid`='&1'", mysql_thread_id($db['dblink_slave'])); } $db['temptables_slave'] = array(); } } if ($db['dblink'] === $db['dblink_slave']) { $db['dblink_slave'] = false; return; } //is connected and no persistent connect used? if ($opt['db']['pconnect'] == false && $db['dblink_slave'] !== false) { mysql_close($db['dblink_slave']); } $db['dblink_slave'] = false; }
function db_thread_id() { return mysql_thread_id(); }
/** * get thread id. * get the mysql thread id of the current connection * when one gets reconnected the threadid will be changed * * @access public * @return void */ public function getThreadId() { if ($this->connect()) { return mysql_thread_id($this->_connection); } return null; }
} echo "Explicit connection on close\n"; $link = my_mysql_connect($host, $user, $passwd, $db, $port, $socket); $link1_thread_id = mysql_thread_id($link); $default1_thread_id = mysql_thread_id(); echo 'Expect same thread id for $link and default conn: '; var_dump($link1_thread_id == $default1_thread_id); var_dump($link); mysql_close($link); var_dump($link); // we sohuld have no default link anymore mysql_close(); echo "\nClosing default link\n"; $link = my_mysql_connect($host, $user, $passwd, $db, $port, $socket); $link2_thread_id = mysql_thread_id($link); $default2_thread_id = mysql_thread_id(); echo 'Expect same thread id for $link and default conn but not the previous: '; var_dump($link1_thread_id == $default1_thread_id && $link1_thread_id != $link2_thread_id); var_dump($link); mysql_close(); var_dump($link); mysql_close($link); var_dump($link); echo "\nExplicit resource and pconnect\n"; $link = my_mysql_pconnect($host, $user, $passwd, $db, $port, $socket); var_dump($link); mysql_close($link); var_dump($link); // we sohuld have no default link mysql_close(); echo "\nDefault link and pconnect\n";
<?php require_once 'connect.inc'; $conn = mysql_connect($host, $user, $passwd); var_dump(mysql_thread_id() > 0);
function threadId() { /* 返回当前线程的 ID */ return mysql_thread_id($this->LinkId); }
include "table.inc"; if ($socket) { $myhost = sprintf("%s:%s", $host, $socket); } else { if ($port) { $myhost = sprintf("%s:%s", $host, $port); } else { $myhost = $host; } } if (!($plink = mysql_pconnect($myhost, $user, $passwd))) { printf("[001] Cannot connect to the server using host=%s/%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n", $host, $myhost, $user, $db, $port, $socket); } mysql_select_db($db, $plink); $pthread_id = mysql_thread_id($plink); $thread_id = mysql_thread_id($link); if (!($res = mysql_query("SHOW FULL PROCESSLIST", $link))) { printf("[002] Cannot get processlist, [%d] %s\n", mysql_errno($link), mysql_error($link)); } $processlist = array(); while ($row = mysql_fetch_assoc($res)) { $processlist[$row['Id']] = $row; } mysql_free_result($res); if (!isset($processlist[$thread_id])) { printf("[003] Cannot find regular connection thread in process list, [%d] %s\n", mysql_errno($link), mysql_error($link)); } if (!isset($processlist[$pthread_id])) { printf("[004] Cannot find persistent connection thread in process list, [%d] %s\n", mysql_errno($link), mysql_error($link)); } if (!mysql_query(sprintf("KILL %d", $pthread_id), $link)) {
/** * stores query, filenames, errors in TradeBanqDb::queries and TradeBanqDb::errors * @param string $query * @return void */ private function end_query($query, $records = 0) { if ($this->enable_log) { $error = mysql_error(); $this->queries[] = array('start' => date("Y-m-d H:i:s"), 'query' => $query, 'records' => $records, 'time' => microtime(true) - $this->start_time, 'error' => $error, 'connection' => $this->last_connection_used && $this->master_connection && mysql_thread_id($this->last_connection_used) == mysql_thread_id($this->master_connection) ? "master" : "slave"); } $this->start_time = 0; }
/** * Retrieve the current thread id * * @return @e integer */ public function getThreadId() { return mysql_thread_id($this->connection_id); }
printf("FAILURE: [%d] %s\n", mysql_errno(), mysql_error()); } else { print "SUCCESS: connect\n"; } if (!mysql_select_db($db, $con)) { printf("FAILURE: [%d] %s\n", mysql_errno($con), mysql_error($con)); } $id_default = mysql_thread_id(); $id_con = mysql_thread_id($con); if ($id_default != $id_con) { printf("FAILURE: Different values for default and specified connection\n"); } if (!is_int($id_con)) { printf("FAILURE: Function should have returned an integer value, got %s value\n", gettype($id_con)); } $id_con = mysql_thread_id($illegal_link_identifier); if (!is_null($id_con)) { printf("FAILURE: Function should have returned a NULL value, got %s value\n", gettype($id_con)); } if ($id_con !== NULL) { printf("FAILURE: Should return NULL because of illegal link identifier\n"); } mysql_close($con); ?> --EXPECT-EXT/MYSQL-OUTPUT-- SUCCESS: connect --EXPECT-EXT/MYSQL-PHP-ERRORS-- --EXPECT-EXT/MYSQLI-OUTPUT-- SUCCESS: connect
/** * Starts a timer which will kill the DB thread after $timeout seconds */ function startTimer($timeout) { global $IP; if (function_exists('mysql_thread_id')) { # This will kill the query if it's still running after $timeout seconds. $tid = mysql_thread_id($this->mConn); exec("php {$IP}/includes/killthread.php {$timeout} {$tid} &>/dev/null &"); } }
<?php require_once "connect.inc"; require_once "table.inc"; if ($plink = my_mysql_connect($host, $user, $passwd, $db, $port, $socket, NULL, true)) { printf("[001] Can connect to the server.\n"); } if (($res = mysql_query('SELECT id FROM test ORDER BY id ASC', $plink)) && ($row = mysql_fetch_assoc($res)) && mysql_free_result($res)) { printf("[002] Can fetch data using persistent connection! Data = '%s'\n", $row['id']); } $thread_id = mysql_thread_id($plink); mysql_close($plink); if (!($plink = my_mysql_connect($host, $user, $passwd, $db, $port, $socket, NULL, true))) { printf("[003] Cannot connect, [%d] %s\n", mysql_errno(), mysql_error()); } if (mysql_thread_id($plink) != $thread_id) { printf("[004] Looks like the second call to pconnect() did not give us the same connection.\n"); } $thread_id = mysql_thread_id($plink); mysql_close($plink); if (!($plink = my_mysql_connect($host, $user, $passwd, $db, $port, $socket))) { printf("[005] Cannot connect, [%d] %s\n", mysql_errno(), mysql_error()); } if (mysql_thread_id($plink) == $thread_id) { printf("[006] Looks like connect() did not return a new connection.\n"); } print "done!"; require_once "clean_table.inc";
} mysql_free_result($res); var_dump($row); // change the password for the second DB user and kill the persistent connection if (!($res = mysql_query("SELECT CURRENT_USER() AS _user", $link))) { printf("[006] [%d] %s", mysql_errno($link), mysql_error($link)); } $row = mysql_fetch_assoc($res); mysql_free_result($res); $host = substr($row['_user'], strrpos($row['_user'], "@") + 1, strlen($row['_user'])); $sql = sprintf('SET PASSWORD FOR pcontest@"%s" = PASSWORD("newpass")', mysql_real_escape_string($host, $link)); if (!mysql_query($sql, $link)) { printf("[007] Cannot change PW of second DB user, [%d] %s\n", mysql_errno($link), mysql_error($link)); } // persistent connections cannot be closed but only be killed $pthread_id = mysql_thread_id($plink); if (!mysql_query(sprintf('KILL %d', $pthread_id), $link)) { printf("[008] Cannot KILL persistent connection of second DB user, [%d] %s\n", mysql_errno($link), mysql_error($link)); } // give the server a second to really kill the thread sleep(1); if (!($res = mysql_query("SHOW FULL PROCESSLIST", $link))) { printf("[009] [%d] %s\n", mysql_errno($link), mysql_error($link)); } $running_threads = array(); while ($row = mysql_fetch_assoc($res)) { $running_threads[$row['Id']] = $row; } mysql_free_result($res); if (isset($running_threads[$pthread_id])) { printf("[010] Persistent connection has not been killed\n");
/** * Test pconnect * * @return boolean */ public function MySQL_Pconnect_Test() { // We need to make sure that the connection ids are the same $lastMySQLId = false; $lastOurId = false; for ($x = 0; $x <= 5; $x++) { // Connect $mysql = mysql_pconnect(TEST_HOST, TEST_USER, TEST_PASS); $ourDb = $this->_object->mysql_pconnect(TEST_HOST, TEST_USER, TEST_PASS); // Keep track of resource $this->_cached[] = $mysql; // Get thread ids $thisMySQLId = mysql_thread_id(); $thisOurId = $this->_object->mysql_thread_id(); // Get original ids if not set if ($lastMySQLId == false) { $lastMySQLId = $thisMySQLId; $lastOurId = $thisOurId; } // Keep checking that the ids are the same if ($thisMySQLId !== $lastMySQLId || $thisOurId !== $lastOurId) { return false; } } return true; }
protected function getThreadId() { return mysql_thread_id($this->db_Conn); }
<?php include_once "connect.inc"; $tmp = NULL; $link = NULL; if (!is_null($tmp = @mysql_thread_id($link))) { printf("[001] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp); } require 'table.inc'; if (!is_int($id = mysql_thread_id($link)) || 0 === $id) { printf("[002] Expecting int/any but zero, got %s/%s. [%d] %s\n", gettype($id), $id, mysql_errno($link), mysql_error($link)); } if (!is_int($id_def = mysql_thread_id()) || 0 === $id_def) { printf("[003] Expecting int/any but zero, got %s/%s. [%d] %s\n", gettype($id_def), $id_def, mysql_errno(), mysql_error()); } assert($id === $id_def); mysql_close($link); if (false !== ($tmp = mysql_thread_id($link))) { printf("[003] Expecting boolean/false, got %s/%s\n", gettype($tmp), $tmp); } print "done!";
function search_updatelist($options = '') { global $win32, $setctl, $bd, $runinit; kprintheader(get_lang(17), 1, 0); $updateall = false; @ini_set('output_buffering', '1'); if (isset($options['deleteunused'])) { $deleteunused = 1; } else { $deleteunused = 0; } if (isset($options['debugmode'])) { $debugmode = 1; } else { $debugmode = 0; } if (isset($options['sleeppertrans'])) { $sleeptrans = $options['sleeppertrans']; } else { $sleeptrans = 0; } if (isset($options['rebuildid3'])) { $updateall = true; } $setctl->publish('updusecache'); $setctl->publish('followsymlinks'); $db_out = $db_mtime = $db_unique = $db_path = array(); $filecntr = 0; $file = ''; $fixurl = 'http://www.kplaylist.net/forum/viewtopic.php?p=3672'; echo '<font class="notice">' . get_lang(296, '<a href="' . $fixurl . '" target="_blank">' . $fixurl . '</a>') . '</font><br/><br/>'; echo '<font class="notice">' . get_lang(136) . '..</font><br/>'; flush(); $data = array(); $datacnt = 0; if ($debugmode) { @ini_set('display_errors', 'On'); echo '<!-- update debug step 0 - mem limit detected: ' . @ini_get('memory_limit') . ' -->'; } $kqm = new kq_Measure(); $kqm->start(); echo '<br/><div id="up_status2" class="notice"></div>'; echo '<br/><div id="up_status" class="notice"></div><br/>'; $cnt = 0; if ($runinit['astream']) { if (!updaterunning()) { $setctl->set('updatemid', mysql_thread_id()); } else { die('Another update is currently running.'); } } for ($i = 0, $c = $bd->getcnt(); $i < $c; $i++) { if ($bd->gtype($i) == 'l') { $grabdata = array(); if ($debugmode) { echo '<!-- update debug step 1 - grabbing filelist from ' . $bd->getpath($i) . ' -->'; } $stripc = strlen($bd->getpath($i)); GetDirArray($bd->getpath($i), $grabdata, $cnt, $stripc); $data[$i] = $grabdata; $datacnt += count($data[$i]); } } if ($debugmode) { echo '<!-- update debug step 2 -->'; } if ($datacnt > 0) { $result = db_execquery('SELECT count(*) FROM ' . TBL_SEARCH . ' WHERE xid = 0'); $row = mysql_fetch_row($result); $dbrows = $row[0]; $result = db_execquery('SELECT fsize, id, md5, fname, drive, mtime, fpath FROM ' . TBL_SEARCH . ' WHERE xid = 0 ORDER BY id ASC', true); $dcntr = 0; updateup_status(get_lang(314, $dcntr, $dbrows)); while ($row = mysql_fetch_row($result)) { if (UPDUSECACHE) { $db_out[$dcntr] = $row; } else { $db_out[$dcntr] = array($row[0], $row[1]); } if ($dcntr % 50 == 0) { updateup_status(get_lang(314, $dcntr, $dbrows)); } if (!isset($db_mtime[$row[0]][$row[5]])) { $db_mtime[$row[0]][$row[5]] = $dcntr; } if (!isset($db_unique[$row[0]][$row[2]])) { $db_unique[$row[0]][$row[2]] = $dcntr; } if (strlen($row[6]) != 0 || strlen($row[3]) != 0) { $path = crc32($row[6] . $row[3]); if (isset($db_path[$path])) { $ids = $db_path[$path]; $ids[] = $dcntr; $db_path[$path] = $ids; } else { $db_path[$path] = array($dcntr); } } $dcntr++; } updateup_status(get_lang(314, $dcntr, $dbrows), 'up_status'); db_free($result); if ($debugmode) { echo '<!-- update debug step 3 -->'; } if ($updateall) { $db_mtime = array(); } $query = null; updateup_status(get_lang(18, $datacnt)); $totalqupds = $dcntr; $totalins = $datacnt; $qins = $qupd = $failed = $qupdins = $skips = $qdels = $fdups = 0; $rowinsertid = -1; if ($datacnt > 0) { for ($drive = 0, $drivec = $bd->getcnt(); $drive < $drivec; $drive++) { if ($bd->gtype($drive) == 'l') { for ($i = 0, $ic = count($data[$drive]); $i < $ic; $i++) { $filein = $data[$drive][$i]; $file = $bd->getpath($drive) . $filein; if ($i % 50 == 0 || $debugmode) { $countups = $qupd + $qupdins; $out = get_lang(20, $qins, $countups); $out .= kp_strlen($filein) > 60 ? addslashes(kp_substr($filein, 0, 60)) . '...' : addslashes($filein); updateup_status($out, 'up_status'); } $fsize = filesize($file); $mtime = filemtime($file); if (!$fsize) { echo '<font class="notice">' . get_lang(19, $file) . '</font><br/>'; flush(); $skips++; continue; } $filecntr++; if (isset($db_mtime[$fsize][$mtime])) { $i2 = $db_mtime[$fsize][$mtime]; if (UPDUSECACHE) { $userow = $db_out[$i2]; } else { $userow = getsrow($db_out[$i2][1]); } if ($db_out[$i2][0] != -1 && $userow[6] . $userow[3] == $filein && $userow[4] == $drive) { $db_out[$i2][0] = -1; $qupd++; continue; } } $md5 = md5file($file); if ($sleeptrans > 0 && !$win32) { usleep($sleeptrans); } if (!empty($md5)) { if (isset($db_unique[$fsize][$md5])) { $idupdate = $db_unique[$fsize][$md5]; if ($db_out[$idupdate][0] == -1) { if (UPDUSECACHE) { $userow = $db_out[$idupdate]; } else { $userow = getsrow($db_out[$idupdate][1]); } $checkf = $bd->getpath($userow[4]) . $userow[6] . $userow[3]; /* duplicate check - not in production - uncomment to use. But, do not use if you do not understand what it does. $identical = false; $fp1 = fopen($checkf, 'rb'); $fp2 = fopen($file, 'rb'); if ($fp1 && $fp2) { $identical = true; while (!feof($fp1)) { $data1 = fread($fp1, 32768); $data2 = fread($fp2, 32768); if (strcmp($data1,$data2) != 0) { $identical = false; break; } } fclose($fp1); fclose($fp2); } if ($identical) $extra = '! '; else $extra = '';*/ echo '<font class="notice">' . get_lang(168, $file, $checkf) . '</font><br/><br/>'; $fdups++; continue; } } else { $idupdate = -1; } if ($idupdate != -1) { $db_out[$idupdate][0] = -1; if (UPDUSECACHE) { $userow = $db_out[$idupdate]; } else { $userow = getsrow($db_out[$idupdate][1]); } if ($updateall) { $userow[5] = 0; } if ($mtime != $userow[5]) { $fid = get_file_info($file); $query = search_qupdorins($db_out[$idupdate][1], $fid, $filein, $md5, $drive, $mtime, 0, $fsize, time()); $qupdins++; } else { if ($userow[6] . $userow[3] != $filein || $userow[4] != $drive) { $query = search_qupdfree($filein, $drive, $db_out[$idupdate][1]); $qupdins++; } } } else { $frel = getrelative($filein); $ffilein = kp_basename($filein); $checkex = crc32($frel . $ffilein); $useid = -1; if (isset($db_path[$checkex])) { $ids = $db_path[$checkex]; for ($i3 = 0, $c3 = count($ids); $i3 < $c3; $i3++) { $cid = $ids[$i3]; if (UPDUSECACHE) { $userow = $db_out[$cid]; } else { $userow = getsrow($db_out[$cid][1]); } if ($userow[3] == $ffilein && $userow[6] == $frel) { $useid = $cid; break; } } } $fid = get_file_info($file); if ($useid == -1) { $query = search_qupdorins(0, $fid, $filein, $md5, $drive, $mtime, 0, $fsize, time()); if (UPDUSECACHE) { $db_out[$dcntr] = array(-1, 0, $md5, $filein, $drive, $mtime, $filein); } else { $db_out[$dcntr] = array(-1, 0); } $db_unique[$fsize][$md5] = $dcntr; $rowinsertid = $dcntr; $dcntr++; $qins++; } else { $query = search_qupdorins($db_out[$useid][1], $fid, $filein, $md5, $drive, $mtime, 0, $fsize, time()); $db_out[$useid][0] = -1; $qupdins++; } } if ($query !== null) { $result = db_execquery($query, true); if (!$result) { $failed++; echo '<font class="wtext">' . get_lang(22, $query) . '</font><br/>'; } else { if ($rowinsertid != -1) { $db_out[$rowinsertid][1] = mysql_insert_id(); $rowinsertid = -1; } } $query = null; } } else { echo '<font class="notice">' . get_lang(23, $file) . '</font><br/>'; flush(); $skips++; } } // end of file loop } // if local drive } // end of drive loop } // if found any files $fordel = 0; for ($i2 = 0; $i2 < $dcntr; $i2++) { if ($db_out[$i2][0] != -1) { $fordel++; } } if ($deleteunused) { if ($skips == 0) { for ($i2 = 0; $i2 < $dcntr; $i2++) { if ($db_out[$i2][0] != -1) { if (UPDUSECACHE) { $userow = $db_out[$i2]; } else { $userow = getsrow($db_out[$i2][1]); } echo '<font class="notice">' . get_lang(24, $userow[6] . $userow[3]); $result = db_execquery('DELETE FROM ' . TBL_SEARCH . ' WHERE id = ' . $db_out[$i2][1], true); if ($result) { $qdels++; } echo '</font><br/>'; $fordel = 0; } } echo '<br/>'; } else { if ($fordel > 0) { echo '<font class="notice">' . get_lang(335) . '</font><br/>'; } } } else { $one = false; for ($i2 = 0; $i2 < $dcntr; $i2++) { if ($db_out[$i2][0] != -1) { $one = true; if (UPDUSECACHE) { $userow = $db_out[$i2]; } else { $userow = getsrow($db_out[$i2][1]); } echo '<font class="notice">' . get_lang(315, $userow[6] . $userow[3]) . '</font><br/>'; } } if ($one) { echo '<br/>'; } } $kqm->stop(); updateup_status(get_lang(26), 'up_status'); echo '<font class="notice">' . get_lang(25, $qins, $qupdins, $qdels, $failed, $skips, $filecntr, $kqm->result(3), $fordel); echo '</font><br/><br/>'; } else { for ($i = 0, $c = $bd->getcnt(); $i < $c; $i++) { if ($bd->gtype($i) == 'l') { echo '<font class="notice">' . get_lang(28, $bd->getpath($i)) . '</font><br/>'; } } } // network update && clean up for ($i = 0, $c = $bd->getcnt(); $i < $c; $i++) { if ($bd->gtype($i) == 'n') { updateup_status(get_lang(253)); updateup_status('', 'up_status'); $kpn = new kpnetwork(); if ($kpn->setdrive($i)) { if ($kpn->checklogin()) { $cnt = $kpn->preparesync($i); $host = $kpn->getnetworkhost(); if ($cnt > 0) { updateup_status(get_lang(347, $host->geturl(), $cnt)); if ($kpn->genchlist($updateall, $deleteunused) > 0) { $kpn->dosync(); } updateup_status(get_lang(348, $host->geturl(), get_lang(181)), 'up_status'); } else { if ($cnt == -1) { updateup_status(get_lang(348, $host->geturl(), $kpn->geterrorstr()), 'up_status'); } } } else { updateup_status(get_lang(348, $host->geturl(), $kpn->geterrorstr()), 'up_status'); } } } else { if ($bd->gtype($i) == 'l') { if ($deleteunused) { $res = db_execquery('SELECT count(*) as cnt FROM ' . TBL_SEARCH . ' WHERE xid != 0 AND drive = ' . $i); $row = mysql_fetch_assoc($res); if ($row['cnt'] > 0) { $res = db_execquery('DELETE FROM ' . TBL_SEARCH . ' WHERE xid != 0 AND drive = ' . $i); } } } } } echo '<input type="button" value="' . get_lang(27) . '" name="close" class="fatbuttom" onclick="javascript: self.close();"/><br/><br/>'; if ($runinit['astream']) { $setctl->set('updatemid', 0); } $setctl->set('basedir_changed', 0); cache_updateall(); kprintend(); }