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.

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

报告相同问题?

悬赏问题

  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改