Mysql + cookie problem

Hello, im trying to find out how i can select one specific row in my database to show just that line in php, Its hard to explain but ill show.

[php]<?php

$con = mysql_connect("######", “######”, “######”);
if (!$con)
{
die('Could not connect: ’ . mysql_error());
}

mysql_select_db(“lanarp_com”, $con);

$result = mysql_query(“SELECT * FROM products”);

while($row = mysql_fetch_array($result))
{
echo $row[‘costumerid’] . " " . $row[’.$_COOKIE[ID_my_site];’];
echo “
”;
}

mysql_close($con);

?>[/php]
this function is for my website where ull have to login to the site and when u logg in you also create a cookie, i want that cookie to select a specific row in my database… is that possible?
as in the code over here, it will show “costumerid” from the database, thats simple, but i also want to select the specific row with the help of that cookie.

/thanks

If you store in a cookie some identifier for logged in user (say ‘username’), then your query will look like this:
[php]$result = mysql_query(“SELECT * FROM products WHERE username=’”.mysql_real_escape_string($_COOKIE[‘ID_my_site’])."’");
[/php]

Hm…
I dont realy get how i shud get that code to work.
In this case my cookie name is “ID_my_site” and that one contains information that sais “ompaa”, and in the database i got a row in “products” that contains a column with the name “costumerid” and in that row the name “ompaa” is shown since i saved information about a product. so i just want to show all the rows that have a costumerid with ompaa…

Hard to explain… sry!

If so, you just need to use field name ‘costumerid’ in your query:
[php]$result = mysql_query(“SELECT * FROM products WHERE costumerid=’”.mysql_real_escape_string($_COOKIE[‘ID_my_site’])."’");[/php]

this query will return all records for costumerid = saved cookie value.

shud the new code look something like this then?

[php]<?php

$con = mysql_connect(“localhost”, “username”, “password”);
if (!$con)
{
die('Could not connect: ’ . mysql_error());
}

mysql_select_db(“lanarp_com”, $con);

$result = mysql_query(“SELECT * FROM products WHERE costumerid=’”.mysql_real_escape_string($_COOKIE[‘ID_my_site’])."’");

echo "$result";

?>[/php]

Im sry for not realy understanding…
When i tryed this code i got some results saying “Resource id #4”?

ompaa, you must be kidding… See the code what you posted in your very first post. Replace the line in your code where you query table, with the line what I posted.
To be more clear, replace this line in your code:
[php]$result = mysql_query(“SELECT * FROM products”);[/php]

with this line:
[php]$result = mysql_query(“SELECT * FROM products WHERE costumerid=’”.mysql_real_escape_string($_COOKIE[‘ID_my_site’])."’");[/php]

Make sense?

Sponsor our Newsletter | Privacy Policy | Terms of Service