0

i made php code with database connection named test.php like below :

<?
$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();
?>

it works when i call via browser http://localhost/learn/test.php and data inserted to my database

but when i running in 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();'

no data inserted. update now i tes to eval($code); seems OKAY in windows but not in LINUX

3
  • what error your getting ? Commented Jan 29, 2014 at 9:21
  • The syntax highlighter seems to think you have a quote escaping issue.. Commented Jan 29, 2014 at 9:30
  • now i tes to eval($code); seems OKAY in windows but not in LINUX Commented Jan 30, 2014 at 9:12

3 Answers 3

1

try this

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();'
Sign up to request clarification or add additional context in comments.

1 Comment

change (NULL, \'defender\', \'tortoise\') to (NULL, \"defender\", \"tortoise\")
0

I guess you have kind of a syntax error there. You're starting your php code with ' and at the INSERT INTO statement you're wrapping your values also with '. Maybe that's the problem?

Comments

0

Try this

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();'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.