Mysql插入查询无法在php脚本中工作 - 权限问题?

时间:2014-10-01 21:20:14

标签: permissions

问题描述

我无法使用php写入Mysql表。使用mysql客户端的本机命令行有效。但我可以在所有变种中做出“选择” - 看起来像是一个许可问题,但是什么样的? mysql凭据是相同的

我从存储库中安装了Apache,PHP和Mysql的全新Debian 7.6。 PHP很特别,我的应用程序需要PHP 5.3。 (原来7.6回购只有PHP 5.4)。所以我按照this answer中的建议安装了PHP。

测试

已安装的软件包:

root@{servername}:/var/www# dpkg -l | grep php
ii  libapache2-mod-php5                   5.3.3-7+squeeze19                  amd64        server-side, HTML-embedded scripting language (Apache 2 module)
ii  php5                                  5.3.3-7+squeeze19                  all          server-side, HTML-embedded scripting language (metapackage)
ii  php5-cli                              5.3.3-7+squeeze19                  amd64        command-line interpreter for the php5 scripting language
ii  php5-common                           5.3.3-7+squeeze19                  amd64        Common files for packages built from the php5 source
ii  php5-curl                             5.3.3-7+squeeze19                  amd64        CURL module for php5
ii  php5-gd                               5.3.3-7+squeeze19                  amd64        GD module for php5
ii  php5-mysql                            5.3.3-7+squeeze19                  amd64        MySQL module for php5
ii  php5-suhosin                          0.9.32.1-1                         amd64        advanced protection module for php5
ii  php5-xmlrpc                           5.3.3-7+squeeze19                  amd64        XML-RPC module for php5
root@{servername}:/var/www# dpkg -l | grep mysql
ii  libdbd-mysql-perl                     4.021-1+b1                         amd64        Perl5 database interface to the MySQL database
ii  libmysqlclient16                      5.1.73-1                           amd64        MySQL database client library
ii  libmysqlclient18:amd64                5.5.38-0+wheezy1                   amd64        MySQL database client library
ii  mysql-client                          5.5.38-0+wheezy1                   all          MySQL database client (metapackage depending on the latest version)
ii  mysql-client-5.5                      5.5.38-0+wheezy1                   amd64        MySQL database client binaries
ii  mysql-common                          5.5.38-0+wheezy1                   all          MySQL database common files, e.g. /etc/mysql/my.cnf
ii  mysql-server                          5.5.38-0+wheezy1                   all          MySQL database server (metapackage depending on the latest version)
ii  mysql-server-5.5                      5.5.38-0+wheezy1                   amd64        MySQL database server binaries and system database setup
ii  mysql-server-core-5.5                 5.5.38-0+wheezy1                   amd64        MySQL database server binaries
ii  php5-mysql                            5.3.3-7+squeeze19                  amd64        MySQL module for php5
root@{servername}:/var/www# dpkg -l | grep apache
ii  apache2-mpm-prefork                   2.2.22-13+deb7u3                   amd64        Apache HTTP Server - traditional non-threaded model
ii  apache2-utils                         2.2.22-13+deb7u3                   amd64        utility programs for webservers
ii  apache2.2-bin                         2.2.22-13+deb7u3                   amd64        Apache HTTP Server common binary files
ii  apache2.2-common                      2.2.22-13+deb7u3                   amd64        Apache HTTP Server common files
ii  libapache-pom-java                    10-2                               all          Maven metadata for all Apache Software projects
ii  libapache2-mod-dnssd                  0.6-3                              amd64        Zeroconf support for Apache 2 via avahi
ii  libapache2-mod-php5                   5.3.3-7+squeeze19                  amd64        server-side, HTML-embedded scripting language (Apache 2 module)

我的PHP测试脚本:

root@{servername}:/var/www# cat testscript.php 
<?php

ini_set('display_errors', 'on');
error_reporting('E_ALL');

$db = mysql_connect('localhost', '{applicationname}', '{password}');
mysql_select_db('{applicationname}');

