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);
?>
9
  • " after '%{$value}%' is not required. Commented Dec 23, 2015 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 [] Commented Dec 23, 2015 at 18:57
  • Please, edit your question and provide relevant code so we can help you. This error isn't related to the code you provided. Commented Dec 23, 2015 at 19:00
  • thanks all... I updated with my php in full above Commented Dec 23, 2015 at 19:03
  • What error are you getting now? Commented Dec 23, 2015 at 19:06

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

9 Comments

You should explain the difference between the OP's code and yours. Also, that late sidenote :p.
@FirstOne I'm sure the query is pretty self explanatory.
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.
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.
@RichRich First check whether it returns any result set or not. Use mysql_num_rows() function for that.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.