简体   繁体   English

MySQL在PHP中失败,但在phpmyadmin中工作

[英]Mysql fails in php but works in phpmyadmin

I've made this a lot of times but now I can't :( 我已经做过很多次了,但是现在我不可以了:(

The insert allways return false but if I execute the same SQL script (taked from the output) it inserts in the database without any problem. 插入始终返回false,但是如果我执行相同的SQL脚本(从输出中获取),它将毫无问题地插入数据库。 I'm connected to the database because some values are fetched from another table. 我已连接到数据库,因为某些值是从另一个表中获取的。

This is my code: 这是我的代码:

     $query = "INSERT INTO normotensiones(fecha,macropera,pozo,equipo_pmx,equipo_compania,paciente,sexo,edad,id_compania,otra_compania,puesto,ta,tum,ove,coordinador) 
                                VALUES('$fecha','$macropera','$pozo','$equipo_pmx','$equipo_compania','$paciente','$sexo',$edad,$id_compania,'$otra_compania','$puesto','$ta','$tum','$ove','$coordinador')";
        if (mysql_query($query,$connection)){
            //OK
        } else {
            $errno = mysql_errno();
            $error = mysql_error();
            mysql_close($connection);
            die("<br />$errno - $error<br /><br />$query");
            exit;
        }

The output is: 输出为:

0 - 0-

INSERT INTO normotensiones(fecha,macropera,pozo,equipo_pmx, equipo_compania,paciente,sexo,edad,id_compania, otra_compania,puesto,ta,tum,ove,coordinador) 
                VALUES('20111001','P. ALEMAN 1739','P. ALEMAN 1715','726', 'WDI 838','SERGIO AYALA','M',33,21, '','','110/70','ROBERTO ELIEL CAMARILLO','VICTOR HUGO RAMIREZ','LIC. PABLO GARCES')

Looks like there are no error, but allways execute the code in the else part of the if instruction. 看起来没有错误,但是始终在if指令的else部分中执行代码。 Any idea? 任何想法? Thanks in advance. 提前致谢。

I think the issue might be you are missing the mysql_select_db line after the connection. 我认为问题可能是您在连接后缺少了mysql_select_db行。

After the connection with the database is established you need to select a DB. 与数据库建立连接后,您需要选择一个数据库。 Please make sure you have selected the Database that your desired table resides in. 请确保已选择所需表所在的数据库。

And you can even use the following snippets to get some useful informated through mysql_errors. 您甚至可以使用以下代码片段通过mysql_errors获得一些有用的信息。

    $connection = mysql_connect('localhost', 'root', 'password');

    if (!$connection) {
        die('<br>Could not connect: ' . mysql_error());
    }

    if (!mysql_select_db('db_name')) {
       die('Could not select database: ' . mysql_error());
    }

And try you insert query after these lines of code. 并尝试在这些代码行之后插入查询。 All the best. 祝一切顺利。

I agree with the others concerning the column types. 对于列类型,我同意其他观点。 INT is one of the only data types that do not require single quotes. INT是唯一不需要单引号的数据类型之一。

There are two blank strings. 有两个空白字符串。 There is a possibility that the variables are not defined, and therefore giving you a PHP exception (not even in the MySql yet) but that requires stricter-than-normal exception settings. 可能未定义变量,因此会给您一个PHP异常(甚至在MySql中也没有),但是需要比正常的异常设置更严格的设置。 I would personally look into the $connection variable. 我将亲自调查$ connection变量。 Before the SQL query statement, put this and send us the cleaned results: 在SQL查询语句之前,将其放入并发送给我们:

echo '<pre>'.var_dump($connection, true).'</pre>';

Additionally, on your mysql_connect function call, put 另外,在您的mysql_connect函数调用中,将

OR die('No connection')

afterwords. 后记。 Do the same thing with the mysql_select_db function, changing it to 'No DB Select' obviously. 使用mysql_select_db函数执行相同的操作,显然将其更改为“ No DB Select”。

Ultimately, we will need more information. 最终,我们将需要更多信息。 But changing to mysqli is very desirable. 但是更改为mysqli是非常可取的。

Oh! 哦! And make sure the permissions for the user you are connecting as are not changed. 并确保您要连接的用户的权限没有更改。 Sometimes I find people who connect to PhpMyAdmin using one user account but a different account in their PHP code. 有时我发现有人用一个用户帐户连接到PhpMyAdmin,但在其PHP代码中使用了另一个帐户。 This is problematic, and will lead to problems eventually, as you forget the different accounts, at times. 这是有问题的,并且最终会导致问题,因为您有时会忘记其他帐户。

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

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