为什么服务器拒绝我的PDO查询?

时间:2014-08-13 09:06:45

标签: php drop-down-menu pdo

我一直在尝试使用下拉菜单编写一个简单的搜索和显示网站,我尝试过使用mysqli和PDO,但我没有得到结果。我刚检查了服务器登录cpanel,发现访问权限已被拒绝到结果页面。在填充下拉列表时,下拉页面的访问权限必须正确。我不知道为什么它被阻止了。服务器运行PHP 5.3.27和MYSQL 5.5.36。

这是下拉列表。

 <form action="search3.php" method="post" >

<?php

    $mysqli = new mysqli('localhost','user','password','engraved_stamps');
    if ($mysqli->connect_error) {
    die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
    }

    $query = "SELECT DISTINCT Country FROM engravers ORDER BY Country";
    $result = $mysqli->query($query);

    ?>
    <select >
    <?php

       while ($row = $result->fetch_assoc()) {
         echo "<option value=\"{$row['Country']}\">";
         echo $row['Country'];
         echo "</option>";
    }
    $mysqli->close();
    ?>
      </select>

              <input type="submit" name="dropdown" />   
              </form>

这是结果页面(restul的动作)(称为Search3.php)

    <?php

    $hostname = "localhost";
    $user = "user";
    $password = "password";
    $connection = mysqli_connect($hostname, $user, $password,);
    if(!$connection){
    echo"Could not connect to server";
    };
    mysqli_select_db($connection,'engraved_stamps');
    if(!mysqli_select_db($connection,'engraved_stamps')){
    echo"could not connect to database";
    };

    if(isset($_POST['dropdown'])){
    $Country = $_POST['dropdown'];
    }else{
    $Country = "none";
    }
    $sql= "SELECT * FROM engravers WHERE Country = :Country";
    $stmt = $pdo->prepare($sql);
    $stmt->bindParam(':Country', $Country, PDO::PARAM_STR);
    $stmt->execute();
    $total = $stmt->rowCount();

    while ($row = $stmt->fetchObject()) {
    echo $row->Country;
    }

    $mysqli->close();

        ?>

4 个答案:

答案 0 :(得分:1)

我认为PDO和mysqli是不同的PHP扩展。你应该只选择一件事来处理你的工作。

例如,您可以只使用mysqli或PDO。

有关PDO的详细信息,请参阅:http://php.net/manual/en/pdo.construct.php

答案 1 :(得分:0)

您正在混合两个完全不同的数据库扩展:

  1. MySQL Improved Extension(又名 mysqli ):

    $mysqli = new mysqli(...)
                  ^^^^^^
    
  2. PHP Data Objects(又名 PDO ):

    $stmt->bindParam(':Country', $Country, PDO::PARAM_STR);
                                           ^^^
    
  3. 尽管你的问题标题和标签,你的代码似乎完全使用mysqli。你只需要摆脱PDO位。

    您还使用了一些未定义的变量:

    $stmt = $pdo->prepare($sql);
            ^^^^
    

    您显然没有收到相应的错误消息,这表明您尚未配置PHP开发框来执行此操作。一旦你这样做,你可能会被警告其他一些问题。

答案 2 :(得分:0)

<?php
try {
    //here add user and password and database please be sure u have used correct one
$dbh = new PDO("mysql:host=localhost;dbname=engraved_stamps",'root','');
    }
catch(PDOException $e)
    {
    echo $e->getMessage();
    }
?>

<?php

$STM = $dbh->prepare("SELECT * FROM engravers WHERE Country = :Country");
 if(isset($_POST['dropdown'])){
    $Country = $_POST['dropdown'];
    }else{
    $Country = "name";//here you can assing % too for value of any if u need tell me to recode, NOTE here now the value u have assign is (name) is the record value on ur engravers table for column of Country.
    }


$STM->bindParam(':Country', $Country, PDO::PARAM_STR);
$STM->execute();                    
$row= $STM->fetchAll();
 if(count($row)){
    foreach($row as $data){
        echo $data['Country'] . "<br />";// here country is the column of ur table u can echo other columns too
    }
 }else {
    echo 'no row found';
 }

?> 

答案 3 :(得分:0)

抱歉abas_rafiq。注释只允许一定数量的字符。 这是结构: CREATE TABLE IF NOT NOT EXISTS engravers(   Key int(10)NOT NULL AUTO_INCREMENT,   StampImages文本NOT NULL,   Images文本NOT NULL,   Country文本NOT NULL,   Year int(4)NOT NULL,   Description文本NOT NULL,   SGNumber文本NOT NULL,   ScottNumber文本NOT NULL,   Engraver1Surname文本NOT NULL,   Engraver1OtherNames文本NOT NULL,   Engraver2Surname文本NOT NULL,   Engraver2OtherNames文本NOT NULL,   Engraver3Surname文本NOT NULL,   Engraver3OtherNames文本NOT NULL,   Designer1Surname文本NOT NULL,   Designer1OtherNames文本NOT NULL,   Designer2Surname文本NOT NULL,   Designer2OtherNames文本NOT NULL,   Printer文本NOT NULL,   Notes文本NOT NULL,   PRIMARY KEY(Key),   独特的钥匙KeyKey) )ENGINE = MyISAM DEFAULT CHARSET = latin1 AUTO_INCREMENT = 91; 他们不是全部&#34; Not Null&#34;当我设置它但我看到它们现在。