Jump to content

[SOLVED] Having a few SELECT issues...


RP

Recommended Posts

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.

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.