There are several methods of connecting to a database, here’s one example:
// Set the database access information as constraints
DEFINE ('DB_USER', 'userName);
DEFINE ('DB_PASSWORD', 'passWord');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'databaseName');
Use DEFINE to set your login details, then:
// Make the connection
$dbc = @mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) OR die ('Error: Check UN, PW, and host ' . mysql_error());
Connect using mysql_connect, then:
// Select the database
@mysql_select_db (DB_NAME) OR die ('Could not connect to database: ' . mysql_error() );
Select the batabase to use…
Once that’s done, you can write SQL queries like:
$query = "SELECT * FROM tablename WHERE conditions";
$result = mysql_query($query);
if(mysql_num_rows($result) != 0) { // if results are found
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$var1 = $row['col1'];
$var2 = $row['col2'];
$var3 = $row['col3'];
// etc...
} // end while
} // end if
Main Category
Secondary Categories