Reputation: 35
Ive been searching for a answer and have tried multiple things and still can't seem to figure this out no matter how I put it. Im missing something.. haha.
I have a php file getting data from mysql.
$query = "SELECT * FROM `UnitMaintbl` WHERE `Unit` LIKE '%{$value}%' ORDER BY Recnum DESC";
This works great, however I have a field that is a index of another table and I need the descriptions from the other table. So I need to INNER JOIN both table. Ok No problem.
$query = "SELECT * FROM `UnitMaintbl`
INNER JOIN `Statustbl` ON UnitMaintbl.StatusID=Statustbl.StatusDesc
ORDER BY Recnum DESC";
First I wanted to get the JOIN working based on any unit for testing. Then I wanted to get my $Value back in there from the form. So...
$query = "SELECT * FROM `UnitMaintbl`
INNER JOIN `Statustbl` ON UnitMaintbl.StatusID=Statustbl.StatusDesc
WHERE Unitmaintbl.Unit = '%{$value}%'"
ORDER BY Recnum DESC";
Ive tried a few different help scenarios but just can't seem to get this working.
Any help is greatly appreciated.... thanks in advance..
EDIT
<?php
$host = "localhost"; //Your database host server
$db = "PicorpBE"; //Your database name
$user = "username"; //Your database user
$pass = "password"; //Your password
$value=$_GET['Unit']; // Unit Number
$connection = mysql_connect($host, $user, $pass);
//Check to see if we can connect to the server
if(!$connection)
{
die("Database server connection failed.");
}
else
{
//Attempt to select the database
$dbconnect = mysql_select_db($db, $connection);
//Check to see if we could select the database
if(!$dbconnect)
{
die("Unable to connect to the specified database!");
}
else
{
$query = "SELECT * FROM `UnitMaintbl`
INNER JOIN `Statustbl` ON UnitMaintbl.StatusID=Statustbl.StatusDesc
WHERE Unitmaintbl.Unit = '%{$value}%'
ORDER BY Recnum DESC";
$resultset = mysql_query($query, $connection);
$records = array();
//Loop through all our records and add them to our array
while($r = mysql_fetch_assoc($resultset))
{
$records[] = $r;
}
//Output the data as JSON
echo json_encode($records);
}
}
?>
Ive redone the php and not I get:
[23-Dec-2015 14:26:52] PHP Warning: mysqli_query() [function.mysqli-query]: Empty query in /home/picorp05/public_html/json.php on line 25 [23-Dec-2015 14:26:52] PHP Warning: mysqli_close() expects parameter 1 to be mysqli, boolean given in /home/picorp05/public_html/json.php on line 45
<?php
// Variables From Search
$sunit=$_GET['Unit']; // Unit Number
// Create connection
$con=mysqli_connect("localhost","username","password","dbname");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// This SQL statement should select Unit ($sunit) from the table 'UnitMainTbl and
// Join related table. '
$query = "SELECT * FROM `UnitMaintbl`
INNER JOIN `Statustbl` ON UnitMaintbl.StatusID=Statustbl.StatusDesc
WHERE Unitmaintbl.Unit = '%{$sunit}%'
ORDER BY Recnum DESC";
// Check if there are results
if ($result = mysqli_query($con, $sql))
{
// If so, then create a results array and a temporary one
// to hold the data
$resultArray = array();
$tempArray = array();
// Loop through each row in the result set
while($row = $result->fetch_object())
{
// Add each row into our results array
$tempArray = $row;
array_push($resultArray, $tempArray);
}
// Finally, encode the array to JSON and output the results
echo json_encode($resultArray);
}
// Close connections
mysqli_close($result);
mysqli_close($con);
?>
Ok final code and it works with my first few test anyway haha.. woohoo.. thanks guys for all your help.
<?php
// Variables From Search
$sunit=$_GET['Unit']; // Unit Number
//open connection to mysql db
$connection = mysqli_connect("localhost","user","pass","PicorpBE") or die("Error " . mysqli_error($connection));
//fetch table rows from mysql db
$sql = "SELECT * FROM `UnitMaintbl`
INNER JOIN `Statustbl` ON UnitMaintbl.StatusID=Statustbl.StatusDesc
WHERE UnitMaintbl.Unit LIKE '%{$sunit}%'
ORDER BY Recnum DESC";
$result = mysqli_query($connection, $sql) or die("Error in Selecting " . mysqli_error($connection));
//create an array
$emparray = array();
while($row =mysqli_fetch_assoc($result))
{
$emparray[] = $row;
}
echo json_encode($emparray);
//close the db connection
mysqli_close($connection);
?>
Upvotes: 2
Views: 175
Reputation: 16963
Your query should be like this:
$query = "SELECT * FROM `UnitMaintbl`
INNER JOIN `Statustbl` ON UnitMaintbl.StatusID=Statustbl.StatusDesc
WHERE Unitmaintbl.Unit LIKE '%{$value}%'
ORDER BY Recnum DESC";
Instead of =
use LIKE
to search for a specified pattern in a column.
Sidenote: Please don't use mysql_
database extensions, they were deprecated in PHP 5.5.0 and were removed in PHP 7.0.0. Use mysqli
or PDO
extensions instead. And this is why you shouldn't use mysql_ functions.
Upvotes: 1