You should initiate your connection before quering the database.
$mysqli = new mysqli("example.com", "user", "password", "database");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: " . $mysqli->connect_error;
}
It's also dangerous to just append your string from $_GET. Your site can be attacked with SQL injection. You definitely should use PDO with prepared statements. Or some another library like that, I like dibi for example.
Update:
You are accessing column name
that doesn't exist.
It should throw a notice (PHP Notice: Undefined property: stdClass::$name
). If you don't see notices and you are developing locally (and you definitely should), you can enable it in your php.ini
file with error_reporting = E_ALL
.
You probably want column newsletterId
.
So altogether:
$mysqli = new mysqli('example.com', 'user', 'password', 'database');
if ($mysqli->connect_errno) {
echo 'Failed to connect to MySQL: ' . $mysqli->connect_error;
}
$dateRaw = $_GET['bdate']; // for example '22.12.2012', if this value is already in format YYYY-MM-DD, you can step next line and use it
$date = date('Y-m-d', strtotime($dateRaw));
$statement = $mysqli->prepare("SELECT `newsletterId` FROM `newsletter` WHERE `ndate`=? LIMIT 1");
$statement->bind_param('s', $date); // replace the question mark with properly escaped date
$statement->execute();
$result = $statement->get_result();
if ($result->num_rows) {
$newsletterObject = $result->fetch_object();
$newsletterId = $newsletterObject->newsletterId; // you had $newsletterObject->name here
} else {
echo "Newsletter with the date doesn't exist";
}
$statement->close();