MySQL Date types and other misc queries

view a random?

Having a real bother trying to get the data to insert into my custom built tables (which I might add are rather ugly!) - but for now it is ok, working is the functionality I desire most. Unfortunately, I’m beginning to have difficulty focusing, so it has possibly reached time to sleep.

But first, I want to get my dates inserting into the tables (these are all machine generated, hence the ugliness!). Dates are in using the format:
$date = date("Y-m-d H:i:s");
$host_ip = $_SERVER["REMOTE_ADDR"];
$set_up = "INSERT INTO `survey` (`ip`, `date`) VALUES ('$host_ip', '$date');";

Obviously I’m recording IP as well… no good reason to at the moment, but it could be useful to find out how honest people are in their responses to where they are from. Which can be used to work out how much weight we put behind the rest of what they say.

Unfortunately, the query isn’t working when going from PHP. But it did directly into the SQL client. Ok, it turns out the issue I was having there was the old defining the variable in my exception handling statement… so it was never set and always failed!

Got that sorted. And so now there is a lot of error handling around my database connections! Check this out:

/**
* connect to the database.
*
* @return mysqli $conn A database connection. Or throw an exception if we can't
* return a connection.
*/
function db_connect()
{
static $conn;

if(isset($conn))
{
if($conn->ping())
{
//echo "connection is good? ";
return $conn;
}
else
{
//echo "connection is bad? ";
$conn->close();
}
}

echo "\n\n\n\n”;

@$conn = new mysqli(DB_SERVER, DB_USER, DB_PASS, DB_NAME);
// or die(mysql_error());
if (!$conn||mysqli_connect_errno())
{
throw new Exception(’Could not connect to database server:’.
mysqli_connect_error());
}
else
{
return $conn;
}
}

I also wanted to chat a little about inner joins, left joins, right joins and outer joins… personally this level of naming seems ridiculous to me - but I’ve had an issue around admitting that there is anything to database management at all… so I will get onto this stuff (because a join is a join to me at the moment!).

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • MisterWong
  • Reddit
  • Scoopit
  • StumbleUpon
  • Technorati
  • rss

Post a Comment

You must be logged in to post a comment.