PHP link

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Code always helps.
mademokid
Forum Commoner
Posts: 25
Joined: Tue Jul 10, 2007 11:10 am

Post by mademokid »

Right. I have successfully generate the variable I need with this code:

Code: Select all

$query = sprintf("SELECT id FROM ibf_members WHERE name='%s'",
    mysql_real_escape_string($username));

// Perform Query
$result = mysql_query($query);

// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!$result) {
    $message  = 'Invalid query: ' . mysql_error() . "\n";
    $message .= 'Whole query: ' . $query;
    die($message);
}

// Use result
// Attempting to print $result won't allow access to information in the resource
// One of the mysql result functions must be used
// See also mysql_result(), mysql_fetch_array(), mysql_fetch_row(), etc.
while ($row = mysql_fetch_assoc($result)) {
    print $row['id'];
}

// Free the resources associated with the result set
// This is done automatically at the end of the script
mysql_free_result($result);
But I need to include the $row['id'] variable in a link.

The link should be in the following format:

http://forum.example.com/index.php?showuser=$row['id']

But I don't know how to write that in php.

I would also like 'View your forum CP' to be shown as the 'link'

I hope someone understands me, because even I'm not sure!
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Code: Select all

<?php
$query = "SELECT id FROM ibf_members WHERE name='" . mysql_real_escape_string($username) . "'";

// Perform Query & check result
if (!$result = mysql_query($query))
    // This shows the actual query sent to MySQL, and the error. Useful for debugging. 
    $message  = 'Invalid query: ' . mysql_error() . "\n"; 
    $message .= 'Whole query: ' . $query; 
    die($message); 
} 

// Use result 
$user = mysql_fetch_array($result);

// Make the link
$link = 'http://forum.example.com/index.php?showuser=' . $user['id'];

// Make the markup
$href = '<a href="' . $link . '">View your forum CP</a>';

// Free the resources associated with the result set 
// This is done automatically at the end of the script 
mysql_free_result($result);


// Now show the markup
echo $href;
?>
mademokid
Forum Commoner
Posts: 25
Joined: Tue Jul 10, 2007 11:10 am

Post by mademokid »

Had to change it a bit:

Code: Select all

<?php
		$query = sprintf("SELECT id FROM ibf_members WHERE name='%s'",
    mysql_real_escape_string($username));

// Perform Query
$result = mysql_query($query);

// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!$result) {
    $message  = 'Invalid query: ' . mysql_error() . "\n";
    $message .= 'Whole query: ' . $query;
    die($message);
}

// Use result
// Attempting to print $result won't allow access to information in the resource
// One of the mysql result functions must be used
// See also mysql_result(), mysql_fetch_array(), mysql_fetch_row(), etc.
while ($row = mysql_fetch_assoc($result)) 
$link = "http://forum.askscholey.com/index.php?showuser=".$row['id'];
$href = '<a href="' . $link . '">View your forum CP</a>';
echo $href; 

// Free the resources associated with the result set
// This is done automatically at the end of the script
mysql_free_result($result);
?>
But it works fine now

Thanks!
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Glad I could help.
Post Reply