PHP/MySQL connection snippet

I snarfed this from ScriptyGoddess because I thought it was a good idea to capture code snippets somewhere on this site too. This is her PHP/MySQL snippet. I have a hard time remembering this too.

$databaseName = "YOURDATABASENAME";
$dbconnection = mysql_connect("localhost", "DATABASE-USERNAME", "DATABASE-PASSWORD" or
die ('I can’t connect to the database.');
mysql_select_db($databaseName,$dbconnection);

$value = "SOMEVALUE";

$query = sprintf("SELECT FIELDNAME from TABLENAME where FIELDNAME='%s';", $value);

$result = mysql_query($query);

$totalNum = mysql_num_rows($result);

while ($row = mysql_fecth_array($result)) {
echo $row['FIELDNAME'];
}

////////OR//////

for ($i =0; $i < $totalNum; $i++) {
$row = mysql_fecth_array($result);
echo $row['FIELDNAME'];
}

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <b> <strong> <dd> <dl> <dt> <i> <li> <ol> <u> <ul> <code> <blockquote> <fieldset> <legend> <h6> <span> <img> <div> <p>
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options