Page 1 of 1

Using db query in print

Posted: Wed Jul 11, 2007 10:36 am
by mademokid
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

Posted: Wed Jul 11, 2007 11:03 am
by Gente
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?

Posted: Wed Jul 11, 2007 11:14 am
by mademokid
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

Posted: Wed Jul 11, 2007 11:52 am
by mademokid
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);