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? Link to comment https://forums.phpfreaks.com/topic/150502-solved-how-would-i-do-this/ 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... Link to comment https://forums.phpfreaks.com/topic/150502-solved-how-would-i-do-this/#findComment-790491 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! Link to comment https://forums.phpfreaks.com/topic/150502-solved-how-would-i-do-this/#findComment-790498 Share on other sites More sharing options...
Sobbs Posted March 21, 2009 Author Share Posted March 21, 2009 Thank you that should help. Link to comment https://forums.phpfreaks.com/topic/150502-solved-how-would-i-do-this/#findComment-790523 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. Link to comment https://forums.phpfreaks.com/topic/150502-solved-how-would-i-do-this/#findComment-790536 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. Link to comment https://forums.phpfreaks.com/topic/150502-solved-how-would-i-do-this/#findComment-790538 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. Link to comment https://forums.phpfreaks.com/topic/150502-solved-how-would-i-do-this/#findComment-790541 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. Link to comment https://forums.phpfreaks.com/topic/150502-solved-how-would-i-do-this/#findComment-790548 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. Link to comment https://forums.phpfreaks.com/topic/150502-solved-how-would-i-do-this/#findComment-790549 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.