Jump to content

[SOLVED] How would I do this?


Sobbs

Recommended Posts

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.