dongtongjian3127 2013-05-26 21:24
浏览 61
已采纳

从php函数中的选择查询中获取结果

I have a php function of selection from mySql database:

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'];
  }
}

an error is shown 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 and the line is while ($Res_user = mysql_fetch_assoc($result)

So what is the problem ? How can i fix it?

  • 写回答

2条回答 默认 最新

  • doujia1163 2013-05-26 21:40
    关注

    Try this

    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 The function above has just been updated to use PDO, display any errors, and output the tax_id value to the browser

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度