SQL query not working

I have a sql query that works when I test directly on the database but fails when placed into my php file. I’m not exactly sure what the issue is, I’m getting a "no data received error’ from chrome. It’s a simple search fileld that is searching through 2 columns in a table.

Here is my php:
[php]if (get_magic_quotes_gpc()) {
$searchterm = stripslashes($searchterm);
}
$searchterm = mysql_real_escape_string($searchterm);
$sql = “SELECT dropbox.id, rma.rma_number, rma.serial_number, status.status_name, ref_categories.category_name, dropbox.mimetype, dropbox.filedata, dropbox.filename, dropbox.date, dropbox.description
FROM dropbox
INNER JOIN rma on rma.id = dropbox.ref_rma_id
INNER JOIN status on status.id = dropbox.ref_status_id
INNER JOIN ref_categories on ref_categories.id = rma.ref_categories_id
WHERE rma.rma_number LIKE ‘%$searchterm%’ OR rma.serial_number LIKE ‘%$searchterm%’ AND rma.ref_categories_id=3
ORDER BY status.id”;
$result = $conn->query($sql) or die(mysqli_error());
[/php]

How does the sql look if you var_dump it out from PHP? Also you should be using prepared statements instead of inserting variables directly into the query.

When I do a var_dump I get - string(9) “LKT999000”

with LKT999000 being what I entered into the search field.

I was thinking about the complete sql query generated, if you dump it out (with the search value) and run it in sql yourself / phpmyadmin do you get a result?

Sorry I misunderstood. Yes, when I run the query in phpmyadmin with the search value inserted into the statement I get the result I’m looking for.

that’s strange.

do you have anything in the $result variable after the query in php?

I think I just may have found the issue. When I click the button to search, chrome is giving me a “No Data Received” error. So I’m assuming that perhaps there is a problem with my database connection.

I’m trying to move my connection strings to an include file and use the following code to connect to my database:

[php]require_once(‘includes/connection.php’);
$conn = dbConnect(‘write’);[/php]

Should I change that to “include” or “require”?

When I use the traditional connection method it works:

[php]$dbh=mysql_connect (“localhost”, “username”, “password”) or die ('I cannot connect to the database because: ’ . mysql_error());
mysql_select_db (“database”);[/php]

You seem to be mixing mysql with mysqli, and procedural and object oriented coding. Which is it you are (wanting to be) using?
oop mysqli?

Thanks for bearing with me…

This is a rather old application that I built 8 or 9 years ago. At this point I’m not really sure which approach is better.

I’d like to use mysqli but I’m not sure if it’s better to code in procedural or object oriented style. Everybody seems to be moving to object oriented.

Ah, so it’s from when mysqli/pdo was new ^^

I would definitly recommend converting to PDO or mysqli OOP, mysql_* is deprecated and will be removed in a future version.

Is it a large application?

No, It’s not very large. There are many things that should now be done differently. I worked full time for the company when I originally built the application, kind of a side project that I built to solve an issue and become more organized. I’m now working on this project as a freelancer since I no longer work for them. So convincing the client that a working application needs to be rewritten is a challenge.

That is true, you could try to pitch it with the truth. Things have happened the last 8 years and web applications have evolved. They might need (now, soon or some time) to have the application rewritten in order to split it into an API and a web app, ie if they want to introduce a mobile app as well. This could be a step in that direction.

That is true, thanks for your help with all of this! Perhaps it is time for a redesign especially if they plan on expanding it to include more of their customers. If they were to approach me with that right now the application would be come a mess.

Thanks again!
-Brandon

Edit:: astonecipher
Create your own topic for questions. And when asking for help, ask a question not “check this”

Sponsor our Newsletter | Privacy Policy | Terms of Service