在PHP中调用MySQL存储过程只会更新一行

时间:2011-04-05 23:36:30

标签: php mysql stored-procedures mysqli

我在MySQL中有一个存储过程,应该更新表中的列。我跑的时候

CALL recalculate_city_ids();

从MySQL提示符中,更新正确的行数(几百个)。当我从PHP运行命令时,只更新了一行,我没有收到任何错误。

这是PHP:

$con = mysqli_connect('localhost', 'user', 'pass', 'dbname' );
$result = $con->query( 'call recalculate_city_ids()' );
mysql_close($con);

和sproc的SQL:

    DROP PROCEDURE IF EXISTS recalculate_city_ids;
    DELIMITER $$
    CREATE PROCEDURE recalculate_city_ids()
    READS SQL DATA
    BEGIN
        DECLARE o_id INT DEFAULT 0;
        DECLARE o_latitude FLOAT;
        DECLARE o_longitude FLOAT;
        DECLARE done INT DEFAULT 0;
        DECLARE cur_users CURSOR FOR SELECT id, latitude, longitude FROM user WHERE latitude IS NOT NULL ORDER BY fname;
        DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;

        OPEN cur_users;
        users: LOOP
            FETCH cur_users INTO o_id, o_latitude, o_longitude;

            IF done=1 THEN
                LEAVE users;
            END IF;

            SELECT @closest_city_distance:=fn_distance_cosine(o_latitude, o_longitude, latitude, longitude) AS distance, @closest_city_id:=id AS id FROM category WHERE zone="city" AND active=1 ORDER BY distance LIMIT 1;

            UPDATE user SET city_id = IF(@closest_city_distance<=30, @closest_city_id, 0) WHERE id=o_id;
        END LOOP users;
        CLOSE cur_users;
    END
    $$

我可以使用mysqli从PHP运行其他查询(也尝试过mysql对象)。我也无法从PHP创建存储过程(没有错误),也不能从MySQL提示符中创建该部分。

PHP和我的MySQL提示符使用相同的用户名。

1 个答案:

答案 0 :(得分:0)

我认为当查询返回多个结果集时,PHP不喜欢它。我真的不需要返回任何东西,因为这只是一个荣耀的UPDATE陈述,所以我改变了我的

SELECT @closest_city_distance:=fn_distance_cosine...查询

SELECT fn_distance_cosine(o_latitude, o_longitude, latitude, longitude) as distance,id into closest_city_distance, closest_city_id FROM category WHERE zone="city" AND active=1 ORDER BY distance LIMIT 1;

由于这是返回结果集的唯一位置,因此删除返回的集会解决问题。