2

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);
?>
Rich Rich
  • 35
  • 6
  • `"` after `'%{$value}%'` is not required. – Jigar Dec 23 '15 at 18:51
  • I tried take that off and I get: Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/picorp05/public_html/json.php on line 43 [] – Rich Rich Dec 23 '15 at 18:57
  • Please, [edit](http://stackoverflow.com/posts/34441939/edit) your question and provide [relevant](http://stackoverflow.com/help/mcve) code so we can help you. This error isn't related to the code you provided. – FirstOne Dec 23 '15 at 19:00
  • thanks all... I updated with my php in full above – Rich Rich Dec 23 '15 at 19:03
  • What error are you getting now? – Rajdeep Paul Dec 23 '15 at 19:06
  • Im getting Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/picorp05/public_html/json.php on line 43 [] with the above php file – Rich Rich Dec 23 '15 at 19:09
  • Please, [don't use mysql_* functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php), they are deprecated. You should also take a look at [How can I prevent SQL-injection in PHP?](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – FirstOne Dec 23 '15 at 19:09
  • @RichRich See my answer. – Rajdeep Paul Dec 23 '15 at 19:11
  • Two problems... one is your join conditions correct UnitMaintbl.StatusID=Statustbl.StatusDesc statusid of unitmaintbl is equal to statusdesc ??? Id and desc??? And second one wrong way of using string no need to use % sign – devpro Dec 23 '15 at 20:03

1 Answers1

1

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.

Community
  • 1
  • 1
Rajdeep Paul
  • 16,887
  • 3
  • 18
  • 37
  • You should explain the difference between the OP's code and yours. Also, that late sidenote :p. – FirstOne Dec 23 '15 at 19:15
  • @FirstOne I'm sure the query is pretty self explanatory. – Rajdeep Paul Dec 23 '15 at 19:16
  • I know, but what I meant is what you've changed. (The `like` part). You could add some text explaining or at least saying that you changed from `=` to `like`. – FirstOne Dec 23 '15 at 19:17
  • I see the difference.. LIKE but I still get the same error. Side note taken, Ill rewrite once I figure out how to get this working..haha. Appreciate the help guys. – Rich Rich Dec 23 '15 at 19:20
  • @RichRich First check whether it returns any result set or not. Use `mysql_num_rows()` function for that. – Rajdeep Paul Dec 23 '15 at 19:37
  • @RajdeepPaul Ive updated code from another example I found. using mysqli_ still seems this query is to blame. – Rich Rich Dec 23 '15 at 20:30
  • @RichRich It should be `if ($result = mysqli_query($con, $query)){ ... }`. And don't mix the procedural and object oriented style of `mysqli`. – Rajdeep Paul Dec 23 '15 at 20:33
  • working on it.. now I'm getting mysqli_close() expects parameter 1 to be mysqli, boolean given in /home/picorp05/public_html/json.php on line 45 sigh... – Rich Rich Dec 23 '15 at 20:42
  • Thanks for getting me in the right direction @RajdeepPaul new working code above. – Rich Rich Dec 23 '15 at 21:01