我很难在MySQL中存储令牌

时间:2015-12-06 00:26:43

标签: php mysql oop token

请帮我弄清楚如何在MySQL字段中存储下面脚本中的令牌。我通过进行试验和错误测试来发现我能够存储密钥,编码密码,但由于某种原因我无法存储令牌,从而解决了这个问题。我应该用什么字段来存储它?什么长度?

private void drawAt(Point p)
{
    Graphics g = board.getGraphics();
    g.setColor(brushColor);
    g.setFont(brushFont);
    g.drawString(brushText, p.x, p.y);
    g.dispose();
}

1 个答案:

答案 0 :(得分:1)

使用表格中的二进制字段,例如varbinary,您可以使用prepared statement并将令牌参数绑定为blob,然后通过mysqli_stmt::send_long_data

发送实际数据
<?php
$mysqli = new mysqli('localhost', 'localonly', 'localonly', 'test');
if ($mysqli->connect_errno) {
    trigger_error( sprintf('mysqli connect error (%d) %s', $mysqli->connect_errno, $mysqli->connect_error), E_USER_ERROR);
    die;
}
mysqli_report(MYSQLI_REPORT_STRICT|MYSQLI_REPORT_ALL);
$mysqli->query('
    CREATE TEMPORARY TABLE soFoo (
        id int auto_increment,
        x varbinary(128),
        primary key(id)
    )
');

$token = openssl_random_pseudo_bytes(96); // just some binary data

$stmt = $mysqli->prepare('INSERT INTO soFoo (x) VALUES (?)');
$stmt->bind_param('b', $foo);
$stmt->send_long_data(0, $token);
$stmt->execute();
$stmt = null;

foreach( $mysqli->query('SELECT x FROM soFoo WHERE id=1') as $row ) {
    var_export($token===$row['x']);
}

可能也只能绑定一个字符串参数。

$stmt = $mysqli->prepare('INSERT INTO soFoo (x) VALUES (?)');
$stmt->bind_param('s', $token);
$stmt->execute();

编辑:使用具有多个blob参数的预准备语句:

$stmt = $mysqli->prepare('INSERT INTO soFoo (x, y, anInteger, largeblob) VALUES (?,?,?,?)');
$stmt->bind_param('bbib', $foo, $foo, $n, $foo);

// the first parameter of send_long_data specificies the positional parameter of the prep.statement, starting with 0
$stmt->send_long_data(0, $token); // the data "for" x
$stmt->send_long_data(1, $token); // the data "for" y

// assume a case where the data for largeblob might potentially be larger than max_client_packet
// there's an integer parameter that already "has its data" between the last blob and this one, so this is parameter #3
while( !feof($fp) ) { // the loop is a bit oversimplified; you would probably have a more error handling in production code....
    $stmt->send_long_data(3, fread($fp, 4096)); // the next data chunk "for" largeblob
}

$stmt->execute();
$stmt = null;

正如我在上一篇评论中所提到的,使用send_long_data()绝不是形状或形式必须。如果你愿意,我建议你从

开始

http://dev.mysql.com/doc/refman/5.7/en/charset-connection.html
http://dev.mysql.com/doc/refman/5.7/en/binary-varbinary.html
http://dev.mysql.com/doc/refman/5.7/en/string-literals.html
https://dev.mysql.com/doc/refman/5.7/en/packet-too-large.html

作为决定这是否适合您的理由; - )