简体   繁体   English

为什么我的php绑定参数代码可以在wamp上运行,但在lnmp平台上却不起作用

[英]Why my php bind param code can run on wamp, but it didn't work on lnmp platform

My code is quite simple.It is about submit some data to mysql database. 我的代码很简单,它是向mysql数据库提交一些数据。 It did work to use normal way to submit data like query(). 使用正常方式提交数据(如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. 当我提交到该php网页时,它没有指出任何错误。 I'm a university student who are not majoring CS or other relative subjects.Please help me.Thanks. 我是一名不修CS或其他相关专业的大学生。请帮助我。谢谢。

Update: This following code can work on the lnmp platform. 更新:以下代码可在lnmp平台上运行。

    $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. 上次更新:我尝试检查Nginx的错误日志以查看是否遗漏了任何错误,但是相对于这种奇怪的事情,它没有任何问题。

It could be many things, but since you have different results on different environments, the two most likely causes are: 可能有很多事情,但是由于您在不同的环境下有不同的结果,因此两个最可能的原因是:

  1. Your lnmp platform wasn't compiled with mysqli support. 您的lnmp平台未使用mysqli支持进行编译。 Check by creating a page called 'phpinfo.php' with only <?php phpinfo(); 通过仅创建<?php phpinfo();创建一个名为'phpinfo.php'的页面进行检查 in it. 在里面。 Load that page in your browser, and make sure there's a section with 'mysqli' as its title. 在浏览器中加载该页面,并确保有一个标题为“ mysqli”的部分。 Read its description and verify it's there. 阅读其描述并验证它是否在那里。

  2. Your connection data is different on LNMP. 您的连接数据在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. 它可能很简单,因为LNMP盒上的数据库名称,用户名和密码不同,需要重新配置。 Go to a terminal, and type mysql -u <username> -p <databasename> then when prompted verify you can query the database (eg 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. 转到终端,然后键入mysql -u <username> -p <databasename>然后在出现提示时验证您可以查询数据库(例如,执行SHOW TABLES;SELECT * FROM comments等)。如果可以查询,但是您无法从上方手动运行该“ INSERT INTO ...”,则说明您的用户已设置,但没有正确的权限在该表上进行插入。 Google around for MySQL help with configuring that. Google周围的MySQL帮助您进行配置。

This is most certainly wrong: 这肯定是错误的:

$con->query("SET NAMES 'utf8'");

Charsets in MySQL aren't denoted as strings, but as literals: MySQL中的字符集不表示为字符串,而是文字:

$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: 您实际上应该首先使用mysqli API:

$con->set_charset('utf8');

Beyond this, it's unclear what exactly "doesn't work" or what other pitfalls there may be. 除此之外,还不清楚到底什么“不起作用”或可能存在其他陷阱。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM