Hi there, Im trying to do a basic script which allows the user to view details by Letters, I have a feeling Im doing it completely wrong (and it really wouldnt suprise me). So far Im just trying to get it to show the A's, so the script is no where near complete, can anyone help? <p> <p> <center> <?php include 'config.php'; $alphabet = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'); foreach ($alphabet as $letter) { echo "<a href=\"?letter=" . $letter . "\">" . $letter . "</a> ¦ "; } ?> <td width="173"> </td> </tr> <tr> <td colspan="3"><?php $rowsPerPage = 20; $product = $_GET['prodcut']; $query = "SELECT * FROM 'productinfo' WHERE 'product' LIKE "$letter" ORDER BY 'product' DEC"; echo $query; $result = mysql_query($query) or die('Error, query failed'); while(list($product) = mysql_fetch_array($result)) { echo "$product <br>"; } ?> PHP: It has connected to the database, I know that much lol. Sorry Im a complete n00b when it comes to things like this. Any help would be welcome feel free to boo me when reading Thanks
What's wrong with the script, what error messages are you getting? The only thing that I see immediatly is that in your SQL you have ORDER BY dec? Shouldn't that be desc... I'm no expert though... I didn't test the script, however I have done something similar in the past the same way that you are attempting to.
I have tried it with $query = "SELECT product FROM 'productinfo' WHERE SUBSTR(product FROM 1 FOR 1 ) = $letter" PHP: but it keeps coming up with - Error, query failed with echo $query it comes up with Zfailed
can you post the error message? you lack % in like $query = "SELECT * FROM 'productinfo' WHERE 'product' LIKE %".$letter."% ORDER BY 'product' DESC"; what are the other problems?
1) Don't use single quotes around table and column names. You need them strange 'back' quotes for that, or nothing. 2) Use single quotes around the value you're confronting the column product with (because product is alfanumeric). 3) Use % after the string you're searching with, otherwise the LIKE will act like an = (equal): - product LIKE 'A' will find only those products that are 'A' - product LIKE 'A%' will find all products starting with 'A' - product like '%A%' will find all products that contain an 'A' somewhere 4) Use the 'or die' construction to display the mysql error code and error message in case of a mysql error. Final result: $query = "SELECT product FROM productinfo WHERE product LIKE '$letter%'" or die("MySQL error - ".mysql_errno()." - ".mysql_error()); PHP: