Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.
Moderator: General Moderators
mademokid
Forum Commoner
Posts: 25 Joined: Tue Jul 10, 2007 11:10 am
Post
by mademokid » Wed Jul 11, 2007 10:36 am
I have executed the following:
Code: Select all
$id = mysql_query('SELECT id FROM ibf_members');
print "http://forum.example.com/index.php?showuser=$id"
How can I associate the id with the name of the user that is logged in.
The logged in user has a cookie with their username as ['ID_my_site']
The username has a corresponding ID in the db and i want the id to be a variable $id.
Could someone please change/rewrite the code so that the variable would work.
Thanks
Gente
Forum Contributor
Posts: 252 Joined: Wed Jun 13, 2007 9:43 am
Location: Ukraine, Kharkov
Contact:
Post
by Gente » Wed Jul 11, 2007 11:03 am
You have a lot of mistakes (syntax and logical). The main one:
Result of following query is array, not the single id
So what id do you need?
mademokid
Forum Commoner
Posts: 25 Joined: Tue Jul 10, 2007 11:10 am
Post
by mademokid » Wed Jul 11, 2007 11:14 am
I have:
Code: Select all
$result = mysql_query("SELECT id FROM ibf_members WHERE name = ".$_COOKIE['ID_my_site']."");
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
$row = mysql_fetch_row($result);
echo $row[0]
The id of the user is in the same row as their name so I want to associate the name with the id, but display the id
mademokid
Forum Commoner
Posts: 25 Joined: Tue Jul 10, 2007 11:10 am
Post
by mademokid » Wed Jul 11, 2007 11:52 am
Sorted it!
I used:
Code: Select all
$query = sprintf("SELECT id FROM ibf_members WHERE name='%s'",
mysql_real_escape_string($username));
$result = mysql_query($query);
if (!$result) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
while ($row = mysql_fetch_assoc($result)) {
echo 'http://forum.askscholey.com/index.php?showuser=',
$row['id'];
}
mysql_free_result($result);