douxin8610 2015-04-05 18:45
浏览 14
已采纳

为什么这个准备插入到MySql中,PDO插入错误的值?

I am trying to seed a database table with map points reflecting each point in a jpg map. The code is a test with tiny 3x7 image, but the application is intended for much larger maps, in which map point information will associated with each point and stored in the table. The map_terrain field represents the color hex value for a given coordinate. But it is the associated x,y values that are going wrong.

I am ending up with each row in the table with map_point_x = 7 and map_point_y = 3. The map_terrain field has the correct values.

How is this going astray?

$image_filespec = '../test.jpg';
$map_id = 1;

include_once '../includes/functions.php';

function heximagecolorat($image, $x, $y, $sharp_prefix = TRUE) {

        $rgb = imagecolorat($image, $x, $y);

        $r = ($rgb >> 16) & 0xFF;
        $g = ($rgb >> 8) & 0xFF;
        $b = $rgb & 0xFF;

   If ($sharp_prefix == TRUE) {
       $hex = "#";
   } else {
       $hex = "";
   }
   $hex .= str_pad(dechex($r), 2, "0", STR_PAD_LEFT);
   $hex .= str_pad(dechex($g), 2, "0", STR_PAD_LEFT);
   $hex .= str_pad(dechex($b), 2, "0", STR_PAD_LEFT);

   return $hex; // returns the hex value including the number sign (#)
}


if ($image_filespec == 'nnn.jpg' ){
    echo 'you need to edit teh php file to make this work and alter $image_filespec as appropriate';
    return;
}


//$result = query_db('SELECT * FROM users');
//  
//while($row = $result->fetch()){
//    echo $row['uID'] . " - " . $row['uUsername'] . "<br/>";
//}

$image = imagecreatefromjpeg($image_filespec); // imagecreatefromjpeg/png/

$width = imagesx($image);
$height = imagesy($image);
$colors = array();


for ($y = 0; $y < $height; $y++) {

    for ($x = 0; $x < $width; $x++) {

        $colors[] = heximagecolorat($image, $x, $y, $sharp_prefix=FALSE) ; 
    } 
}

// connect and insert arrya to database $row in this context is talking about table row as opposed to row from the original image
$dbh = db_connect();

$query = "INSERT INTO map_points (  map_id, map_point_x, map_point_y, map_terrain ) VALUES "; //Prequery
$qPart = array_fill(0, count($colors), "( ?, ?, ?, ?)");
$query .=  implode(",",$qPart);
$stmt = $dbh -> prepare($query); 
$i = 1;

$point=0;
for ($y = 0; $y < $height; $y++) {
    for ($x = 0; $x < $width; $x++) { //bind the values one by one
        $stmt -> bindParam($i++, $map_id);
        $stmt -> bindParam($i++, $x);
        $stmt -> bindParam($i++, $y);
        $stmt -> bindParam($i++, $colors[$point]);
        $point += 1;
    }
}
$temp=$stmt;
$stmt -> execute(); //execute
  • 写回答

1条回答 默认 最新

  • doumowu7371 2015-04-05 18:55
    关注

    I believe the problem is the use of PDOStatement::bindParam(): this binds the value of the variables (like $x, $y, ...) at the moment of query execution. This will be the final values after the loops, i.e. 3 and 7 in your example.

    Use PDOStatement::bindValue() instead.

    Additionally, it may be more easy to read and maintain your code if you execute the statement for each point, instead of using as many tuples as there are rows. In other words, use multiple INSERT INTO map_points(map_id, map_point_x, map_point_y, map_terrain) VALUES (?, ?, ?, ?) instead of one INSERT INTO map_points(map_id, map_point_x, map_point_y, map_terrain) VALUES (?, ?, ?, ?), (?, ?, ?, ?), .... Multiple tuples have better performance, but a similar performance can be reached by wrapping the INSERT statements in a transaction.

    $dbh = db_connect();
    
    $stmt = $dbh->prepare('INSERT INTO map_points (map_id, map_point_x, map_point_y, map_terrain) VALUES (?, ?, ?, ?)');
    $y = 0;
    $x = 0;
    $stmt->bindValue(1, $map_id);
    $stmt->bindParam(2, $x);
    $stmt->bindParam(3, $y);
    $dbh->beginTransaction();
    for (; $y < $height; $y++) {
        for (; $x < $width; $x++) {
            $stmt->bindValue(4, heximagecolorat($image, $x, $y, $sharp_prefix=FALSE));
            $stmt->execute();
        }
    }
    $dbh->commit();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 切换TabTip键盘的输入法
  • ¥15 关于#网络安全#的问题:求ensp的网络安全,不要步骤要完成版文件
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM