dongyou8368 2010-12-24 00:04
浏览 38
已采纳

PHP,预示着奇怪的行为

Sorry for the limited title. Go easy on me, i'm not quite advanced at PHP.

In my functions.php i've got the following 2 functions to see if the "recievers" or "senders" already exist.

function getSenderId($fromAddress){
    global $database;
    $check = $database->query("SELECT * FROM custMod_senders WHERE email = '$fromAddress'");
    if($check->fetchRow() > 0){
        $id = $check->fetchRow();
        return $id['id'];       
    }else{
        return 0;
    }
}

function getReceiverId($toAddress){
    global $database;
    $check = $database->query("SELECT * FROM custMod_receivers WHERE email = '$toAddress'");
    if($check->fetchRow() > 0){
        $id = $check->fetchRow();
        return $id['id'];       
    }else{
        return 0;
    }
}

Now in my the script i've got the following.

$from = $_SESSION['fromAddress'];
$to = $_SESSION['addresses'];
$std = $_SESSION['possibleStd'];

    if (!is_array($to)){
        $to = array($to);       
    }



foreach($to as &$address) { 

        if(getReceiverID($address) == 0){
            echo 'receiver not found';
            $database->query("INSERT INTO custMod_receivers (email, possibleStd) VALUES ('$address', '$std')");

        }

        if(getSenderID($from) == 0){
            echo 'sender not found';
            $database->query("INSERT INTO custMod_senders (email) VALUES ('$from')");

        }

        $send_id = getSenderID($from);
        $receive_id = getReceiverID($address);

        $database->query("INSERT INTO custMod_lt_send_rec (send_id, receive_id) VALUES ('$send_id', '$receive_id')");

    };  

Given that my vars, session and tablenames all exist and are available.. What is wrong with this loop or function setup?

The problem is in de last query within the foreach(). the link table is giving all kinds of crazy results.

EDIT: The problem is that the results in the DB are double, incomplete or missing. As if my 2 functions are giving false results...

  • 写回答

3条回答 默认 最新

  • dth54864 2010-12-24 00:21
    关注

    If you're using mysqli (which you should), your database object can automatically grab the IDs. It's faster (two fewer lookups) and instantaneous.

    $conn = new mysqli("localhost", "username", "password");
    $conn->query("INSERT BLAH BLAH BLAH");
    
    $id_of_new_row = $conn->insert_id;
    

    What you're likely seeing (as you mentioned in the comments) is that the database is still writing and you're seeing a cached response (null) or some other improper value due to concurrency nonsense. It makes no sense to send an extra two queries when you can just grab the IDs that you're looking up with the two functions you posted, though, so switch your DB object over to this method and let us know how it goes!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向