我不想用mysql_query 回答你的问题,原因有两个:
1. mysql_query 官方状态为Deprecated:mysql 扩展已弃用,未来将被移除:改用mysqli 或PDO
2.易受SQL注入,见How can I prevent SQL injection in PHP?
改用 PDO(PHP 数据对象),它是安全的并且是面向对象的
这里有一些教程可以在 12 个视频中掌握这一点 http://www.youtube.com/watch?v=XQjKkNiByCk
用这个替换你的 MySQL 实例
// instance of pdo
$config['db'] = array
(
'host' => '',
'username' => '',
'password' => '',
'dbname' => ''
);
$dbh = new PDO('mysql:host=' . $config['db']['host'] .
';dbname=' . $config['db']['dbname'],
$config['db']['username'],
$config['db']['password']);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
global $dbh;
//dbh 只是对象的自定义名称,您可以将其命名为数据库
编辑凭据,接下来让我们查询您的代码,如果实例不在同一个文件中,即包含连接脚本,则在启动 sql 之前调用global $dbh;,以便将对象带到当前文件,否则
所以你的代码看起来像这样
<?php
global $dbh;
//lets prepare the statement using : to input what ever variables we need to (securely)
$displayData= $dbh->prepare("SELECT vName,id FROM employee WHERE vName LIKE :my_data ORDER BY vName");
$displayData->bindValue(':my_data', $my_data , PDO::PARAM_STR);
//then we execute the code
$displayData->execute();
//store the result in array
$result = $displayData->fetchAll();
print_r($result); //take a look at the structured
//depending on the structure echoing could be like **echo $result[0][theIdYouTrynaGet];**
?>
那么我该如何在其他页面中检索它?
<html>
<?php
include_once '/*the path of your file where the above is happening*/';
<input type="hidden" value="<?php echo $result['pass in your parameters'] ?>"/>
?>
<html>