MySQL Tips & Tricks

Monday, November 24, 2008

PHP Primitive to Create an HTML Form Dropdown from a MySQL table

$options_html=fn_make_form_selection_element($selected);

function fn_make_form_selection_element($selected_value)
{
$select_html="";
$query = "select * from lookup_offices order by office_id";
$result = mysql_query($query) or die ("query not valid".mysql_error());

while($row = mysql_fetch_assoc($result))
{
$db_value=$row['office_id'];
$db_name=$row['office_real_name'];

$option_selected='';
if ($db_value==$selected_value) $option_selected='selected';
$select_html.=<<<_STR_
< option value="$db_value" $option_selected>$db_name< /option>\n
_STR_;
}
return ($select_html);
}

Labels: , , ,

Tuesday, November 18, 2008

MySQL to Create an HTML Hyperlink from an URL

SELECT url,href FROM tbl_password where private = '1' and url NOT like '%squid%';

update tbl_password
set href=concat('< a href="',url,'">',url,'< /a>')
where private = '1' and url NOT like '%squid%';

I really wanted to strip the http:// off the anchor text as well

update tbl_password
set href=concat('< a href="',url,'">',SUBSTRING(url,7),'< /a>')
where private = '1' and url NOT like '%squid%' ;

Note here how flexible concat is with multiple parameters

Labels: ,