在php数据库查询中获取错误

时间:2014-06-03 13:10:38

标签: php mysql

我对php不太了解。这是一个php我用来发送android的数据。但在测试时,我正在使用html。从这个HTML我发送iddevice,latitud和纵向变量。

正如您在php代码中看到的那样,首先我得到与从数据库中的html发送的iddevice相关的名称。

问题是我没有从数据库中获取名称变量。这个名字应该是“manuel”,但是在我的mysql数据库中,我得到的是“资源ID#2”。其他变量在数据库行(iddevice,latitud和longitudinal)中是正确的。

为什么会发生这种情况? thx提前

php代码:

<?php

// conexion a la base de datos
mysql_connect("xxx", "xxx", "xxx") or die (mysql_error());

mysql_select_db("qry899");

// conseguimos el nombre
$newname = mysql_query("SELECT nombre FROM gcm_v3 WHERE iddevice='".$_POST["iddevice"]."'") or die(mysql_error());

// insertamos id de registro devuelto por el GCM
mysql_query("INSERT INTO position (iddevice, nombre, latitud, longitud) VALUES ('".$_POST["iddevice"]."', '$newname', '".$_POST["latitud"]."', '".$_POST["longitud"]."')") or die(mysql_error());

mysql_close();

?>

4 个答案:

答案 0 :(得分:3)

您应该使用mysql_fetch_assoc($res)获取如下行:

+ 请避免使用 mysql _ 。使用mysqliPDO

<?php

// conexion a la base de datos
mysql_connect("xxx", "yyyy", "zzz") or die (mysql_error());

mysql_select_db("ccc");

// conseguimos el nombre
$res1= mysql_query("SELECT nombre FROM gcm_v3 WHERE iddevice='".$_POST["iddevice"]."'") or die(mysql_error());//this will return a resource pointer

$record=mysql_fetch_assoc($res1);//this will return a record of pointed select the(res1)
$newname = $record['nombre'];// this will give you the specific field you were looking for 

// insertamos id de registro devuelto por el GCM
mysql_query("INSERT INTO position (iddevice, nombre, latitud, longitud) VALUES ('".$_POST["iddevice"]."', '$newname', '".$_POST["latitud"]."', '".$_POST["longitud"]."')") or die(mysql_error());

mysql_close();

?>

答案 1 :(得分:3)

您实际上并未从数据库中提取值,您需要使用mysql_fetch_assoc

// conseguimos el nombre
$result = mysql_query("SELECT nombre FROM gcm_v3 WHERE iddevice='".$_POST["iddevice"]."'") or die(mysql_error());
$row = mysql_fetch_assoc($result);
$newname = $row['nombre'];

你应该避免使用其他人提到的mysql *函数。

最后更改您的数据库登录详细信息,它们位于编辑历史记录中,以便任何人都可以访问它

答案 2 :(得分:0)

<?php
// conexion a la base de datos
mysql_connect("xxx", "xxx", "xxx") or die (mysql_error());

mysql_select_db("qry899");

// conseguimos el nombre
$newname = mysql_query("SELECT nombre FROM gcm_v3 WHERE iddevice='".$_POST["iddevice"]."'") or die(mysql_error());

$row = mysql_fetch_array($newname);
$newname1 = $row['latitud'];

// insertamos id de registro devuelto por el GCM
mysql_query("INSERT INTO position (iddevice, nombre, latitud, longitud) VALUES ('".$_POST["iddevice"]."', '$newname1', '".$_POST["latitud"]."', '".$_POST["longitud"]."')") or die(mysql_error());

mysql_close();

?>

答案 3 :(得分:0)

只要你这样做,我就永远不会工作mysql_query("SELECT nombre FROM gcm_v3 WHERE iddevice='".$_POST["iddevice"]."'")首先尝试将$_POST["iddevice"]分配给变量,然后在查询中使用该变量后,如下所示:

$device = $_POST["iddevice"];
mysql_query("SELECT nombre FROM gcm_v3 WHERE iddevice='$device'");
相关问题