RP Posted April 13, 2007 Share Posted April 13, 2007 Im having a little trouble selecting from my tables while im trying to make some dynamically generated content; This code works a treat; <?php $host = ""; $user = ""; $pass = ""; $dbname = ""; if (!$db = mysql_connect($host, $user, $pass)) { echo 'Could not connect to mysql'; exit; } if (!mysql_select_db($dbname, $db)) { echo 'Could not select database'; exit; } // Date in the past header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // always modified header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // HTTP/1.1 header("Cache-Control: no-store, no-cache, must-revalidate"); header("Cache-Control: post-check=0, pre-check=0", false); // HTTP/1.0 header("Pragma: no-cache"); //XML Header header("content-type:text/xml"); $query = "SELECT * FROM locations"; $query = mysql_query($query); echo "<locations>"; while ($row=mysql_fetch_assoc($query)){ echo '<location id="'.$row['location_id'].'" station="'.$row['station'].'" lat="'.$row['lat'].'" lng="'.$row['lng'].'" />'; } echo "</locations>"; ?> but its not what i need, i need to be able to select according to a variable passed from a form ($code), so im trying this; <?php $code = $_POST['id']; $code = strtoupper($code); $host = ""; $user = ""; $pass = ""; $dbname = ""; if (!$db = mysql_connect($host, $user, $pass)) { echo 'Could not connect to mysql'; exit; } if (!mysql_select_db($dbname, $db)) { echo 'Could not select database'; exit; } // Date in the past header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // always modified header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // HTTP/1.1 header("Cache-Control: no-store, no-cache, must-revalidate"); header("Cache-Control: post-check=0, pre-check=0", false); // HTTP/1.0 header("Pragma: no-cache"); //XML Header header("content-type:text/xml"); $query = "SELECT * FROM locations WHERE id1 = '$code' || id2 = '$code' || id3 = '$code' || id4 = '$code' || id5 = '$code'"; $query = mysql_query($query); echo "<locations>"; while ($row=mysql_fetch_assoc($query)){ echo '<location id="'.$row['location_id'].'" station="'.$row['station'].'" lat="'.$row['lat'].'" lng="'.$row['lng'].'" />'; } echo "</locations>"; ?> any my output is consistently <locations></locations>, despite the $code variable being in the id1 column. Any ideas as to what im doing wrong would be greatly appreciated. Cheers. Quote Link to comment https://forums.phpfreaks.com/topic/46810-solved-having-a-few-select-issues/ Share on other sites More sharing options...
artacus Posted April 13, 2007 Share Posted April 13, 2007 Always a good idea to echo out your query when its not performing like you think it should. Then paste it into phpmyadmin or something and see what it does. I'm going to go out on a limb here and guess that your database is not normalized. I can't think of a case where you'd have a query like that on a normalized database. I'd recommend reading a tutorial or two on normalization/ db design before you go too much further. Another problem is that you are going to search through 5 fields in every row of your table. So to get any kind of performance, you will have to index all 5 fields. If you still insist on going this route, this query will work: SELECT * FROM locations WHERE '$code' IN (id1, id2, id3, id4, id5) Quote Link to comment https://forums.phpfreaks.com/topic/46810-solved-having-a-few-select-issues/#findComment-228175 Share on other sites More sharing options...
bubblegum.anarchy Posted April 13, 2007 Share Posted April 13, 2007 ... also, unlikely, but make sure you are not comparing against case sensative fields that are in lower case and make sure that the value held in $code is what you are expecting. Quote Link to comment https://forums.phpfreaks.com/topic/46810-solved-having-a-few-select-issues/#findComment-228186 Share on other sites More sharing options...
RP Posted April 16, 2007 Author Share Posted April 16, 2007 cheers guys, worked a treat. many thanks. Quote Link to comment https://forums.phpfreaks.com/topic/46810-solved-having-a-few-select-issues/#findComment-230044 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.