Error with PHP and MySQL

2

I have this connection

<?php
   # Conexion con la Base de Datos
   $userdb = 'administrador';
   $passworddb = '1234567';
   $dbhost= 'dbserver';
   $dbname = 'sk_modular';
?>

and I have I'm calling the database to bring me some data

require("conexion.php");
require("error.php");
$link= mysqli_connect($dbhost,$userdb,$passworddb,$dbname);

 if ($link->connect_error) {
        die("Connection failed: " . $link->connect_error);
    } 
  mysql_select_db($link) or die('No se pudo seleccionar la base de datos');
  $query= "SELECT DISTINCT sk_tipo FROM 'sk_standar' WHERE sk_tipo = 'ALTU-148' UNION SELECT DISTINCT sk_tipo FROM 'sk_standar' WHERE sk_tipo = 'ALTU-160'";
  $result = mysql_query($query)
            or die("Ocurrio un error en la consulta SQL");
            mysql_close();
  echo '<option value="0">Escoger altura</option>';
  while (($fila = mysql_fetch_array($result)) != NULL) {
    echo '<option value="'.$fila["sk_tipo"].'">'.$fila["sk_tipo"].'</option>';
  }
  //liberar resultador
  mysql_free_result($result);
  //Cerrar la conexión
  mysql_close($link);
?>

and when entering the url I get this error

  

Warning: mysql_select_db () expects parameter 1 to be string, object   given in D: \ Modular \ php \ DB.php on line 13 Could not select the   database

I do not know what I'm missing in

mysql_select_db()
    
asked by Eduard Zora 11.04.2017 в 03:09
source

3 answers

2

Possible errors in your code

Dejar de Usar la extensión Mysql por problemas de seguridad y además está obsoleta.

The final code would be like this:

require("conexion.php");
require("error.php");
$link= mysqli_connect($dbhost,$userdb,$passworddb,$dbname);

 if ($link->connect_error) {
        die("Connection failed: " . $link->connect_error);
    } 
  $query= "SELECT DISTINCT sk_tipo FROM 'sk_standar' WHERE sk_tipo = 'ALTU-148' UNION SELECT DISTINCT sk_tipo FROM 'sk_standar' WHERE sk_tipo = 'ALTU-160'";
  $result = mysqli_query($link,$query);
  echo '<option value="0">Escoger altura</option>';
  while ($row=mysqli_fetch_array($result,MYSQLI_ASSOC)) {
    echo '<option value="'.$fila["sk_tipo"].'">'.$fila["sk_tipo"].'</option>';
  }
  mysqli_free_result($result);
  mysqli_close($link);
?>
    
answered by 11.04.2017 / 04:40
source
3

Hello the error that is giving you, is because you are only passing the connection to the mysqli_select_db method; and you are missing the database mysqli_select_db (connection, "bdname"); . Now if you are using mysqli you do not need to call the mysqli_select_db method because the mysqli_connect you are passing the database, I leave you the example of a function that you can use to make your connection to the database Ej:

define("SERVER", "dbserver");
define("USERNAME", "administrador");
define("PASSWD", "1234567");
define("DATABASE", "sk_modular");    

public static function conexion(){
        include_once "config.php";
        $conexion = new mysqli(constant('SERVER'),constant('USERNAME'),constant('PASSWD'),constant('DATABASE'))or die('No se pudo conectar: ' . mysqli_connect_error());
        $conexion->query("SET NAMES 'utf8'");
        return $conexion;
    }

We call the connection and execute the query:

$db = classDb::conexion();    

$query= "SELECT DISTINCT sk_tipo FROM 'sk_standar' WHERE sk_tipo = 'ALTU-148' UNION SELECT DISTINCT sk_tipo FROM 'sk_standar' WHERE sk_tipo = 'ALTU-160'";
      $result = $db->query($query)
      echo '<option value="0">Escoger altura</option>';
      while (($fila = mysqli_fetch_array($result)) != NULL) {
        echo '<option value="'.$fila["sk_tipo"].'">'.$fila["sk_tipo"].'</option>';
      }

      //Cerrar la conexión
      mysqli_close($db);
  

Note: if you are using mysqli you can not combine it with mysql and it's something I'm seeing in your code.

    
answered by 11.04.2017 в 04:28
2
require("conexion.php");
require("error.php");
$link= mysqli_connect($dbhost,$userdb,$passworddb,$dbname);

 if ($link->connect_error) {
        die("Connection failed: " . $link->connect_error);
    } 
   $query= "SELECT DISTINCT sk_tipo FROM 'sk_standar' WHERE sk_tipo = 'ALTU-148' UNION SELECT DISTINCT sk_tipo FROM 'sk_standar' WHERE sk_tipo = 'ALTU-160'";
  $result = mysql_query($query)
            or die("Ocurrio un error en la consulta SQL");
            mysql_close();
  echo '<option value="0">Escoger altura</option>';
  while (($fila = mysql_fetch_array($result)) != NULL) {
    echo '<option value="'.$fila["sk_tipo"].'">'.$fila["sk_tipo"].'</option>';
  }

  //liberar resultador
  mysql_free_result($result);
  //Cerrar la conexión
  mysql_close($link);
?>

I guess you should delete the mysql_select_db() , put in the $ link and you're already passing the database to connect.

    
answered by 11.04.2017 в 03:18