如何将我的PHP代码连接到MySQL数据库?

时间:2013-10-16 14:20:54

标签: sql database database-connection

如何将我的php代码连接到MySQL数据库?

我已多次尝试,无法访问数据库。

这些是我的php文件中的代码:

<html>

<head>
<title>Brandon's Search Engine - Results</title>
</head>

<body>
<h2>Brandon's Search Engine</h2>
<form action='./search.php' method='get'>
<input type='text' name='k' size='80' value='<?php echo $_GET['k']; ?>' />
<input type='submit' value='Search' >
</form>

<hr />
<?php
$k = $_GET['k'];

$terms = explode(" ", $k);
$query = "SELECT * FROM search WHERE ";

foreach ($terms as $each){
$i++;
if ($i == 1)
$query .= "keywords LIKE '%$each%' ";
else
$query .= "OR keywords LIKE '%$each%' ";
}

// connect
mysql_connect("sql102.freezoy.com", "frzoy_13854016", "xxxx");
mysql_select_db("frzoy_13854016_search");

$query = mysql_query($query);
$numrows = $query->num_rows;
if ($numrows > 0){

while ($row = mysql_fetch_assoc($query)){
$id = $row('id');
$title = $row('title');
$description = $row('description');
$keywords = $row('keywords');
$link = $row('link');

echo "<h2><a href='$link'>$title</a></h2>
$description<br /><br />";

}

}
else
echo"No results found for \"<b>$k</b>\"";
// disconnect
mysql_close();
?>

</body>

</html>

我已将一些数据插入数据库。

请帮忙。提前谢谢。

0 个答案:

没有答案