dongmin3754 2014-06-27 13:02
浏览 22
已采纳

没有数据库选择php / mysql

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'> 
"; 
//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 !"; 
} 
}
?> 

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...

  • 写回答

2条回答 默认 最新

  • duanfei1975 2014-06-27 13:04
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?