homepage Welcome to WebmasterWorld Guest from 54.166.113.249
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / Databases
Forum Library, Charter, Moderator: open

Databases Forum

    
Randomly sort and return 3 values from single user
Randomly Sort
JerryD



 
Msg#: 4602023 posted 2:41 am on Aug 15, 2013 (gmt 0)

I have a MySql DataBase with 18 categories for each ID. I want to to be able to fetch all IDs from that database based on the information contained in a single field ( for example where continent = Africa) then randomly choose from among those IDs that are returned from that query, then store the information from several fields in the database for that single ID.
I want to insert the information stored from the various fields for that single ID into several places on a web page using PHP. I want to insert some of the information stored into the HEAD portion of the page in 1 or more of the META TAG fields, & I want to insert some of the information stored from the Database query down the BODY section of the page.
I am new to MySQL and not an expert in PHP by any measure, so please understand if I have posted something in error.

Here is the code I am trying to run but it returns an error message.

<?PHP require_once ('../mysql_connect.php');
$data = ("SELECT embed1, length, email, header FROM redirect WHERE continent = 'Africa'");
ORDER_BY RAND();
LIMIT '1';

 

Dijkgraaf

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 4602023 posted 9:21 pm on Aug 15, 2013 (gmt 0)

Why do you have an underscore in the ORDER_BY?
There should be no underscore.
Also you need to have that inside the query

$data = ("SELECT embed1, length, email, header FROM redirect WHERE continent = 'Africa' ORDER BY RAND(); ");

whoisgregg

WebmasterWorld Senior Member whoisgregg us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4602023 posted 9:23 pm on Aug 15, 2013 (gmt 0)

Welcome to WebmasterWorld, JerryD!

Assuming you are connecting to mysql using the standard functions, your PHP code should look more like this:

// Formulate Query
$query = "SELECT embed1, length, email, header FROM redirect WHERE continent = 'Africa' ORDER BY RAND() LIMIT 1";

// 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
while ($row = mysql_fetch_assoc($result)) {
echo $row['embed1'];
echo $row['length'];
echo $row['email'];
echo $row['header'];
}


This sample comes from the PHP.net page for the mysql_query() [php.net] function. It's a good place to start to learn how to query a database. :)

To echo the values in other places in the HTML, just store the values into a variable during the while loop and echo them out where convenient.

JerryD



 
Msg#: 4602023 posted 6:59 am on Aug 16, 2013 (gmt 0)

Thanks a load guys. That got me past the issue I was having and I was able to finish it from there. You guys are the best and I apologize for my relative inexperience in using PHP to access MySQL.
Thanks Again !

whoisgregg

WebmasterWorld Senior Member whoisgregg us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4602023 posted 10:12 pm on Aug 20, 2013 (gmt 0)

Happy to help, JerryD. We were all inexperienced at some point. ;)

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Databases
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved