2014-01-29 66 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

我已經讓值變成了可變但仍然不起作用 –