简体   繁体   English

MySQL选择不返回资源

[英]MySQL Select Not Returning resource

I am having a small problem with my PHP MySQL Select. 我的PHP MySQL Select遇到了一个小问题。 The function is inside of a PHP class. 该函数位于PHP类中。 Here is the error I get: 这是我得到的错误:

Warning: mysql_fetch_array() expects parameter 1 to be resource, integer given in C:\\xampp\\htdocs\\include\\database.php on line 59 Warning: extract() expects parameter 1 to be array, null given in C:\\xampp\\htdocs\\include\\database.php on line 59 警告:mysql_fetch_array()期望参数1是资源,第59行的C:\\ xampp \\ htdocs \\ include \\ database.php中给出的整数警告:extract()期望参数1为数组,在C:\\ xampp \\中给出null第59行的htdocs \\ include \\ database.php

The function just simply updates the database to show what browser and OS they visited the site with. 该功能只是简单地更新数据库,以显示他们访问该网站的浏览器和操作系统。 The function is called from another file that is called by an AJAX call that uses POST to send the data about the OS and browser that was gathered from a Javascript file. 该函数从另一个文件调用,该文件由AJAX调用调用,该调用使用POST发送有关从Javascript文件收集的操作系统和浏览器的数据。 It only fails if there is an entry of the IP address already in the database. 只有在数据库中已存在IP地址条目时才会失败。 If there is no IP Address entry in the database it succeeds in creating one. 如果数据库中没有IP地址条目,则成功创建一个。

Here is my code: 这是我的代码:

function addStat($browser, $os){
    $IE = 0; $Firefox = 0; $Safari = 0; $Opera = 0; $Chrome = 0; $otherb = 0;
    $Windows = 0; $Linux = 0; $Mac = 0; $Android = 0; $iOS = 0; $otheros = 0;
    $ql = 0; $totalVisits = 0;
    $ip = ip2long($_SERVER['REMOTE_ADDR']);
    $q1 = mysql_query("SELECT * FROM " . DB_STATS . " WHERE ip='$ip'", $this->connection);
    if (mysql_num_rows($q1)==0){
        $browser = mysql_real_escape_string($browser);
        $os = mysql_real_escape_string($os);
        switch($browser){
            case "Internet Explorer":
                $IE += 1;
            break;
            case "Firefox":
                $Firefox += 1;
            break;
            case "Safari":
                $Safari += 1;
            break;
            case "Opera":
                $Opera += 1;
            break;
            case "Chrome":
                $Chrome += 1;
            break;
            default:
                $otherb += 1;
            break;
        }
        switch($os){
            case "Windows":
                $Windows += 1;
            break;
            case "Mac OS X":
                $Mac += 1;
            break;
            case "Linux":
                $Linux += 1;
            break;
            case "Android":
                $Android += 1;
            break;
            case "iOS":
                $iOS += 1;
            break;
            default:
                $otheros += 1;
            break;
        }
        $q = $this->query("INSERT INTO " . DB_STATS . " VALUES (null, '$ip', '$Chrome', '$IE', '$Firefox', '$Opera', '$Safari', '$otherb', '$Windows', '$Mac', '$Linux', '$Android' , '$iOS' , '$otheros', 1)");
        if ($q == true){
           return(1);
        }
        else{
           return(0);
        }
    }
    else if (mysql_num_rows($q1)==1){
        extract(mysql_fetch_array($ql));
        switch($browser){
            case "Internet Explorer":
                $IE += 1;
            break;
            case "Firefox":
                $Firefox += 1;
            break;
            case "Safari":
                $Safari += 1;
            break;
            case "Opera":
                $Opera += 1;
            break;
            case "Chrome":
                $Chrome += 1;
            break;
            default:
                $otherb += 1;
            break;
        }
        switch($os){
            case "Windows":
                $Windows += 1;
            break;
            case "Mac OS X":
                $Mac += 1;
            break;
            case "Linux":
                $Linux += 1;
            break;
            case "Android":
                $Android += 1;
            break;
            case "iOS":
                $iOS += 1;
            break;
            default:
                $otheros += 1;
            break;
        }
        $totalVisits += 1;
        $q = $this->query("UPDATE " . DB_STATS . " set Chrome='$Chrome', IE='$IE', Firefox='$Firefox', Opera='$Opera', Safari='$Safari', otherb='$otherb', Windows='$Windows', Mac='$Mac', Linux='$Linux', Android='$Android' , iOS='$iOS' , otheros='$otheros', totalVisits='$totalVisits'");
        if ($q == true){
           return(1);
        }
        else{
           return(0);
        }
    }
    else{
        return(-1);
    }
}

I hope everything made sense and that someone will help. 我希望一切都有意义,有人会帮助。

I see it now -- you used $ql (lower case L) when you intend to use $q1 . 我现在看到了 - 你打算使用$q1时使用$ql (小写L)。 Let this be a lesson against using very short variable names or very similar names. 让这成为使用非常短的变量名或非常相似的名称的教训。

// $ql was initialized to 0
$ql = 0; $totalVisits = 0;

// $q1 holds the result resource
extract(mysql_fetch_array($q1));

It is not advisable to call extract() on the output of mysql_fetch_array() unless you also specify the second parameter MYSQL_ASSOC as the fetch type. 除非您还将第二个参数MYSQL_ASSOC指定为fetch类型,否则不建议在mysql_fetch_array()的输出上调用extract() By default it returns both numeric and associative indices for each column. 默认情况下它返回每列数字关联索引。

extract(mysql_fetch_array($q1, MYSQL_ASSOC));
// Or better
extract(mysql_fetch_assoc($q1));

In general, I would probably advise against using extract() in most any situation, since it results in numerous variables dumped into the global namespace, in particular when you have done SELECT * without being specific about which columns are selected. 一般来说,我可能会建议不要在大多数情况下使用extract() ,因为它会导致许多变量被转储到全局命名空间中,特别是当你完成了SELECT *而没有具体说明选择了哪些列时。 Better to access them via their array: 最好通过他们的阵列访问它们:

$row = mysql_fetch_assoc($q1);
echo $row['browser'];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM