数据库连接问题


database connection issues

我正试图将我的网站链接到我的数据库以获取结果,但它显示了以下错误:

Warning: mysql_select_db() expects parameter 1 to be string, resource given in C:'wamp'www'SearchEngine'connect.php on line 9

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:'wamp'www'SearchEngine'search.php on line 44

以下是这两个文件的代码:

connect.php:

<?php
$con = mysql_connect("localhost", "root", "");
if (!$con)
{
    echo "Cannot connect to database";
    die();
}
mysql_select_db($con,"SearchEngine");
?>

search.php:

<?php
//php code goes here
include 'connect.php'; // for database connection
$query = $_GET['q'] // query
?>
<html>
    <head>
        <title>
            Brandon's Search Engine
        </title>
        <style type="text/css">
            #search-result {
                font-size: 22;
                margin: 5px;
                padding: 2px;
            }
            #search-result:hover {
                border-color: red;
            }
        </style>
    </head>
    <body>
        <form method="GET" action="search.php">
            <table>
                <tr>
                    <td>
                        <h2>
                            Brandon's Search Engine
                        </h2>
                    </td>
                </tr>
                <tr>
                    <td>
                        <input type="text" value="<?php echo $_GET['q']; ?>" name="q" size="80" name="q"/>
                        <input type="submit" value="Search" />
                    </td>
                </tr>
                <tr>
                    <td>
                        <?php
                        //SQL query
                        $stmt = "SELECT * FROM web WHERE title LIKE '%$query%' OR link LIKE '%$query%'";
                        $result = mysql_query($stmt);
                        $number_of_result = mysql_num_rows($result);
                        if($number_of_result < 1)
                            echo "Your search did not match any documents. Please try different keywords.";
                        else
                        {
                                //results found here and display them
                                while($row = mysql_fetch_assoc($result))
                                {
                                    $title = $row["title"];
                                    $link = $row["link"];
                                    echo "<div id='search-result'>";
                                    echo "<div id='title'" . $title . "</div>";
                                    echo "<br />";
                                    echo "<div id='link'" . $link . "</div>";
                                    echo "</div>";
                                }
                        }
                        ?>
                    </td>
                </tr>
            </table>
        </form>
    </body>
</html>

如果可能的话,请向我解释。

mysql数据库选择语法,

bool mysql_select_db ( string $database_name [, resource $link_identifier = NULL ] )

尝试使用,

mysql_select_db("SearchEngine",$con);

而不是

mysql_select_db($con,"SearchEngine");

参考编号:http://us2.php.net/mysql_select_db

注意:尝试使用mysqli_*函数或PDO,而不是mysql_*函数(已弃用)

您的参数错误,应该是

mysql_select_db("SearchEngine",$con);

您的脚本也是不安全的,您应该执行以下操作:

  • 如果可用,请使用mysqli_*方法而不是mysql_*,例如:

$con = mysqli_connect("localhost", "root", "");mysqli_select_db($con,"SearchEngine");

等等。

  • 在任何查询中使用查询参数之前,请先转义该参数,以防止SQL注入,例如:

更换

$stmt = "SELECT * FROM web WHERE title LIKE '%$query%' OR link LIKE '%$query%'";

带有

$stmt = "SELECT * FROM web WHERE title LIKE '%" . mysqli_real_escape_string($con, $query) . "%' OR link LIKE '%" . mysqli_real_escape_string($con, $query) . "%'";

  • 用HTML转义用户生成的内容,以防止跨站点脚本(XSS),例如:

更换

<input type="text" value="<?php echo $_GET['q']; ?>" name="q" size="80" name="q"/>

带有

<input type="text" value="<?php echo htmlspecialchars($_GET['q']); ?>" name="q" size="80" name="q"/>

这个错误告诉第一个参数应该是字符串。如果你检查文档,你会看到数据库名称首先出现,然后是连接资源。

这样做:

mysql_select_db("SearchEngine",$con);

另外:根本不要使用mysql*函数!切换到mysqli或更好的PDO进行数据库交互。

我可以看到你的错误,它应该是这样的

mysql_select_db("SearchEngine",$con);

第一个参数必须是数据库名称

mysql_select_db("SearchEngine",$con);

¨