$result = mysql_fetch_assoc(mysql_query('SELECT * from clients'));
print_r($result);
echo "\n\n";

$code = mysql_query("INSERT INTO clients VALUES ('2', 'TestEntry', 'Test xy', 0)");

$result = mysql_fetch_assoc(mysql_query('SELECT * from clients'));
print_r($result); print_r($code);
print mysql_error(); echo "\n\n";

$code = mysql_query("DELETE FROM clients where ClientID = 2");

$result = mysql_fetch_assoc(mysql_query('SELECT * from clients'));
print_r($result); print_r($code);
print mysql_error(); echo "\n\n";

原生mysql-client测试

mysql> use {applicationname}

Database changed
mysql> select * from clients;
+----------+------------+-------------------+--------------+
| ClientID | ClientName | ClientDescription | ParentClient |
+----------+------------+-------------------+--------------+
|        1 | CPN        | Company Name      |            0 |
+----------+------------+-------------------+--------------+
1 row in set (0.00 sec)

mysql> INSERT INTO clients VALUES ('2', 'TestEntry', 'Test xy', 0);
Query OK, 1 row affected (0.00 sec)

mysql> select * from clients;
+----------+------------+-------------------+--------------+
| ClientID | ClientName | ClientDescription | ParentClient |
+----------+------------+-------------------+--------------+
|        1 | CPN        | Company Name      |            0 |
|        2 | TestEntry  | Test xy           |            0 |
+----------+------------+-------------------+--------------+
2 rows in set (0.00 sec)

mysql> delete from clients where ClientID=2;
Query OK, 1 row affected (0.00 sec)

mysql> select * from clients;
+----------+------------+-------------------+--------------+
| ClientID | ClientName | ClientDescription | ParentClient |
+----------+------------+-------------------+--------------+
|        1 | CPN        | Company Name      |            0 |
+----------+------------+-------------------+--------------+
1 row in set (0.00 sec)

我的测试文件的输出

root@{servername}:/var/www# php testscript.php 
Array
(
    [ClientID] => 1
    [ClientName] => CPN
    [ClientDescription] => Company Name
    [ParentClient] => 0
)

Array
(
    [ClientID] => 1
    [ClientName] => CPN
    [ClientDescription] => Company Name
    [ParentClient] => 0
)
1

Array
(
    [ClientID] => 1
    [ClientName] => CPN
    [ClientDescription] => Company Name
    [ParentClient] => 0
)
1

结论(到目前为止)

假设没有发生错误,PHP不会在mysql_error()中抛出任何错误。从mysql_query()返回代码也没问题。必须有类似服务器级权限应用程序的东西。

这个debian是否启用了apparmor或selinux?我找不到/ selinux / setenforce(没有文件)

我应该查看哪些日志? / var / www / mysql *全是空的。

1 个答案:

答案 0 :(得分:0)

您发布的代码有点循环......您可以尝试以下内容吗?您的PHP测试似乎表明INSERTDELETE 有效(EG:print_r(true) == 1)。     

ini_set('display_errors', 'on');
error_reporting('E_ALL');

$db = mysql_connect('localhost', '{applicationname}', '{password}');
mysql_select_db('{applicationname}');

echo "--Initial Select\n--";
$result = mysql_fetch_assoc(mysql_query('SELECT * from clients'));
var_dump($result);

echo "--Initial Insert\n--";
$code = mysql_query("INSERT INTO clients VALUES ('2', 'TestEntry', 'Test xy', 0)");
var_dump($code);
var_dump(mysql_error());

echo "--Select after Insert\n--";
$result = mysql_fetch_assoc(mysql_query('SELECT * from clients'));
var_dump($result);

echo "--Delete\n--";
$code = mysql_query("DELETE FROM clients where ClientID = 2");
var_dump($code);
var_dump(mysql_error());

echo "--select after deletion--\n";
$result = mysql_fetch_assoc(mysql_query('SELECT * from clients'));
var_dump($result);