0

如何将我的 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>

我已经在数据库中插入了一些数据。

请帮忙。提前致谢。

4

0 回答 0