[英]no database selected php/mysql
i have a trouble with this T_T this is a searching/consult script for cars parts 我对此T_T有麻烦,这是汽车零件的搜索/咨询脚本
this is the php 这是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... 我希望有人可以帮我解决这个问题。PD:我正在使用带有PhpMyAdmin的本地主机数据库,我在表上有项目,我验证了表不是空的...
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 . 仅供参考, 您不应在新代码中使用
mysql_*
函数 。 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.
而是了解准备好的语句 ,并使用PDO或MySQLi- 本文将帮助您确定哪一个。 If you choose PDO, here is a good tutorial .
如果您选择PDO, 这是一个很好的教程 。
将mysql_selectdb()
放在mysql_query()
之前。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.