Forum Moderators: coopster
<?php
$dbhost = 'localhost';
$dbuser = '*******';
$dbpass = '*******';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
$dbname = '*******';
mysql_select_db($dbname);
// Formulate Query
// This is the best way to perform a SQL query
// For more examples, see mysql_real_escape_string()
$query = sprintf("SELECT DISTINCT DiscNo FROM CDG ORDER BY DiscNo ASC");
// Perform Query
$result = mysql_query($query);
// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!$result) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
echo "<table border='1'>
<tr>
<th>Disc No</th>
</tr>";
// Use result
// Attempting to print $result won't allow access to information in the resource
// One of the mysql result functions must be used
// See also mysql_result(), mysql_fetch_array(), mysql_fetch_row(), etc.
while ($row = mysql_fetch_assoc($result))
{
echo "<tr>";
echo "<td>" . $row['DiscNo'] . "</td>";
echo "</tr>";
}
echo "</table>";
// Free the resources associated with the result set
// This is done automatically at the end of the script
mysql_free_result($result);
?>
// This is the best way to perform a SQL query
<?php
// server info
$server = '127.0.0.1';
$user = 'user';
$pass = 'pass';
$db = 'database';
// connect to the database
$mysqli = new mysqli($server, $user, $pass, $db);
if ($mysqli->connect_errno) {
die('Error connecting to the database');
}
$sql="SELECT DISTINCT DiscNo FROM CDG ORDER BY DiscNo ASC";
if( $result = $mysqli->query($sql) ) {
//print the header suppressed
while($row = $result->fetch_object()) {
print(' <tr>'."\n");
print(' <td>'.htmlentities($row->DiscNo,ENT_COMPAT,'UTF-8').'</td>');
print(' </tr>'."\n");
}
//print the footer suppressed
} else {
die ("database query failed\n");
}
// parse id, numerical input only
if ( ( isset($_REQUEST['id']) ) && ( strlen($_REQUEST['id']) > 0 ) ) {
$id= preg_replace('/[^a-zA-Z0-9]/m', '', $_REQUEST['id'] );
} else {
die("bad query\n");
}
// rangecheck id
if(($id<0) || ($id>999999)){
die("bad query\n");
}
// server info
$server = '127.0.0.1';
$user = 'user';
$pass = 'pass';
$db = 'database';
// connect to the database
$mysqli = new mysqli($server, $user, $pass, $db);
// note the ? instead of the replaced parameter
$sql = "SELECT TrackNo, Artist FROM CDG WHERE DiscNo =?";
if($stmt = $mysqli->prepare($sql)) {
$stmt->bind_param("i", $id);
//this assumes the field is an integer, not a string
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($trackno,$artist);
//output header here
while ($stmt->fetch() ) {
print('<tr>'."\n");
print(' <td>'.htmlentities($trackno,ENT_COMPAT,'UTF-8').'</td>'."\n");
print(' <td>'.htmlentities($artist,ENT_COMPAT,'UTF-8').'</td>'."\n");
print('</tr>'."\n");
}
$stmt->close();
// output footer here
} else {
die("ERROR\n");
}