'参数无效......'导致致命错误:调用非对象

时间:2014-04-11 12:22:19

标签: php mysql pdo

我的想法是创建一个简单的地址脚本,这意味着,您将邮政编码放在上面并使用相同的邮政编码获取地址。

我收到三条错误消息:

  

警告:在第20行的/var/www/tabelle/eingabe2/index.php中为foreach()提供的参数无效

     

注意:第28行/var/www/tabelle/eingabe2/index.php中的未定义索引:suche

     

致命错误:在第31行的/var/www/tabelle/eingabe2/index.php中的非对象上调用成员函数quote()

<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="description" content="Adressen Datenbank">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Testside</title>
        <link rel="stylesheet" href="css/style.css">
        <link rel="author" href="humans.txt">
    </head>
    <body>
    <h1>Testside</h1>
    <form action="<? echo $PHP_SELF?>" method= "POST">
        <textarea cols="10" rows="1" name="suche" wrap=virtual></textarea>
        <input type="submit" value="Suchen">
    </form>
<?php
try {
    $dbh = new PDO('mysql:host=localhost;dbname=dbaname', 'user', 'pass');
    foreach($dbh->query('SELECT * from pizzadb2') as $row) {
        print_r($row);
    }
    $dbh = null;
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}
$suche = ($_POST['suche']);

$sql = "SELECT laden_name, vorwahl, telenr1, plz, ort, 
linkname1, forum_link FROM pizzeria_table WHERE plz = " .$dbh->quote($suche);

#FROM pizzeria_table";
#$result = mysql_query($sql);
$result = $dbh->query($sql,PDO::FETCH_ASSOC);
if (!$result || $result->rowCount()==0) { 
echo "<p>Keine Datensätze gefunden</p>\n";
}


$felder = mysql_num_fields($result);
$datensaetze = mysql_num_rows($result);
echo "<p>Anzahl der Datensätze: <b>$datensaetze</b></p>\n";
echo "<table border='1' cellspacing='0'>\n"; // Tabelle beginnen
echo "<tr>"; // Zeile erzeugen
for ($i = 0;$i < $felder;$i++) {
    $feldname = mysql_field_name($result, $i);
    echo "<th>$feldname</th>";
} 
echo "</tr>\n"; // Zeile schließen

while ($row = mysql_fetch_assoc($result)) {
    echo "<tr>"; // Zeile erzeugen
    foreach ($row as $key => $value) {
        echo "<td>$value&nbsp;</td>";
    } 
    echo "</tr>\n"; // Zeile schließen
} 
echo "</table>\n"; // Tabelle schließen
mysql_close($dp);
?>

    </body>
</html>

2 个答案:

答案 0 :(得分:0)

你正在使用PDO绝对错误的方式。基本问题是:

  1. 您必须在
  2. 上启用例外
  3. 您应该使用预准备语句而不是手动引用值
  4. 你不应该杀死你的PDO连接。
  5. 你不应该在这里使用try / catch
  6. PHP中没有自动全局变量。
  7. 所以这就是

    <?php
    
    $dbh = new PDO('mysql:host=localhost;dbname=dbaname', 'user', 'pass');
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
    $dbh->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
    
    $shuchen = $dbh->query('SELECT * from pizzadb2')->fetchAll();
    ?>
    <!doctype html>
    <html>
    <head>
        <meta charset="utf-8">
        <meta name="description" content="Adressen Datenbank">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Testside</title>
        <link rel="stylesheet" href="css/style.css">
        <link rel="author" href="humans.txt">
    </head>
    <body>
    <h1>Testside</h1>
    <form>
        <select name="suche">
        <?php foreach ($suchen as $suche): ?>
            <option><?=$suche?></option>
        <?php endforeach ?>
        </select>
        <input type="submit" value="Suchen">
    </form>
    <?php
    if (!isset($_GET['suche'])) die;
    
    $sql = "SELECT laden_name, vorwahl, telenr1, plz, ort, linkname1, forum_link 
            FROM pizzeria_table WHERE plz = ?";
    $stmt = $dbh->prepare($sql);
    $stmt->execute([$_GET['suche']]);
    $data = $stmt->fetchAll();
    if (!$data) { 
        echo "<p>Keine Datensätze gefunden</p>\n";
    } else {
        echo "<p>Anzahl der Datensätze: <b>$datensaetze</b></p>\n";
        echo "<table border='1' cellspacing='0'>\n"; // Tabelle beginnen
        echo "<tr>"; // Zeile erzeugen
        foreach (array_keys($data[0]) as $feldname) {
            echo "<th>$feldname</th>";
        } 
        echo "</tr>"; // Zeile erzeugen
        foreach ($data as $row) {
            echo "<tr>"; // Zeile erzeugen
            foreach ($row as $value) {
                echo "<td>$value&nbsp;</td>";
            } 
            echo "</tr>\n"; // Zeile schließen
        } 
        echo "</table>\n"; // Tabelle schließen
    }
    

    上次修改以修复上一个错误

答案 1 :(得分:-1)

您收到的错误表明您的查询失败 - 它不会引发异常,但会导致->query()返回false,这显然不是有效foreach参数!

您应该做的是将查询结果分配给变量并检查它:

$result = $dbh->query("select * from pizzadb2");
if($result === false) throw new PDOException(var_export($dbh->errorInfo(),true));
foreach($result as $row) {
    print_r($row);
}