經過許多小時的搜索後,我只能找到「過時」和/或「不完整」的答案。 (顯然他們早於PDO。)我正在編寫PHP代碼(版本7.0.15),並使用MySQL版本5.7.17-0(都在KUbuntu「虛擬機」上)。儘管我已經使用計算機超過45年了,但我對PHP和MySQL相當陌生。從PHP中存儲IP到MySQL
我可以在PHP中獲取訪問者的IP地址。然後我想檢查「try_ur_table」來查看它是否已經有一個條目,如果沒有,然後插入條目並查找它,所以我可以在程序的其他部分使用「ur_index」。 ur_index是一個int(11),ur_ip是二進制(16)。
問題是,每次運行代碼時,第一個選擇都會失敗,因此會創建一個新條目,然後第二個選擇也無法找到匹配!
下面是相關的代碼片段:
try
{
$pdc = new PDO('mysql:host=localhost;dbname=toy_database', 'xxxxx', 'xxxxx');
$pdc->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdc->exec('SET NAMES "utf8"');
}
catch (PDOException $e)
{
$output = 'Unable to connect tothe databaseserver. ' . $e->getMessage();
include 'errout.html.php';
exit();
}
// Find the caller data...
if (isset($_SERVER[ 'REMOTE_ADDR' ]))
{
$cd = inet_pton($_SERVER[ 'REMOTE_ADDR' ]);
if ($cd)
{
// inet_pton thinks it succeeded...
try
{
$sql = "SELECT * FROM try_ur_table WHERE ur_ip = ? ";
$rt = $pdc->prepare($sql);
$rt->execute(array($cd));
$u_list = $rt->fetchAll();
}
catch (PDOException $e)
{
$output = 'Problem looking for ur_ip. ' . $e->getMessage();
include 'errout.html.php';
exit();
}
if ($u_list == NULL)
{
// New user!
try
{
$sqm = "INSERT INTO try_ur_table SET ur_ip=?";
$rs = $pdc->prepare($sqm);
$rs->execute(array($cd));
}
catch (PDOException $e)
{
$output = 'Problem inserting new ur_ip. ' . $e->getMessage();
include 'errout.html.php';
exit();
}
// Now go find the new entry...
try
{
$sql = "SELECT * FROM try_ur_table WHERE ur_ip = ? ";
$rt = $pdc->prepare($sql);
$rt->execute(array($cd));
$u_list = $rt->fetchAll();
}
catch (PDOException $e)
{
$output = 'Problem looking for new ur_ip. ' . $e->getMessage();
include 'errout.html.php';
exit();
}
} // $u_list == NULL
// At this point there should be exactly one row in $u_list...
} // $cd != false
}
else
{
// ! isset($_SERVER[ 'REMOTE_ADDR' ]
$cd = false;
}
其他測試表明,$ _ SERVER [「REMOTE_ADDR」]將返回127.0.0.1(這是有道理的,因爲這是「本地主機」)。然而,每次我運行上面的代碼,phpMyAdmin說ur_ip是0x7f00000001000000000000000000000(希望我已經正確計算了零 - 似乎無法從phpMyAdmin複製&粘貼,但這是次要的)。另外,因爲我有ur_index,所以我嘗試了基於它的選擇,當我試圖通過inet_ntop()運行ur_ip時,我得到垃圾,既沒有有效的IPv4也沒有IPv6地址。
[參見如何創建一個最小的,完整的,並且可驗證的示例](http://stackoverflow.com/help/mcve) –
當你插入一個新條目,你不必回去選擇相同的數據。 PDO :: lastInsertId是你的朋友。 – malcanso
45年?你是說當他們仍然是蒸汽動力? ;-) – Strawberry