gpt4 book ai didi

PHP文件无法输入部分代码

转载 作者:行者123 更新时间:2023-11-29 06:05:46 26 4
gpt4 key购买 nike

这是我的 php 文件中的一个函数,用于服务我的 Android 应用程序的请求。

function checkin($DB, $TechID, $ClientID, $SiteID){
$dbConnection = mysql_connect($DB['server'], $DB['loginName'], $DB['password']);
if(!$dbConnection){
die('Error! ' . mysql_error());
}
mysql_select_db($DB['database'], $dbConnection);

$file2 = "C:/wamp/www/file2.txt";
$data2 = "ClientID:".$ClientID." TechID:".$TechID." SiteID:".$SiteID;
file_put_contents($file2, $data2);

$result1 = mysql_query("SELECT COUNT(*) FROM Log") or die('Error! ' . mysql_error());
$query = "SELECT `Type` FROM `Log` WHERE `TechID` = '".$TechID."' ORDER BY LogTime DESC LIMIT 1";
$file5 = "C:/wamp/www/file5.txt";
file_put_contents($file5, $query);
$result2 = mysql_query($query) or die('Error! ' . mysql_error());
while($row1 = mysql_fetch_array($result1)){
$count = $row1['COUNT(*)'];
$file3 = "C:/wamp/www/file3.txt";
$data3 = "ClientID:".$ClientID." TechID:".$TechID." SiteID:".$SiteID." Count:".$count;
file_put_contents($file3, $data3);
while($row2 = mysql_fetch_array($result2)){
$file4 = "C:/wamp/www/file4.txt";
$data3 = "ClientID:".$ClientID." TechID:".$TechID." SiteID:".$SiteID." Count:".$count;
file_put_contents($file4, $data3);
/*if($row2['Type']!="Checkin"){
$count = $count+1;
$Time = date('Y/m/d H:i');
mysql_query("INSERT INTO Log (LogID, TechID, ClientID, SiteID, LogTime, Type)
VALUES (".$count.", ".$TechID.", ".$ClientID.", ".$SiteID.", ".$Time.", Checkin)");
}else{
$query2 = "SELECT TechEmail FROM Tech WHERE TechID=".$TechID;
$result3 = mysql_query($query2) or die('Error! ' . mysql_error());
$subject = "Please check out";
$message = "You have forgot to logout from the last site, please check out manually";
$from = "devadmin@uniserveit.com";
$header = "Form:".$from;
while($row3 = mysql_fetch_array($result3)){
mail($row3['TechEmail'], $subject, $message, $header);
}
}*/
}
}
}

你可以看到我隐藏了一些代码,因为我正在调试它,所以我创建了一些文件只是为了看看哪部分代码无法执行。我发现程序无法进入应创建file4的区域。我发现问题可能出在 $query 上,当它执行时,有时 mysql 会响应“未知表状态:TABLE_TYPE”,我不明白为什么。

最佳答案

正如上面评论中所写,您应该分而治之,以使您的生活更轻松(尤其是当您在大型函数中使用代码时编写代码)。这确实很简单:

function file_put($number, $data)
{
$path = sprintf("C:/temp/wamp/www/file%d.txt", $number);
file_put_contents($path, $data);
}

例如,这只是替换许多重复的行,您只需要一个在其中放入一些字符串的(编号)文件。

但是您也可以使用更复杂的东西来完成此操作,例如数据库操作。您可能希望将错误处理移出您的视线,并在需要时注意连接到数据库,并以更灵活的方式获取数据。这可以通过将(温和地弃用的)mysql_* 函数移动到其自己的一两个类中来完成,这样它就离开了您的视线。这将使它的使用更加容易(我首先展示):

// Create your database object to use it later on:
$config = array(
'server' => 'localhost',
'name' => 'root',
'password' => '',
'db' => 'test',
);
$db = new MySql($config);

我将数据库类命名为MySql,因为它代表 mysql 连接并且可以与旧的 mysql 扩展一起使用。您只需将该数据库对象传递到问题中的函数中即可。与 file_put 函数结合,它看起来像这样:

function checkin(MySql $DB, $TechID, $ClientID, $SiteID)
{
$query = sprintf("SELECT `Type` FROM `Log` WHERE `TechID` = '%d' ORDER BY LogTime DESC LIMIT 1", $TechID);
file_put(5, $query);

$result1 = $DB->query("SELECT COUNT(*) FROM Log");
$result2 = $DB->query($query);

foreach ($result1 as $row1) {
list($count) = $row1;
$data = "ClientID:$ClientID TechID:$TechID SiteID:$SiteID Count:$count"
file_put(3, $data);
foreach ($result2 as $row2) {
file_put(4, $data);
}
}
}

