将MySQL服务器转移到php page =查询无效?

时间:2014-04-18 20:41:33

标签: php mysql

我试图创建一个显示MySQL搜索引擎的php页面,并在一台服务器上工作:但是,在该服务器上崩溃,我被迫重新启动它。即使我使用相同的代码,搜索引擎也不再有效 - 我的代码如下:

<html>
<head>
<meta charset="UTF-8">

<title>Search Engine Test</title>
<h1>Gromax</h1>

</head>
<body>
<form action="search1.php" method="post">
    <input type="text" name="keyword">
    <input type="submit" name="search" value="Search">
    <br>
    <br>
</form>


<script language="php">
// Create a database connection
error_reporting(0);

$connection = mysql_connect("localhost", "$ mysql -u anonymous", "");
if (!$connection) {
    die("Please reload page. Database connection failed: " . mysql_error());
}

// Select a databse to use
$db_select = mysql_select_db("test", $connection);
if (!$db_select) {
    die("Please reload page. Database selection failed: " . mysql_error());
}

// Search Engine
// Only execute when button is pressed
if (isset($_POST['keyword'])) {
    // Filter
    $keyword = trim($_POST['keyword']);

    // Select statement
    $search = "SELECT Price FROM table_1 WHERE Model = '$keyword'";

// Display
    $result = mysql_query($search) or die('query did not work');

while ($result_array = mysql_fetch_array($result)) {
        $arrlength=count($result_array);
    for($x=0;$x+1<$arrlength;$x++){

    echo "Price: " . $result_array[$x];
    echo "<br>";

} 
 }


} 
?>




</script>


</body>
</html>

任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分:0)

我很确定您的查询或数据库连接出错。尝试评论该行:

// error_reporting(0);

看看你得到了什么错误......

答案 1 :(得分:0)

尝试使用这样的格式来确定您的选择查询中出现的问题:

<?php
$sql = " 
    SELECT 
        Price 
    FROM 
        table_1
    WHERE 
        Model = '".$keyword."'
"; 

if(!$res = mysql_query($sql)) 
{ 
    trigger_error(mysql_error().'<br />In query: '.$sql); 
} 
elseif(mysql_num_rows($res) == 0) 
{ 
    echo 'Geen resultaten gevonden'; 
} 
else 
{ 
    while($row = mysql_fetch_assoc($res)) 
    { 
        echo $row['voornaam'].'<br />'; 
    } 
} 
?>