0

我有一个从 mySql 数据库中选择的 php 函数:

function Master_file($name, $latin ){

  $HOST_DB ="localhost";

  $NAME_DB="nom";
  $USER_DB ="utilisaeur";
  $PWD_DB="K3Pud1";
  $connect = mysql_connect($HOST_DB,$USER_DB,$PWD_DB);
  $db=mysql_select_db($NAME_DB);


  $qry = "SELECT tax_id FROM  master where name =".$name." and latin =".$latin;
  echo $qry;
  $result = mysql_query($qry);

  while ($Res_user = mysql_fetch_assoc($result) ) {

    return $Res_user['tax_id'];
  }
}

显示错误Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/admin/public_html/hitlist/include/fg_membersite.php on line 446并且该行是while ($Res_user = mysql_fetch_assoc($result)

那么问题是什么?我该如何解决?

4

2 回答 2

1

尝试这个

function Master_file($name, $latin ){

    $dsn = 'mysql:host=localhost;dbname=nom';
    $username = 'utilisaeur';
    $password = 'K3Pud1';

    try {
        $db = new PDO($dsn, $username, $password);
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    } catch (PDOException $e) {
        echo $e->getMessage();
        exit;
    }


    $result = $db->prepare("SELECT tax_id FROM  master where name =:name");
    $result->bindValue(':name', $name);
    $result->execute();

    foreach($result->fetchAll(PDO::FETCH_ASSOC) as $row){
        echo $Res_user['tax_id'] . '<br />';
    }
}

EDIT 上面的函数刚刚更新为使用 PDO,显示任何错误,并将tax_id值输出到浏览器

于 2013-05-26T21:40:08.117 回答
0

你可以试试这个,因为你回到这里return $Res_user['tax_id'];,所以我认为你需要一个单行

function Master_file($name, $latin ){

    $HOST_DB ="localhost";
    $NAME_DB="nom";
    $USER_DB ="utilisaeur";
    $PWD_DB="K3Pud1";

    $connect = mysql_connect($HOST_DB,$USER_DB,$PWD_DB);
    if (!$connect) {
        die("Could not connect: " . mysql_error());
    }

    $db=mysql_select_db($NAME_DB, $connect);
    if (!$db) {
        die ("Can't use " . $NAME_DB . " : " . mysql_error());
    }

    $qry = "SELECT tax_id FROM  master where name ='" . $name . "' and latin = '" . $latin . "'";
    $result = mysql_query($qry);
    if( $result ){
        $row = mysql_fetch_assoc($result);
        return $row['tax_id'];
    }
}
于 2013-05-26T21:50:54.790 回答