写了一个表单,不知道怎么和数据库连接,是PHP的 MYSQL
<tablewidth="500"border="1"cellspacing="0"><tr><tdcolspan="3"align="center">联系我们</td>...
<table width="500" border="1" cellspacing="0"> <tr> <td colspan="3" align="center">联系我们</td> </tr> <tr> <td width="215">name:</td> <td colspan="2"><input type="text" name="name" width="200px" /></td> </tr> <tr> <td>Enter the password:</td> <td colspan="2"><input type="password" name="password" width="200px" /></td> </tr> <tr> <td>Confirm password:</td> <td colspan="2"><input type="password" name="password" width="200px" /></td> </tr> <tr> <td>E-mail</td> <td colspan="2"><input type="text" name="email" width="200px" /></td> </tr> <tr> <td colspan="2"><input type="submit" /></td> <td width="213"><input type="reset" /></td> </tr></table></form>
求指导 应该怎么写才能正确连接和写入查询呢? 展开
求指导 应该怎么写才能正确连接和写入查询呢? 展开
1个回答
展开全部
您好,这样:
class cls_mysql
{
protected $link_id;
public function __construct($dbhost, $dbuser, $dbpw, $dbname = '', $charset = 'utf8')//构造函数
{
if(!($this->link_id = mysql_connect($dbhost, $dbuser, $dbpw)))
{
$this->ErrorMsg("Can't pConnect MySQL Server($dbhost)!");
}
mysql_query("SET NAMES " . $charset, $this->link_id);//设置编码
if ($dbname)
{
if (mysql_select_db($dbname, $this->link_id) === false )
{
$this->ErrorMsg("Can't select MySQL database($dbname)!");
return false;
}
else
{
return true;
}
}
}
public function select_database($dbname)//选择数据库
{
return mysql_select_db($dbname, $this->link_id);
}
public function fetch_array($query, $result_type = MYSQL_ASSOC)//得到遍历后的数据,是一个数组形式
{
return mysql_fetch_array($query, $result_type);
}
public function query($sql)//执行查询
{
return mysql_query($sql, $this->link_id);
}
public function affected_rows()//得到影响的记录集数
{
return mysql_affected_rows($this->link_id);
}
public function num_rows($query)//获得查询的记录数
{
return mysql_num_rows($query);
}
public function insert_id()
{
return mysql_insert_id($this->link_id);//获得插入的id
}
public function selectLimit($sql, $num, $start = 0)
{
if ($start == 0)
{
$sql .= ' LIMIT ' . $num;
}
else
{
$sql .= ' LIMIT ' . $start . ', ' . $num;
}
return $this->query($sql);
}
public function getOne($sql, $limited = false)//获取一条记录
{
if ($limited == true)
{
$sql = trim($sql . ' LIMIT 1');
}
$res = $this->query($sql);
if ($res !== false)
{
$row = mysql_fetch_row($res);
return $row[0];
}
else
{
return false;
}
}
public function getrow($sql)
{
$res = $this->query($sql);
if ($res !== false)
{
return mysql_fetch_assoc($res);
}
else
{
return false;
}
}
public function getAll($sql)
{
$res = $this->query($sql);
if ($res !== false)
{
$arr = array();
while ($row = mysql_fetch_assoc($res))
{
$arr[] = $row;
}
return $arr;
}
else
{
return false;
}
}
function ErrorMsg($message = '', $sql = '')
{
if ($message)
{
echo "<b>error info</b>: $message\n\n";
}
else
{
echo "<b>MySQL server error report:";
print_r($this->error_message);
}
exit;
}
}
class cls_mysql
{
protected $link_id;
public function __construct($dbhost, $dbuser, $dbpw, $dbname = '', $charset = 'utf8')//构造函数
{
if(!($this->link_id = mysql_connect($dbhost, $dbuser, $dbpw)))
{
$this->ErrorMsg("Can't pConnect MySQL Server($dbhost)!");
}
mysql_query("SET NAMES " . $charset, $this->link_id);//设置编码
if ($dbname)
{
if (mysql_select_db($dbname, $this->link_id) === false )
{
$this->ErrorMsg("Can't select MySQL database($dbname)!");
return false;
}
else
{
return true;
}
}
}
public function select_database($dbname)//选择数据库
{
return mysql_select_db($dbname, $this->link_id);
}
public function fetch_array($query, $result_type = MYSQL_ASSOC)//得到遍历后的数据,是一个数组形式
{
return mysql_fetch_array($query, $result_type);
}
public function query($sql)//执行查询
{
return mysql_query($sql, $this->link_id);
}
public function affected_rows()//得到影响的记录集数
{
return mysql_affected_rows($this->link_id);
}
public function num_rows($query)//获得查询的记录数
{
return mysql_num_rows($query);
}
public function insert_id()
{
return mysql_insert_id($this->link_id);//获得插入的id
}
public function selectLimit($sql, $num, $start = 0)
{
if ($start == 0)
{
$sql .= ' LIMIT ' . $num;
}
else
{
$sql .= ' LIMIT ' . $start . ', ' . $num;
}
return $this->query($sql);
}
public function getOne($sql, $limited = false)//获取一条记录
{
if ($limited == true)
{
$sql = trim($sql . ' LIMIT 1');
}
$res = $this->query($sql);
if ($res !== false)
{
$row = mysql_fetch_row($res);
return $row[0];
}
else
{
return false;
}
}
public function getrow($sql)
{
$res = $this->query($sql);
if ($res !== false)
{
return mysql_fetch_assoc($res);
}
else
{
return false;
}
}
public function getAll($sql)
{
$res = $this->query($sql);
if ($res !== false)
{
$arr = array();
while ($row = mysql_fetch_assoc($res))
{
$arr[] = $row;
}
return $arr;
}
else
{
return false;
}
}
function ErrorMsg($message = '', $sql = '')
{
if ($message)
{
echo "<b>error info</b>: $message\n\n";
}
else
{
echo "<b>MySQL server error report:";
print_r($this->error_message);
}
exit;
}
}
追问
你能我用我的这个表写一个吗?或者是我的表也有问题,应该怎么改
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询