尽管如此,checkin 函数已经接近大型(已经有 12 行代码),但比您的第一个版本短得多,因为它委托(delegate)了写入文件和访问数据库的工作。我希望这个演示有用。以下是完整的代码示例:

/**
* MySql Exception
*/
class MySqlException extends RuntimeException
{
}

/**
* MySql Database Class
*/
class MySql
{
private $server;
private $name;
private $password;
private $db;
private $connection;

public function __construct(array $config)
{
$this->server = $config['server'];
$this->name = $config['name'];
$this->password = $config['password'];
$this->db = $config['db'];
}

private function connect($server, $name, $password)
{
$this->connection = mysql_connect($server, $name, $password);
if (!$this->connection) {
$this->error("Unable to connect to '%s' as user '%s'", $server, $name);
}
}

private function select($db)
{
if (!mysql_select_db($db, $this->connection)) {
$this->error("Unable to select database '%s'", $db);
}
}

private function close()
{
$this->connection && mysql_close($this->connection);
}

private function connectSelect()
{
$this->connect($this->server, $this->name, $this->password);
$this->select($this->db);
}

/**
* @param $query
* @return MySqlResult
*/
public function query($query)
{
$this->connection || $this->connectSelect();
$result = mysql_query($query, $this->connection);
if (!$result) {
$this->error("Unable to execute query '%s'", $query);
}
return new MySqlResult($result);
}

/**
* @param string $format
* @param ...
* @throws MySqlException
*/
private function error($format)
{
$args = func_get_args();
array_shift($args);
$format .= ': %s';
$args[] = $this->connection ? mysql_error($this->connection) : mysql_error();
throw new MySqlException(vsprintf($format, $args));
}

public function __destruct()
{
$this->close();
}
}

/**
* MySql Result Set - Array Based
*/
class MySqlResult implements Iterator, Countable
{
private $result;
private $index = 0;
private $current;

public function __construct($result)
{
$this->result = $result;
}

public function fetch($result_type = MYSQL_BOTH)
{
$this->current = mysql_fetch_array($this->result, $result_type);
return $this->current;
}

/**
* Return the current element
* @link http://php.net/manual/en/iterator.current.php
* @return array
*/
public function current()
{
return $this->current;
}

public function next()
{
$this->current && $this->fetch();
}

/**
* Return the key of the current element
* @link http://php.net/manual/en/iterator.key.php
* @return mixed scalar on success, or null on failure.
*/
public function key()
{
return $this->current ? $this->index : null;
}

/**
* Checks if current position is valid
* @link http://php.net/manual/en/iterator.valid.php
* @return boolean The return value will be casted to boolean and then evaluated.
* Returns true on success or false on failure.
*/
public function valid()
{
return (bool)$this->current;
}

/**
* Rewind the Iterator to the first element
* @link http://php.net/manual/en/iterator.rewind.php
* @return void Any returned value is ignored.
*/
public function rewind()
{
$this->fetch();
}

/**
* Count of rows.
*
* @link http://php.net/manual/en/countable.count.php
* @return int The count of rows as an integer.
*/
public function count()
{
return mysql_num_rows($this->result);
}
}

// Create your database object to use it later on:
$config = array(
'server' => 'localhost',
'name' => 'root',
'password' => '',
'db' => 'test',
);
$db = new MySql($config);

function file_put($number, $data)
{
$path = sprintf("C:/temp/wamp/www/file%d.txt", $number);
file_put_contents($path, $data);
}

function checkin(MySql $DB, $TechID, $ClientID, $SiteID)
{
$query = sprintf("SELECT `Type` FROM `Log` WHERE `TechID` = '%d' ORDER BY LogTime DESC LIMIT 1", $TechID);
file_put(5, $query);

$result1 = $DB->query("SELECT COUNT(*) FROM Log");
$result2 = $DB->query($query);

foreach ($result1 as $row1) {
list($count) = $row1;
$data = "ClientID:$ClientID TechID:$TechID SiteID:$SiteID Count:$count";
file_put(3, $data);
foreach ($result2 as $row2) {
file_put(4, $data);
}
}
}

checkin($db, 1, 2, 3, 4);

关于PHP文件无法输入部分代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11575531/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com