MySQL Database Query problem
I have a MySQL Database on my website that lists engine parts and am currently working on PHP code to dynamically make a table from Query results.
My code for creating the tables for example are as follows:
<?php
mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("djvkcom_ysp") or die(mysql_error());
// Get specific items using query from the "ysp" table and stores them in result variable
$result = mysql_query("SELECT * FROM ysp
WHERE Heading='Anodes' AND `Description` LIKE '%2QM15%'") or die(mysql_error());
// Sort the results into rows
$row= mysql_fetch_array( $result );
// Display the contents of the entry
echo "<table border='1' style='background-color: #ffffff; text-align:center;'>";
echo "<tr> <th>Image</th> <th>Item</th> <th>Engine Models</th> <th>Part No.</th> <th>Price</th> </tr>";
// keeps getting the next row until there are no more to get
while($row= mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo "<tr><td>";
echo "<img src=\"".$row['Image']."\">";
echo "</td><td>";
echo $row['SubHeading'];
echo "</td><td>";
echo $row['Description'];
echo "</td><td>";
echo $row['PartNo'];
echo "</td><td>";
echo $row['Price'];
echo "</td></tr>";
}
echo "</table>";
?>
The above code looks for parts with the heading 'Anodes' that contain the engine model '2QM15' in the description and then displays the results in a tabular format. When running the same query in phpMyAdmin's Search or Query tabs on the database this returns 2 results from my database. However when the above code is used on my website it only returns 1 result.
I know the code is not at fault as it is only having this problem on that specific query. I have other pages with the same format of code but a different query showing multiple results. This makes me think it has something to do with the way the that particular description field is written. But I am confused as to why the Query works fine in phpMyAdmin but not when it is put on the webpage with the above code.
MySQL Query Results on webpage differ to results in phpMyAdmin
ok so try as I might I am still getting a problem with my MySQL Queries not working as they should.
A query from within phpMyAdmin for an entry in my database with the heading of 'Gaskets' and the keyword '2YM15' (an engine model) in description shows one result. And from phpMyAdmin I can generate the PHP code for that query to preform it on my website.
The code it gives me is as follows:
Code:
"SELECT * FROM `ysp` WHERE `Heading` = \'Gaskets\' AND `Description` LIKE \'%2YM15%\' LIMIT 0, 30 ";
Now I tried to use that code on my page but it had a problem with the backslashes (syntax error) but once I removed them it no longer had any errors. And I removed the LIMIT section at the end as I don't want to limit the results at all.
The end result is something like this:
Code:
$result = mysql_query("SELECT * FROM `ysp` WHERE `Heading` = 'Gaskets' AND `Description` LIKE '%2YM15%' "
The problem now is it returns no results at all, instead of the one result it does in phpMyAdmin.
I can not understand why this is happening.
(On further investigation it seems what is happening is it is dropping 1 result from each query, if a query returns 2 results in phpMyAdmin then my code will only return 1 of them.
And likewise if a query returns 1 result in phpMyAdmin then my code will return 0 results. I still can not explain why this is happening)
The PHP code I am using to preform this query is freely available here and so if looking at that code helps you understand better then I hope you can help me. I have tried everything I can think of and I could really use the help, Thanks.