-1

i have a trouble with this T_T this is a searching/consult script for cars parts

this is the php

    <?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");
$sql= mysql_query("SELECT * FROM repuestos WHERE 'id','descripcion' LIKE '%$buscar%' ORDER BY id", $con) or die(mysql_error($con)); 
mysql_select_db($base, $con) or die("Error al conectarse a la base de datos");

$result = mysql_query($sql, $con);

// Tomamos el total de los resultados
if($result) { $total = mysql_num_rows($result); } else { die('Invalid query' . mysql_error($con)); }


      echo "<table border = '1'> \n"; 
//Mostramos los nombres de las tablas 
echo "<tr> \n"; 

while ($field = mysql_fetch_field($result)){ 
            echo "<td>$field->name</td> \n"; 
} 
      echo "</tr> \n"; 

do { 
            echo "<tr> \n"; 

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

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

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

            echo "</tr> \n"; 

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

            echo "</table> \n"; 

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

that when on the form i press "Send or Search" this send me to another page that says "NO DATABASE SELECTED"

I hope somebody can help me with that.. PD: i'm using a localhost DB with PhpMyAdmin i have items on tables, i verified tables not empty...

  • 3
    [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](http://j.mp/XqV7Lp). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). – esqew Jun 27 '14 at 13:04

2 Answers2

2

You select your database after you attempt to run queries. Place the code before you attempt to run queries:

mysql_select_db($base, $con) or die("Error al conectarse a la base de datos");
$sql= mysql_query("SELECT * FROM repuestos WHERE 'id','descripcion' LIKE '%$buscar%' ORDER BY id", $con) or die(mysql_error($con)); 

FYI, you shouldn't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Zoe
  • 27,060
  • 21
  • 118
  • 148
John Conde
  • 217,595
  • 99
  • 455
  • 496
  • Excellent, now when i send again. says me "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''descripcion' LIKE '%1%' ORDER BY id' at line 1" i'm using last xampp with phpmyadmin, idk – Ramón León Jun 27 '14 at 13:12
  • `WHERE 'id','description'` is invalid syntax. – John Conde Jun 27 '14 at 13:14
  • i change it to `WHERE id, descripcion LIKE` but send me the same message. – Ramón León Jun 27 '14 at 13:20
  • `WHERE id LIKE '%$buscar%' AND descripcion LIKE '%$buscar%'` – John Conde Jun 27 '14 at 13:20
  • good one! now i think I hope.. pls god say yes.. >.< is the last erro now send me error on different line.. this time is Line 32 that says `Warning: mysql_query() expects parameter 1 to be string, resource given in C:\xampp\htdocs\maquinas2000\paginas\buscarepuestos.php on line 32 Invalid query` the line 32 is. -> ´$result = mysql_query($sql, $con);´ :D thx – Ramón León Jun 27 '14 at 13:26
  • That's odd. It's like your query disappeared. – John Conde Jun 27 '14 at 13:30
  • yes like that... :S i have one week like a idiot only on this php is the last thing for put the page 100% working – Ramón León Jun 27 '14 at 13:32
  • Since your query is hardcoded into mysql_query() I don't know how that could happen. – John Conde Jun 27 '14 at 13:33
0

place the mysql_selectdb() before the mysql_query().

Gerald Schneider
  • 17,416
  • 9
  • 60
  • 78