Glyph Posted February 11, 2006 Share Posted February 11, 2006 I am trying to tweak the sample code presented on [a href=\"http://www.tizag.com/mysqlTutorial/mysqlwhere.php\" target=\"_blank\"]this page[/a].[b]<?php// Make a MySQL Connectionmysql_connect("localhost", "admin", "1admin") or die(mysql_error());mysql_select_db("test") or die(mysql_error());// Get a specific result from the "example" table$result = mysql_query("SELECT * FROM exampleWHERE name='Sandy Smith'") or die(mysql_error());// get the first (and hopefully only) entry from the result$row = mysql_fetch_array( $result ); // Print out the contents of each row into a tableecho $row['name']." - ".$row['age'];?>[/b]In particular, I want to specify a list of records to obtain from the database, rather than just one. The contents of the list needs to be explicitly specified, as does the order, since it is based on something that simply can't be programmed to order them automatically.Using the above example, instead of just displaying the record for Sandy Smith, I would like to display the records for Bobby Wallace, Tim Mellowman, and Sandy Smith, in that order. How would I rewrite the above in order to produce this? Quote Link to comment https://forums.phpfreaks.com/topic/3391-using-where-with-a-list/ Share on other sites More sharing options...
fenway Posted February 11, 2006 Share Posted February 11, 2006 Well, you can definitely use a WHERE name IN ('name1','name2','name3') to get all three; to get them in a particular order is trickier. You could use ORDER BY FIELD(), but in this case, that's not ideal -- I would suggest dealing with the ordering in PHP with a lookup hash. Quote Link to comment https://forums.phpfreaks.com/topic/3391-using-where-with-a-list/#findComment-11595 Share on other sites More sharing options...
Glyph Posted February 11, 2006 Author Share Posted February 11, 2006 Thanks, but what's a lookup hash? Quote Link to comment https://forums.phpfreaks.com/topic/3391-using-where-with-a-list/#findComment-11596 Share on other sites More sharing options...
Glyph Posted February 11, 2006 Author Share Posted February 11, 2006 Also, how do I generate the output. I've tried your suggestion but it only prints the first item in the list. I presume it uses implode, but what would the precise syntax be? Quote Link to comment https://forums.phpfreaks.com/topic/3391-using-where-with-a-list/#findComment-11597 Share on other sites More sharing options...
fenway Posted February 12, 2006 Share Posted February 12, 2006 Basically, just associate the name with the order, and then you can sort them yourself in PHP. And to get more than one result, you'll need a while loop. Quote Link to comment https://forums.phpfreaks.com/topic/3391-using-where-with-a-list/#findComment-11610 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.