我收到错误PHP致命错误:每当我调用类似的东西时,都在非对象上调用成员函数execute(),该对象引用….-> execute()行.
$select_str = 'select id, stamp, lat, lng, spd from gps';
$select = $db->prepare($select_str);
$select->execute();
要么
$insert = $db->prepare('insert into gps (id, lat, lng, spd) values (?, ?, ?, ?)');
$insert->execute(array($id, $lat, $lng, $spd));
通过在网上搜索,我怀疑$select(或$insert)在某种程度上成为了保存数据的“死对象”,但是无法对其调用方法.
但是我不知道如何防止它,并且我的PHP经验很短(我来自Perl).请帮助我解决此问题,这可能是次要的事情.
下面是我完整的脚本,我想(我希望)它可读性很强-它允许您创建表,插入记录,删除记录,查看它们或再次删除该表-全部取决于$_REQUEST [‘mode’ ]参数:
@define('DBHOST', 'localhost');
@define('DBNAME', 'snake');
@define('DBUSER', 'snake');
@define('DBPASS', 'snake');
# lowercase mode and id parameters; replace commas by dots in lat, lng, spd
$mode = isset($_REQUEST['mode']) ? strtolower(trim($_REQUEST['mode'])) : '';
$id = isset($_REQUEST['id']) ? strtolower(trim($_REQUEST['id'])) : '';
$lat = isset($_REQUEST['lat']) ? strtr(trim($_REQUEST['lat']), ',', '.') : '';
$lng = isset($_REQUEST['lng']) ? strtr(trim($_REQUEST['lng']), ',', '.') : '';
$spd = isset($_REQUEST['spd']) ? strtr(trim($_REQUEST['spd']), ',', '.') : '';
# id must be 32 chars long hex number; lat, lng, spd must be decimal numbers
$id_ok = preg_match('/^[a-f0-9]{32}$/', $id);
$lat_ok = preg_match('/^[+-]?[0-9.]+$/', $lat);
$lng_ok = preg_match('/^[+-]?[0-9.]+$/', $lng);
$spd_ok = preg_match('/^\+?[0-9.]+$/', $spd);
# has the user selected a mode and provided valid input?
$create_ok = ($mode == 'create');
$insert_ok = ($mode == 'insert' && $id_ok && $lat_ok && $lng_ok && $spd_ok);
$delete_ok = ($mode == 'delete' && $id_ok);
$select_ok = ($mode == 'select');
$drop_ok = ($mode == 'drop');
# first call or invalid input: display web form and exit
if (!($create_ok || $insert_ok || $delete_ok || $select_ok || $drop_ok)) {
header('Content-Type: text/html; charset=utf-8');
print '
Mode:
create table
select records (can specify id)
insert 1 record (must specify all)
delete records (must specify id)
drop table
Id: (32 hex chars)
Latitude: (between -90 and 90)
Longitude: (between -90 and 90)
Speed: (not negative)
';
exit();
}
try {
# enable persistent connections and throw exception on any errors
$options = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_PERSISTENT => true);
$db = new PDO('mysql:host=' . DBHOST . '; dbname=' . DBNAME, DBUSER, DBPASS, $options);
if ($create_ok) {
$db->exec('create table gps (
id char(32) not null check length(id)=32,
lat decimal(5,3) not null,
lgt decimal(5,3) not null,
spd decimal(5,3) unsigned not null,
stamp timestamp default now(),
index(id) )');
} else if ($insert_ok) {
$insert = $db->prepare('insert into gps (id, lat, lng, spd) values (?, ?, ?, ?)');
$insert->execute(array($id, $lat, $lng, $spd));
} else if ($delete_ok) {
} else if ($drop_ok) {
$db->exec('drop table gps');
header('Content-Type: text/plain');
print('Database dropped');
exit();
}
# display current table content in XML format
$select_str = 'select id, stamp, lat, lng, spd from gps';
# but filter by id if requested by user
if ($select_ok && $id_ok) {
$select = $db->prepare($select_str . ' where id = ?');
$select->execute(array($id));
} else {
$select = $db->prepare($select_str);
$select->execute();
}
header('Content-Type: text/xml; charset=utf-8');
print('<?xml version="1.0"?>');
while ($row = $select->fetch(PDO::FETCH_ASSOC)) {
printf('',
$row['id'], $row['stamp'], $row['lat'], $row['lng'], $row['spd']);
}
print('');
} catch (Exception $e) {
header('Content-Type: text/plain');
print('Database problem: ' . $e->getMessage());
}
?>
MySQL用户“ snake”具有以下权限:
select * from mysql.user where User='snake';
+-----------+-------+------------------+-------------+-------------+-------------+-------------+-------------+-----------+-------------+---------------+--------------+-----------+------------+-----------------+------------+------------+--------------+------------+-----------------------+------------------+--------------+-----------------+------------------+------------------+----------------+---------------------+--------------------+------------------+----------+------------+-------------+--------------+---------------+-------------+-----------------+----------------------+
| Host | User | Password | Select_priv | Insert_priv | Update_priv | Delete_priv | Create_priv | Drop_priv | Reload_priv | Shutdown_priv | Process_priv | File_priv | Grant_priv | References_priv | Index_priv | Alter_priv | Show_db_priv | Super_priv | Create_tmp_table_priv | Lock_tables_priv | Execute_priv | Repl_slave_priv | Repl_client_priv | Create_view_priv | Show_view_priv | Create_routine_priv | Alter_routine_priv | Create_user_priv | ssl_type | ssl_cipher | x509_issuer | x509_subject | max_questions | max_updates | max_connections | max_user_connections |
+-----------+-------+------------------+-------------+-------------+-------------+-------------+-------------+-----------+-------------+---------------+--------------+-----------+------------+-----------------+------------+------------+--------------+------------+-----------------------+------------------+--------------+-----------------+------------------+------------------+----------------+---------------------+--------------------+------------------+----------+------------+-------------+--------------+---------------+-------------+-----------------+----------------------+
| localhost | snake | 684bce5059b3e0a8 | Y | Y | N | Y | Y | Y | N | N | N | N | N | N | Y | N | N | N | N | N | N | N | N | N | N | N | N | N | | | | | 0 | 0 | 0 | 0 |
+-----------+-------+------------------+-------------+-------------+-------------+-------------+-------------+-----------+-------------+---------------+--------------+-----------+------------+-----------------+------------+------------+--------------+------------+-----------------------+------------------+--------------+-----------------+------------------+------------------+----------------+---------------------+--------------------+------------------+----------+------------+-------------+--------------+---------------+-------------+-----------------+----------------------+
谢谢!
亚历克斯