【问题标题】:OOPHP calling a function within a functionOOPHP 在函数内调用函数
【发布时间】:2012-01-04 20:22:33
【问题描述】:

好的,所以我使用 OOPHP 在 Flash Builder 和 MYSQL 之间进行通信,这两个函数都可以工作,但是当我在另一个函数中调用其中一个函数时,它会引发错误

错误:服务器错误 MySQL 错误 - 2014 我已经在互联网上搜索过,找不到任何合适的答案。

是不是因为第一个函数对MYSQL数据库有锁,所以第二个不能访问数据库?

我有这个问题的原因是在一个表中,我将客户国家作为首字母,我创建了第二个表,其中首字母代表什么,即 UK = United Kingdom,我当时这样做是因为我想我想如果客户端表有很多记录,这将是一个很好的数据库实践来节省空间。

所以现在我正在调用一个函数,该函数为我提供了一个特定的客户端,但在该调用中,我想调用第二个函数来将国家/地区首字母更改为正确的国家/地区字符串。

第一个函数

public function getClients($item) {

    $stmt = mysqli_prepare($this->connection, "SELECT timestamp, id, fname, lname, dateofbirth, monthofbirth, yearofbirth,
    town, country, sex, rate, comments from $this->tablename WHERE (id = $item)");  
    $this->throwExceptionOnError();

    mysqli_stmt_execute($stmt);
    $this->throwExceptionOnError();

    $rows = array();

    mysqli_stmt_bind_result($stmt, $row->timestamp, $row->ID, $row->fname, $row->lname, $row->dateofbirth, $row->monthofbirth, 
    $row->yearofbirth, $row->sex, $row->country, $row->town, $row->rate, $row->comments);

    while (mysqli_stmt_fetch($stmt)) {
      $row->fname = ucfirst(substr($row->fname,0,1));
      $row->lname = ucfirst($row->lname);
      $row->town = ucfirst($row->town);
      $row->comments = strip_tags($row->comments);
      $row->lname = (($row->fname) . " " . ($row->lname));
      $row->country = $this->getCountry($row->country);
      $row->yearofbirth = GetAge($row->dateofbirth. '-' .$row->monthofbirth. '-' .$row->yearofbirth);
      $row->Pic_loc = "";
      $row->Pic_loc= "SLAGSIMAGES/".($row->ID)."/image01.jpg";
      $row->timestamp = new DateTime($row->timestamp);
      $rows[] = $row;
      $row = new stdClass();
      mysqli_stmt_bind_result($stmt, $row->timestamp, $row->id,$row->fname,$row->lname,$row->sex,$row->country,$row->town,$row->dateofbirth,
      $row->monthofbirth,$row->yearofbirth, $row->rate, $row->comments);
    }


    mysqli_stmt_free_result($stmt);
    mysqli_close($this->connection);



    return $rows;
}

这是第一个调用的第二个函数

public function getCountry($countrycode) {

    $stmt = mysqli_prepare($this->connection, "SELECT country FROM countrycodes where CountryCodes ='$countrycode'");       
    $this->throwExceptionOnError();

    mysqli_stmt_execute($stmt);
    $this->throwExceptionOnError();

    $rows = array();

    mysqli_stmt_bind_result($stmt, $row->country);

    while (mysqli_stmt_fetch($stmt)) {
      $rows[] = $row;
      $row = new stdClass();
      mysqli_stmt_bind_result($stmt, $row->country);
    }

    mysqli_stmt_free_result($stmt);
    mysqli_close($this->connection);

    return $rows;
}   

这是第一个引发错误的函数中的一段代码 $row->country = $this->getCountry($row->country);

对不起,如果代码有点混乱,但提前感谢任何正确的答案或信息,告诉我的大脑为什么不能完成。

【问题讨论】:

  • "coz" 不是一个词,请不要在您的 SO 帖子中使用聊天对话。
  • 是的,这是因为您使用第一个 SQL 打开/锁定了一个连接。也许你可以JOIN SQL 进入一次查找,或者执行一次获取/关闭然后执行另一次获取。
  • 谢谢 Rudu,我也是这么想的

标签: php mysql sql flash-builder


【解决方案1】:

“所以现在我正在调用一个函数,该函数为我提供了一个特定的客户端,但在该调用中,我想调用第二个函数来将国家/地区首字母更改为正确的国家/地区字符串”

为什么不在第一个函数中加入另一个 SQL 表,获取您需要的数据而不必调用第二个函数?

【讨论】:

  • 我想我错了,因为我试图整合我所拥有的东西,而不是坐下来用我的大脑来创建一个连接,最终到达那里。谢谢
【解决方案2】:
http://www.php.net/manual/en/mysqli-stmt.execute.php

注意:

使用mysqli_stmt_execute()时,mysqli_stmt_fetch()函数 必须用于在执行任何附加操作之前获取数据 查询。

就像所有 mysql 的东西一样,你一次可以做一个查询 解决此问题的另一种方法是使用连接池 并做类似的事情

$link = (find_first_available_connection_from_pool) (should return locked link)
query($link..)
release($link) [unlock]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-12
    • 2017-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-10
    • 1970-01-01
    相关资源
    最近更新 更多