Forum Moderators: coopster
I am having a bit of trouble here trying to go to a name in a Mysql database and retrieve its id, what i am actually trying to do is:
convert a url with for example dir/index.php?action=showcat&idcat=4
to
dir/tablename/ instead of dir/4/ (4 being a table id)
so what i need is to change the name in the url back to the id, i have tried the following without luck.
if ($catid){
$db=mysql_connect("localhost","user","password");
mysql_select_db("database",$db);
$query="SELECT 'id' FROM dir_categories WHERE name= '.$catid.' ";
$result=mysql_query($query);
$record=mysql_fetch_assoc($result);
$idcat=$record["id"];
}
i have setup the rewrite successfully in htaccess
(thanks to this website) to rewrite as "catid"
I am just trying to use the table names instead of id's for SE purposes, as /widgetgreen/ is better than /4/
Basically is what im trying possible? or am i wasting my time trying
I hope somebody understands what im on about as i seem to of confused myself.
Many Thanks
im very new to MySql and just try to pick up what i can.
as simple as i can:
i have a dir/index.php?action=showcat&idcat=NUMBER url
the number is the "id" in the database table, i have converted the url with rewrite to dir/NUMBER but each "catergory" has a name which i would like to use instead of the number i.e. widgets white has id 1, widgets green has id 2 and so on. so what im trying to do is call dir/index.php?action=showcat&idcat=NUMBER with dir/widgets_white/
i tried using "catid" in url which would find the name and return the "id" as "idcat" with
if ($catid){
$db=mysql_connect("localhost","user","password");
mysql_select_db("database",$db);
$query="SELECT 'id' FROM dir_categories WHERE name= '.$catid.' ";
$result=mysql_query($query);
$record=mysql_fetch_assoc($result);
$idcat=$record["id"];
}
oh dear i think ive confused myself again :(
Thank you very much
if ($catid){
$db=mysql_connect("localhost","user","password");
mysql_select_db("database",$db);
$query="SELECT 'id' FROM dir_categories WHERE name= '.$catid.' ";
echo $query;
die();
$result=mysql_query($query);
$record=mysql_fetch_assoc($result);
$idcat=$record["id"];
}
that will show your actual query, something like
SELECT 'id' FROM dir_categories WHERE name= 'widgetgreen'
which, if run, should return 4
if you get this
SELECT 'id' FROM dir_categories WHERE name= ''
then the value is not getting assigned properly and will help us start narrowing down the issue.
Aslo, are you getting any errors?
i have just been playing with PHPMyAdmin, i dont know if thats right but i ran the command created with PHPMyAdmin:
SELECT id FROM `dir_categories` WHERE name = 'widgetgreen'
and it showed 4
i chose "create php" this is what i got
$sql = 'SELECT id';
$sql .= 'FROM `dir_categories` ';
$sql .= 'WHERE name = \'widgetgreen\' LIMIT 0, 30';
and executed it showed 4 again
probably means nothing but im learning
Now onto the next step, let's see if mysql will throw an error.
if ($catid){
$db=mysql_connect("localhost","user","password");
mysql_select_db("database",$db);
$query="SELECT 'id' FROM dir_categories WHERE name= '.$catid.' ";
echo '<p>',$query;
//die();
$result=mysql_query($query) or die(mysql_error());
$record=mysql_fetch_assoc($result);
echo "<pre>";
print_r($record);
echo "</pre>";
$idcat=$record["id"];
echo '<p>',$idcat;
}
so we have now added a couple more echo's to see what is happening with all of our variables. Let's see what happens with that.
if ($catid){
$db=mysql_connect("localhost","user","password");
mysql_select_db("database",$db);
$query="SELECT 'id' FROM dir_categories WHERE name= '$catid'";
echo '<p>',$query;
//die();
$result=mysql_query($query) or die(mysql_error());
$record=mysql_fetch_assoc($result);
echo "<pre>";
print_r($record);
echo "</pre>";
$idcat=$record["id"];
echo '<p>',$idcat;
}
if ($catid){
$db=mysql_connect("localhost","user","password");
mysql_select_db("database",$db);
$query="SELECT id FROM dir_categories WHERE name= '$catid'";
echo '<p>',$query;
$result=mysql_query($query) or die(mysql_error());
while ($record=mysql_fetch_assoc($result)) {
echo "<pre>";
print_r($record);
echo "</pre>";
$idcat=$record["id"];
echo '<p>',$idcat;
}
}
if ($catid){
$db=mysql_connect("localhost","user","password");
mysql_select_db("database",$db);
$query="SELECT id FROM dir_categories WHERE name= '$catid'";
//echo '<p>',$query;
$result=mysql_query($query) or die(mysql_error());
while ($record=mysql_fetch_assoc($result)) {
//echo "<pre>";
//print_r($record);
//echo "</pre>";
$idcat=$record["id"];
//echo '<p>',$idcat;
}
}
but widgetgreen is also "short_name"
and large is also "short_name"
widgetgreen is a catergory
large is a sub-catergory of widgetgreen
Thanks for all your help, ill check back later dont have much time now.