My code is quite simple.It is about submit some data to mysql database. It did work to use normal way to submit data like query(). Code is here:
$con = new mysqli('127.0.0.1','xxx','xxx','xxx');
$con->query("SET NAMES 'utf8'");
$stmt=$con->prepare("INSERT INTO comments (Nickname, Content, Img) VALUES (?, ?, ?)");
$stmt->bind_param('sss', $nickname, $comments, $proc_comments);
$stmt->execute();
When I submitted to this php webpage, it did not pointing out any mistakes. I'm a university student who are not majoring CS or other relative subjects.Please help me.Thanks.
Update: This following code can work on the lnmp platform.
$con = new mysqli('localhost','user','passwd','database_name');
$con->query("SET NAMES 'utf8'");
$stmt=$con->prepare("UPDATE comments SET Likes=Likes+1 WHERE Num=?");
$stmt->bind_param('i',$favor_num);
$stmt->execute();
Last update: I try to check Nginx's error log to see if there is any mistake I missed, but it did not have any problem relative to this strange thing.
It could be many things, but since you have different results on different environments, the two most likely causes are:
Your lnmp platform wasn't compiled with mysqli support. Check by creating a page called 'phpinfo.php' with only <?php phpinfo();
in it. Load that page in your browser, and make sure there's a section with 'mysqli' as its title. Read its description and verify it's there.
Your connection data is different on LNMP. It could be as simple as the database name, user name, and password are different on your LNMP box and need to be reconfigured. Go to a terminal, and type mysql -u <username> -p <databasename>
then when prompted verify you can query the database (e.g. do a SHOW TABLES;
, SELECT * FROM comments
, etc.) If you can query it, but you can't manually run that 'INSERT INTO...' from above, then your user is setup but doesn't have correct permissions to do an insert on that table. Google around for MySQL help with configuring that.
This is most certainly wrong:
$con->query("SET NAMES 'utf8'");
Charsets in MySQL aren't denoted as strings, but as literals:
$con->query("SET NAMES utf8");
So that query failed, and the charset was not set, and hence you may run into problems trying to insert certain strings. You really should use the mysqli API for this to begin with though:
$con->set_charset('utf8');
Beyond this, it's unclear what exactly "doesn't work" or what other pitfalls there may be.