我收到错误 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'] 参数:
<?php
@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 '<html>
<body>
<form method="post">
<p>Mode:<br />
<input type="radio" name="mode" value="create"><i>create table</i><br />
<input type="radio" name="mode" value="select" checked>select records (can specify id)<br />
<input type="radio" name="mode" value="insert">insert 1 record (must specify all)<br />
<input type="radio" name="mode" value="delete">delete records (must specify id)<br />
<input type="radio" name="mode" value="drop"><i>drop table</i><br />
</p>
<p>Id: <input type="text" name="id" size=32 maxlength=32 /> (32 hex chars)</p>
<p>Latitude: <input type="text" name="lat" size=10 /> (between -90 and 90)</p>
<p>Longitude: <input type="text" name="lng" size=10 /> (between -90 and 90)</p>
<p>Speed: <input type="text" name="spd" size=10 /> (not negative)</p>
<p><input type="submit" value="OK" /></p>
</form>
</body>
</html>
';
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"?><gps>');
while ($row = $select->fetch(PDO::FETCH_ASSOC)) {
printf('<pos id="%s" stamp="%u" lat="%f" lng="%f" spd="%f" />',
$row['id'], $row['stamp'], $row['lat'], $row['lng'], $row['spd']);
}
print('</gps>');
} 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 |
+-----------+-------+------------------+-------------+-------------+-------------+-------------+-------------+-----------+-------------+---------------+--------------+-----------+------------+-----------------+------------+------------+--------------+------------+-----------------------+------------------+--------------+-----------------+------------------+------------------+----------------+---------------------+--------------------+------------------+----------+------------+-------------+--------------+---------------+-------------+-----------------+----------------------+
谢谢! 亚历
答案 0 :(得分:4)
关键是PDO::prepare()
的行为:
如果数据库服务器成功准备语句,PDO :: prepare()将返回PDOStatement对象。如果数据库服务器无法成功准备语句,PDO :: prepare()将返回 FALSE 或发出PDOException(取决于错误处理)。
prepare()
以某种方式失败了您的查询。
默认情况下,PDO是静音的,不会抛出任何错误消息。请参阅this page,了解如何使PDO显示错误,以及有关错误的详细信息。