Ver Mensaje Individual
  #1 (permalink)  
Antiguo 20/10/2021, 15:38
pilucho
 
Fecha de Ingreso: noviembre-2004
Ubicación: NULL
Mensajes: 652
Antigüedad: 19 años, 5 meses
Puntos: 6
unir dos tablas en un fetch.php

Hola amigos del foro tengo la siguiente consulta.
trato de unir dos tablas y necesito traer el nombre de la otra tabla.
la cual hago la unión de la tabla pero no me da el resultado
es decir no muestra nada. dejo el código y comente la parte donde agregue el nombre
espero puedan ayudarme.


Nota: Solo resumí el index donde esta la table-responsive como ejemplo.


Fuente del Código Completo
Demo Aquí

Código MySQL:
Ver original
  1. CREATE TABLE `tbl_employee` (
  2.   `id` int(11) NOT NULL,
  3.   `id_customers` int(11) NOT NULL,
  4.   `name` varchar(50) NOT NULL,
  5.   `address` text NOT NULL,
  6.   `gender` varchar(10) NOT NULL,
  7.   `designation` varchar(100) NOT NULL,
  8.   `age` int(11) NOT NULL,
  9.   `images` varchar(150) NOT NULL
  10. ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
  11.  
  12. CREATE TABLE `tbl_customers` (
  13.   `id_customers` int(11) NOT NULL,
  14.   `name_customers` varchar(50) NOT NULL,
  15.   `address_customers` text NOT NULL
  16. ) ENGINE=MyISAM DEFAULT CHARSET=latin1;


index.php
Código HTML:
Ver original
  1. <table id="user_data" class="table table-bordered table-striped">
  2.        <thead>
  3.         <tr>
  4.          <td>Name</td>
  5.          <td>Gender</td>
  6.          <td>Designation</td>
  7.          <td>Age</td>
  8.          <td>Name Customers</td>
  9.          <td>View</td>
  10.          <td>Edit</td>
  11.          <td>Delete</td>
  12.         </tr>
  13.        </thead>





fetch.php

Código PHP:
<?php

//fetch.php

include('database_connection.php');
$query '';
$output = array();
// $query .= "SELECT * FROM tbl_employee "; 
$query .= "SELECT * FROM tbl_employee tb1 LEFT JOIN tbl_customers tb2 ON tb2.id_customers = tb1.id_customers "// NO FUNCIONA
if(isset($_POST["search"]["value"]))
{
 
$query .= 'WHERE name LIKE "%'.$_POST["search"]["value"].'%" OR address LIKE "%'.$_POST["search"]["value"].'%" OR gender LIKE "%'.$_POST["search"]["value"].'%" OR designation LIKE "%'.$_POST["search"]["value"].'%" OR age LIKE "%'.$_POST["search"]["value"].'%" ';
}
if(isset(
$_POST["order"]))
{
 
$query .= 'ORDER BY '.$_POST['order']['0']['column'].' '.$_POST['order']['0']['dir'].' ';
}
else
{
 
$query .= 'ORDER BY id DESC ';
}
if(
$_POST["length"] != -1)
{
 
$query .= 'LIMIT ' $_POST['start'] . ', ' $_POST['length'];
}
$statement $connect->prepare($query);
$statement->execute();
$result $statement->fetchAll();
$data = array();
$filtered_rows $statement->rowCount();
foreach(
$result as $row)
{
 
$sub_array = array();
 
$sub_array[] = $row["name"];
 
$sub_array[] = $row["gender"];
 
$sub_array[] = $row["designation"];
 
$sub_array[] = $row["age"];
 
$sub_array[] = $row["name_customers"]; // NOMBRE DE LA OTRA TABLA
 
$sub_array[] = '<button type="button" name="view" id="'.$row["id"].'" class="btn btn-primary btn-xs view">View</button>';
 
$sub_array[] = '<button type="button" name="update" id="'.$row["id"].'" class="btn btn-warning btn-xs update">Update</button>';
 
$sub_array[] = '<button type="button" name="delete" id="'.$row["id"].'" class="btn btn-danger btn-xs delete">Delete</button>';
 
$data[] = $sub_array;
}

function 
get_total_all_records($connect)
{
 
$statement $connect->prepare("SELECT * FROM tbl_employee");
 
$statement->execute();
 
$result $statement->fetchAll();
 return 
$statement->rowCount();
}

$output = array(
 
"draw"    => intval($_POST["draw"]),
 
"recordsTotal"  =>  $filtered_rows,
 
"recordsFiltered" => get_total_all_records($connect),
 
"data"    => $data
);
echo 
json_encode($output);
?>