Click to See Complete Forum and Search --> : Select Query


k0r54
12-14-2004, 06:20 PM
Hi,

I have a field in a table called PageCode and the data is entered like 1,3,66,88 ect.. (commas breaking them up)

Now each page on the site has a unique page number which is written into a variable.

How can i check to see if this variable $PageCode (the page number) is in the database.

Basicily i think i need to retrieve the value of $PageCode and then somehow find out one of the numbers between the commas is the $PageCode

Can this be done in a single query?

Whether it can or cant how would i go about doing it?

Thanks
k0r54

MstrBob
12-14-2004, 06:27 PM
<?PHP
$query=mysql_query("SELECT `PageCode` FROM `table_name` LIMIT 1");
$numbers=mysql_result($query, 0);
$numbers=explode(',', $numbers);
foreach($numbers as $value)
{
if($value == $pagecode)
{
echo('Number Found');
}
}
?>

k0r54
12-14-2004, 06:44 PM
Hi, thanks but this still doesn't seem to work, it just does nothing displays the page as normal


function Maintenance_Check($PageCode) {

//connect to db
$conn = mysql_connect("localhost","xxx","xxx");
mysql_select_db("xxx", $conn);

$query = mysql_query("SELECT PageCode FROM history LIMIT 1");
$numbers = mysql_result($query, 0);
$numbers = explode(',', $numbers);
foreach($numbers as $value)
{
if($value == $PageCode)
{
header("Location: www.msn.co.uk");
}
}

}


Thanks Adam

k0r54
12-15-2004, 11:53 AM
any idea on why this isn't working?

ShrineDesigns
12-15-2004, 12:24 PM
try this query:SELECT `PageCode` FROM `history` WHERE LOCATE('$PageCode', `PageCode`) != 0

k0r54
12-16-2004, 09:23 AM
Hi that works great apart from one thing?

If i have data of 1,2,4,35,8

Notice i do not have 3 on its own, only in 35.

This query still picks 35 if i query for 3 i need it to only pick it up if it is on its own.

Any ideas?

k0r54
12-16-2004, 09:44 AM
I have just figured out why the MstrBob code didn't work, it was because it can only do it on one row of the database, What do i do if there is more than one row


Thanks
Adam

k0r54
12-16-2004, 10:24 AM
Hi i have done it :)

Here is the code i used, anything i could improve or is it fine how it is.


//connect to db
$conn = mysql_connect("localhost","apcmaintenance","sepultura");
mysql_select_db("apcmaintenance", $conn);

$query = mysql_query("SELECT PageCode As PageCodeDB FROM history");

while ($row = mysql_fetch_array($query)) {
extract($row);
$numbers = $PageCodeDB;
$numbers = explode(',', $numbers);

foreach($numbers as $value) {
if($value == $PageCode) {
header("Location: http://www.msn.co.uk");
}
}
}


Thanks Adam