doucang8303 2014-06-30 13:29
浏览 35
已采纳

搜索器中的mysql_query()错误(无效查询)[关闭]

this is a searcher program php with MySqL this give me a error, and i need a bit help on this...

This is the php Code:

<?php 
if ($_POST['buscar'])
{
// Tomamos el valor ingresado
$buscar = $_POST['palabra'];
// Si está vacío, lo informamos, sino realizamos la búsqueda
if(empty($buscar))
{
echo "No se ha ingresado una cadena a buscar";
}else{
//Conexión a la base de datos
$servidor = "localhost"; //Nombre del servidor
$usuario = "root"; //Nombre de usuario en tu servidor
$password = "1234"; //Contraseña del usuario
$base = "db_maquinas"; //Nombre de la BD
$con = mysql_connect($servidor, $usuario, $password) or die("Error al conectarse al servidor");
mysql_select_db($base, $con) or die("Error al conectarse a la base de datos");
$sql= mysql_query("SELECT * FROM repuestos WHERE id LIKE '%$buscar%' AND descripcion LIKE '%$buscar%' ORDER BY id", $con) or die(mysql_error($con)); 
$result = mysql_query($sql, $con);  //<----LINE 32!!!
// Tomamos el total de los resultados
if($result) { $total = mysql_num_rows($result); } else { die('Invalid query' . mysql_error($con)); }
  echo "<table border = '1'> 
"; 
//Mostramos los nombres de las tablas 
echo "<tr> 
"; 
while ($field = mysql_fetch_field($result)){ 
        echo "<td>$field->name</td> 
"; 
} 
  echo "</tr> 
"; 
  do { 
        echo "<tr> 
"; 

        echo "<td>".$row["id"]."</td> 
"; 

        echo "<td>".$row["descripcion"]."</td> 
"; 

        echo "<td>".$row["cantidad"]."</td> 
"; 

        echo "</tr> 
"; 

  } while ($row = mysql_fetch_array($result));

        echo "</table> 
"; 
echo "¡ No se ha encontrado ningún registro !"; 
} 
}
?> 

The error is --> Warning: mysql_query() expects parameter 1 to be string, resource given in C:\xampp\htdocs\maquinas2000\paginas\buscarepuestos.php on line 32 Invalid query {Line 32 is -> $result = mysql_query($sql, $con); }

i work with a Localhost xampp ofc, this give me a lot of troubles this code, i need only this and i'll finish 100% the work, so if anyone can give me the answer of this error i'll be very grateful for that, thx!

  • 写回答

1条回答 默认 最新

  • drux41001 2014-06-30 13:32
    关注

    You have already executed the query. mysql_query return true or false and you are passing this return value again in mysql_query , make changes this :

     $sql= "SELECT * FROM repuestos WHERE id LIKE '%$buscar%' AND descripcion LIKE '%$buscar%' ORDER BY id";// remove mysql_query from this line
     $result = mysql_query($sql, $con);
    

    Important : mysql_ is depricated use mysqli instead of that

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改