Apek! Delighted to hear you've got it working.
OK 1st the extra arguments...
$link refers to the mysql-link resource handler.. in place of $link use what you set in the call to mysql_connect() or mysql_pconnect(), the function that you use to create the link to the database resource.
So if you do this:
$link = mysql_connect('localhost', 'user', 'pass');
Then you would pass $link as an argument to the query() function, and the reason for this is two fold, the first reason being $link is set in global scope and because of variable scoping rules, mysql_query() won't automatically pick up on its existence as it does when you simply make a call to mysql_query() in global scope without the database wrapper function. The second reason is when making wrapper functions you should plan to have at minimum the ability to take all the arguments that the original function takes, in this case that being the mysql_query() function which takes two arguments, the first being the query string and the second an optional reference to the variable containing the mysql-link resource handler.
Here is something that I do:
I make each of these extra arguments optional, and then I can fill in the values if I think it will be important to have the extra information. I would make $link optional, because in my scripts I only make a call to mysql_connect() once, and only connect to a single database.
If I place its value in a superglobal, then I can import it into the function, this is nice because it eliminates the need to include it as an argument, and if I really need to specify it explicitly, I can.
__LINE__ and __FILE__ are special "magical" predefined constants, I include these because if you are working on a large project and a query fails it will help you to go directly to the source of the problem... I have scripts that can quickly grow to 2000 or so lines.. in all that you might forget where you make this or that query!
__LINE__ will be the line number that the query appears on, and __FILE__ will be the absolute path to the php script. These are considered "magical" in that they change dynamically based on where in a script you use them.
// Place the mysql link handler in the $GLOBALS suberglobal array
// It will be available in $link AND in $GLOBALS["link"] while in global scope
// (outside of a function or class) it will be available in $GLOBALS["link"] only
// while inside of a function or class.
$GLOBALS["link"] = mysql_connect('localhost', 'user', 'pass');
function query($query, $line = null, $file = null, $link = null)
{
// Automatically import the value of $GLOBALS["link"];
// if $link does not contain a value.
if (is_null($link))
{
$link = $GLOBALS["link"];
}
$result = mysql_query($query, $link);
if (empty($result) && ini_get('display_errors') == 1)
{
echo mysql_error().": ";
echo mysql_errno()."<br />\n";
echo $query."<br />\n";
if (!is_null($line))
echo "@ line: $line ";
if (!is_null($file))
echo "in file: $file<br />\n";
}
return $result;
}
Using the above you can make queries like this:
$result = query("SELECT * FROM `mytable` WHERE `this` = that'", __LINE__, __FILE__);
I left off the $link argument, because now that I've rewritten the function to import it automatically we don't need to specify it explicitly.
I also left off a quote mark in the query.
This will produce error output like this:
#1064: You have an error in your SQL syntax near ''' at line 1
SELECT * FROM `mytable` WHERE `this` = that'
@ line: 1 in file: G:\www\test.php
All that make sense?
Some urls to browse:
http://www.php.net/mysql_query
http://www.php.net/mysql_error
http://www.php.net/mysql_errno
http://www.php.net/manual/en/languag...predefined.php
OK your other question:
Quote:
quote:
and why the insert operation cannot insert data to datetime datatype??
the $_POST["jam"] and $_POST["kerosakan"] will be inserted to the datetime field..
but when i check mysql,there always show 0000-00-00 00:00:00,no matter what i put in the input textbox it still record 0000-00-00 00:00:00.
|
When you create a date/time field, you need to use MySQL functions to specify the time, or structure your string to the exact format of the field.
Example:
INSERT INTO `time` VALUES(now());
Will insert: 2004-01-13 23:56:58
OR
INSERT INTO `time` VALUES('2005');
Will insert: 0000-00-00 00:00:00
OR
INSERT INTO `time` VALUES('2005-01-13 23:56:58');
Will insert that exact string.
If you want a custom time, I would suggest changing the field type to CHAR (a fixed character length) And use PHP to generate a timestamp, I would suggest using UNIX timestamps which are a bit more compatible with the PHP functions mktime() and date() and just IMO more flexible overall. date() will take a UNIX timestamp and format it however you like.
More information:
http://www.php.net/date
http://www.php.net/mktime
http://www.mysql.com/doc/en/DATETIME.html
: )
Rich
:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::