2014-01-29 36 views
0

我与数据库连接的PHP代码名为test.php的象下面这样:PHP代码在PHP不行CLI

<? 
$mysqli = new mysqli("localhost","root","","monster"); 

/* check connection */ 
if (mysqli_connect_errno()) { 
    printf("Connect failed: %s\n", mysqli_connect_error()); 
    exit(); 
} 

/* return name of current default database */ 
if ($result = $mysqli->query("INSERT INTO `earth` (`monster_id`, `type`, `name`) VALUES 
(NULL, 'defender', 'tortoise') 
")) { 
    $row = $result->fetch_row(); 
    printf("Default database is %s.\n", $row[0]); 
    $result->close(); 
} 
$mysqli->close(); 
?> 

它工作时,我通过插入到我的数据库浏览器http://localhost/learn/test.php和数据呼叫

但当我运行在PHP CLI

php -r ' $mysqli = new mysqli("localhost","root","","monster"); 

    /* check connection */ 
    if (mysqli_connect_errno()) { 
     printf("Connect failed: %s\n", mysqli_connect_error()); 
     exit(); 
    } 

    /* return name of current default database */ 
    if ($result = $mysqli->query("INSERT INTO `earth` (`monster_id`, `type`, `name`) VALUES 
    (NULL, 'defender', 'tortoise') 
    ")) { 
     $row = $result->fetch_row(); 
     printf("Default database is %s.\n", $row[0]); 
     $result->close(); 
    } 
    $mysqli->close();' 

没有插入数据。 更新现在我试用eval($ code);似乎OKAY在Windows中,但不是在LINUX

+0

什么样的错误你得到些什么? –

+0

语法高亮显示似乎认为你有一个报价逃跑问题.. – Ben

+0

现在我试用eval($ code); (NULL,\'defender \',\'tortoise \') 到 (NULL,\“defender \”,\“tortoise \”)看起来OKAY在windows中却没有在LINUX –

回答

1

试试这个

php -r ' $mysqli = new mysqli("localhost","root","","monster"); 

    /* check connection */ 
    if (mysqli_connect_errno()) { 
     printf("Connect failed: %s\n", mysqli_connect_error()); 
     exit(); 
    } 

    /* return name of current default database */ 
    if ($result = $mysqli->query("INSERT INTO `earth` (`monster_id`, `type`, `name`) VALUES 
    (NULL, \'defender\', \'tortoise\') 
    ")) { 
     $row = $result->fetch_row(); 
     printf("Default database is %s.\n", $row[0]); 
     $result->close(); 
    } 
    $mysqli->close();' 
+0

仍然不能正常工作__ –

+0

更改 ) – NLSaini

+0

仍然不起作用NLSaini谢谢你的回复 –

0

我想你有一种语法错误那里。你正在用''开始你的php代码,并且在INSERT INTO语句中你也用'包装你的值'。也许这就是问题所在?

0

试试这个

php -r ' $mysqli = new mysqli("localhost","root","","monster"); 

/* check connection */ 
if (mysqli_connect_errno()) { 
    printf("Connect failed: %s\n", mysqli_connect_error()); 
    exit(); 
} 
$type="defender"; 
$name="tortoise"; 
/* return name of current default database */ 
if ($result = $mysqli->query("INSERT INTO `earth` (`monster_id`, `type`, `name`) VALUES 
(NULL, $type, $name)")) 
{ 
    $row = $result->fetch_row(); 
    printf("Default database is %s.\n", $row[0]); 
    $result->close(); 
} 
$mysqli->close();' 
+0

我已经让值变成了可变但仍然不起作用 –