将mysql更改为mysqli错误

时间:2014-09-06 06:24:04

标签: php mysql mysqli phpmyadmin

您好我很难让这段代码在我的服务器上运行,运行PHP 5.5 代码来自www.ip2nation.com

<?php

$server   = ''; // MySQL hostname
$username = ''; // MySQL username
$password = ''; // MySQL password
$dbname   = ''; // MySQL db name


$db = mysql_connect($server, $username, $password) or die(mysql_error());
      mysql_select_db($dbname) or die(mysql_error());

$sql = 'SELECT 
            c.country 
        FROM 
            ip2nationCountries c,
            ip2nation i 
        WHERE 
            i.ip < INET_ATON("'.$_SERVER['REMOTE_ADDR'].'") 
            AND 
            c.code = i.country 
        ORDER BY 
            i.ip DESC 
        LIMIT 0,1';

list($countryName) = mysql_fetch_row(mysql_query($sql));

// Output full country name
echo $countryName;

&GT?;

运行代码时遇到错误,例如:

PHP Parse error:  syntax error, unexpected ''.$_SERVER['' (T_CONSTANT_ENCAPSED_STRING) in /test1.php on line 14

任何帮助将非常感谢!感谢

2 个答案:

答案 0 :(得分:0)

试试这个

$db = mysqli_connect($server, $username, $password) or die(mysqli_error($db));
mysqli_select_db($db,$dbname) or die(mysqli_error($db));

$sql = 'SELECT 
        c.country 
    FROM 
        ip2nationCountries c,
        ip2nation i 
    WHERE 
        i.ip < INET_ATON("'.$_SERVER['REMOTE_ADDR'].'") 
        AND 
        c.code = i.country 
    ORDER BY 
        i.ip DESC 
    LIMIT 0,1';

list($countryName) = mysqli_fetch_row(mysqli_query($db,$sql));

// Output full country name
echo $countryName;

答案 1 :(得分:0)

您可以尝试以下代码,

    <?php
    $server   = ''; // MySQL hostname
    $username = ''; // MySQL username
    $password = ''; // MySQL password
    $dbname   = ''; // MySQL db name


    $db = mysql_connect($server, $username, $password) or die(mysql_error());
    mysql_select_db($dbname) or die(mysql_error());

    $sql = "SELECT 
            c.country 
             FROM 
               ip2nationCountries c,
               ip2nation i 
             WHERE 
               i.ip < INET_ATON('". $_SERVER['REMOTE_ADDR'] . "') 
               AND 
               c.code = i.country 
               ORDER BY 
               i.ip DESC 
             LIMIT 0,1";

    list($countryName) = mysql_fetch_row(mysql_query($sql));

    // Output full country name
    echo $countryName;  
    ?>