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 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等