doudu3961 2015-07-24 05:16
浏览 68
已采纳

从mysqli获取数据时未定义的变量

I'm new to php n I'm trying to make a phone to timezone converter. I'm getting the error undefined variable '$result' on line 49 How do I resolve it. Also if you can find out any other mistakes I've made Please chck for errors I'm making Phone number:

    <button type="submit" name="submit" action="submit">
        Submit
    </button>
</form>
    <?php
        $servername = "127.0.0.1";
        $username = "root";
        $password = "";
        $ccode=$_POST["phone"];
            // Create connection
            $db = new mysqli($servername, $username, $password);

            // Check connection
            if ($db->connect_error) {
                die("Connection failed: " . $db->connect_error);
            } 
        $pattern = '/^\(?[\d]{1,4}\)?( |-)?/';
        if(preg_match($pattern, $ccode, $matches))
        {

            $code = $matches[0];
        } else $code = substr($ccode, 0, 4);
        $q = "SELECT nicename FROM country.country WHERE phonecode=$code";
        if($query = $db->query($q)) 
        {
            $record = $query->fetch_assoc();
        } else echo "<br/>NO RECORD";
        echo '<pre>';print_r($record);

        if (empty($_POST["phone"]))
        {
            echo "<p>Please write a Phone Number!</p>";
        }
        $abb = "SELECT zone_id FROM zone INNER JOIN country on country.country_code = zone.country_code
                WHERE country.country_name = " . $record['nicename'];
        if($query = $db->query($abb)) 
        {
            $result = $query->fetch_assoc();
        } else echo "<br/>NO RECORD";
        echo '<pre>';print_r($result);
    ?>
</body>

Thanks for the help!

  • 写回答

1条回答 默认 最新

  • doushi5117 2015-07-24 05:41
    关注

    I believe this way it will works. I'm not sure about $code, if he contains the right information, you can check that by echo'ing the $code.

        if(preg_match($pattern, $ccode, $matches))
        {
            $code = $matches[0];
        } else { $code = substr($ccode, 0, 4); }
    
        $q = "SELECT nicename FROM country WHERE phonecode=$code";
        if($query = $db->query($q)) 
        {
            $record = $query->fetch_assoc();
            echo '<pre>';
            print_r($record);
        } else { echo "<br/>NO RECORD"; }
    
    
        $abb = "SELECT zone_id FROM zone INNER JOIN country on country.country_code = zone.country_code
                WHERE country.country_name = '" . $record['nicename'] . "'";
        if($query = $db->query($abb)) 
        {
            $result = $query->fetch_assoc();
            echo '<pre>';
            print_r($result);
            //Here $result will exist!
        } else { echo "<br/>NO RECORD"; }
        // You had your $result here, and here, the result may not exist, 
        // depending if the query succeeded or not.
        // Same counts for the query above.
    

    There were 2 problems as far as I can tell.

    1 was that you tried to print $record, while $record may not have existed, and the same counts for $result.

    2 was the zone-query, if you check if a column/field equals a certain string, then make sure the string has quotes around itself. So country_name='something'. You didn't have these quotes around $record['nicename'], so whatever came out of $record['nicename'], he thought it was a column/field, but not a value to check upon.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?