KBD

Keith Devens .com

Friday, September 5, 2008 Flag waving
"Why teach drawing to accountants? Because drawing class doesn't just teach people to draw. It teaches them to be... – Randy S. Nelson (dean of Pixar University)
← Awesome shirt(s)Kerry's underhanded campaign tactics and media complicity →

Daily link icon Tuesday, March 9, 2004

Boo MySQL

I'm normalizing some database tables more than I was hoping to get away with. I would have gotten away with it too, if it wasn't for you meddling kids... er, if I didn't need the ability to rename what I was planning to make a key.

Because MySQL doesn't support subqueries (if there was ever a case of worse-is-better, it's PHP and MySQL), the following very simple query:

UPDATE Bookmark_Keywords SET Bookmark_Keyword_Id = (
   SELECT Bookmark_Keyword_Id
   FROM Bookmark_Keyword_Info
   WHERE Bookmark_Keyword_Name = Bookmark_Keyword
)

is going to require that I write procedural code to update the table. Bust.

Update: For future reference, here's the code I had to write:

<?php
$sql 
'SELECT DISTINCT Bookmark_Keyword_Info.Bookmark_Keyword_Id id, Bookmark_Keyword_Name name
   FROM Bookmark_Keyword_Info
   INNER JOIN Bookmark_Keywords ON(Bookmark_Keyword_Name = Bookmark_Keyword)
   ORDER BY Bookmark_Keyword_Name'
//reformatted to fit layout
$rs_id mysql_query($sql$conn) or die("Couldn't select your keywords: ".mysql_error());
while(
$row mysql_fetch_assoc($rs_id)){
    
$sql "UPDATE Bookmark_Keywords
        SET Bookmark_Keyword_Id = $row[id]
        WHERE Bookmark_Keyword = '$row[name]'"
// reformatted to fit layout
    
print_r($row);
    echo 
'<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
    
print_r($sql);
    
mysql_query($sql$conn) or die("Couldn't update your table: ".mysql_error());
    echo 
'<br>';
}
?>

Any formatting in there (print_r, echo), etc., was for me to see that the right thing was going to happen before I uncommented the mysql_query line. And of course, this is just "one-off" code... for instance, I didn't addslashes() on $row[name] because I knew none of my names had slashes or quotes in them.

← Awesome shirt(s)Kerry's underhanded campaign tactics and media complicity →

Comments XML gif

65.111.194.250 wrote:

A discussion of php and worse is better: http://www.sitepoint.com/forums/showthread.php?t=155043

∴ 65.111.194.250 | 9-Mar-2004 2:39pm est | #4100

timbu wrote:

<p />Sometimes I do it this way instead.

<pre>
Pseudo-SQL
create tmp_table;
select whatever from real_table into tmp_table;
select * from tmp_table into real_table;
drop tmp_table;
</pre>

<p />But that has it's own awkwardnedd too.

∴ timbu | 11-Mar-2004 10:12pm est | #4107

Feel free to post a comment below. Please see my comment policy.

Formatting Rules (No HTML):

  • **bold**, *italic*, _underlined_, --strikeout--
  • "text"="url" creates a link, and URLs are auto-highlighted
  • Blockquote: Like e-mail, begin paragraph with > (greater-than sign)
  • Lists: begin paragraph with *,-, or + (unordered), or # (ordered)
  • Code block: ?!code:language=perl|php|sql|javascript|etc.{\n}...{\n}?!/code

:
(will be your IP address if blank)
: (optional)
(Will not be shown on site)

: (optional)
:

September 2008
SunMonTueWedThuFriSat
 123456
78910111213
14151617181920
21222324252627
282930 



RSS feed RSS feed for Keith's Weblog
Atom feed Atom feed for Keith's Weblog
Weblog archive
Recent comments
  on 5 posts

Recent comments XML

new⇒I hate ASP.NET

CF, why pick that piece of trash?​Cold Confusion. Is it finally​really a OO...

ColdConfusion: Sep 5, 8:36pm

new⇒Maps of Iraq

This is for Linda, I will be​visiting that site some time in the​near futur...

Bob: Sep 5, 1:20pm

Girls, please don't get breast implants

Well alright I just read my above​comment and I wanted to add​this...I shou...

76.66.140.8: Sep 4, 7:31pm

Spider solitaire

I don't think the question was​necessarily if there are unbeatable​games.  ...

Jared: Sep 4, 12:44pm

Convert Pantone Colors to RGB and Hex - Color Conversion Chart

The colors on those website don't​seem to relate to the pantone data​we hav...

blah: Sep 3, 10:12am

Generated in about 0.196s.

(Used 8 db queries)

mobile phone