Sobbs Posted March 21, 2009 Share Posted March 21, 2009 Im trying to connect to a database table and retrieve about 10 rows of data and turn those into variables to be viwed on a site. Ive attempted this but cannot figure out how I'd manage to do this. It would be somewhat like how a forum would display database info. Could someone help? Quote Link to comment Share on other sites More sharing options...
irkevin Posted March 21, 2009 Share Posted March 21, 2009 did you try something? you can use LIMIT in your query to get a certain number of row.. post the code you have... Quote Link to comment Share on other sites More sharing options...
Mikedean Posted March 21, 2009 Share Posted March 21, 2009 You'll need to learn the following mysql commands. mysql_connect(); mysql_select_db(); mysql_query(); mysql_fetch_assoc(); (there are multiple choices in for this command. E.g. mysql_fetch_row(), mysql_fetch_arrray() - They all do pretty much the same job though.) <?php $databaseConnection = mysql_connect( SERVER, USER, PASS ) or die( "Could not connect"); mysql_select_db( DATABASE, $databaseConnection ) or die( "Could not select the database" ); $queryResults = NULL; $theSQL= "SELECT * FROM <table>"; // * should never really be used, however as I do not know what columns you have I did it for simplicity. $theQuery = mysql_query( $theSQL, $databaseConnection ) or die( mysql_error() ); while( $resultRow = mysql_fetch_assoc( $theQuery ) ) { $queryResults[ count( $queryResults) ] = $resultRow; // place the contents of the row inside our results variable. Incrementing its key by 1. } var_dump( $queryResults ); ?> Read up on these commands and others at http://uk2.php.net/manual/en/book.mysql.php. Oops, and like Kevin said, use LIMIT in your query! Hope this helps! Quote Link to comment Share on other sites More sharing options...
Sobbs Posted March 21, 2009 Author Share Posted March 21, 2009 Thank you that should help. Quote Link to comment Share on other sites More sharing options...
Sobbs Posted March 21, 2009 Author Share Posted March 21, 2009 How would I get a single variable from that though? Like lets say I want to get info from a single row, such as a username. Quote Link to comment Share on other sites More sharing options...
trq Posted March 21, 2009 Share Posted March 21, 2009 Have you tried? There are many exampels of this on the net, were not here to write more tutorials. Quote Link to comment Share on other sites More sharing options...
Sobbs Posted March 21, 2009 Author Share Posted March 21, 2009 Ive tried to find a tutorial on this, but non worked the way i needed it to. Quote Link to comment Share on other sites More sharing options...
Sobbs Posted March 21, 2009 Author Share Posted March 21, 2009 Never mind, i figure this out by trial and error. Quote Link to comment Share on other sites More sharing options...
trq Posted March 21, 2009 Share Posted March 21, 2009 Ive tried to find a tutorial on this, but non worked the way i needed it to. Probably because you've skipped learning the basics and hence cannot adapt examples to your needs. There's a basic database handling tutorial on our main site. I suggest you read it and go from there. Quote Link to comment 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.