if (mysql_db_query ($database, $Query, $Link)) {

I have been using the command:
[php] if (mysql_db_query ($database, $Query, $Link)) { [/php]
which is returning a Deprecated error.

In searches, I have found that I should be “Using mysql_select_db() and mysql_query() instead”, however I haven’t a clue as to how to use both of these together, to utilize the all of the $database, $Query and $Link parameters.

If I use just “mysql_select_db(…)” I get the error that it expects 2 parameters, but 3 given, so I assume I also need the mysql_query() function incorporated also… somehow?

I am using this to insert records into my MySql database. Can anyone advise the exact “if” command to use instead of mysql_db_query?

Use mysql_select_db to select the MySQL database and mysql_query to run the query:

[php]mysql_select_db(‘DB_NAME’, $link);
mysql_query(‘SOME QUERY’, $link);[/php]

jShertz,

thanks for the info. However, I need to incorporate both functions into an if statement, replacing the single function: if (mysql_db_query ($database, $Query, $Link)) {

      ................

} else {

      ................

}

Does the IF just check to see if the query failed?

You could use:

[php]$result = mysql_query('SELECT * FROM table', $link);

if($result) {
//
} else {
//
}[/php]

Yes this was just to check for a failure. Per your info, I used the following:

[php]$result = mysql_query(‘SELECT * FROM mastertable’, $Link);

if($result) {
print “The query was successfully executed!”;
} else {
print “The query could not be executed!”;
}[/php]

With my prior if statement using the deprecated mysql_db_query, I received the deprecated error, regardless of whether the insert was successful or not.

However, besides the deprecated error, I was receiving the “unsuccessful” message, which is another problem other that the deprecated error I am trying to fix here.

With your code, I now receive a “success” message, however it is not inserting the record into the database. It now seems as if you code is allowing a “success” message, but actually it is “unsuccessful” as before.

Have you replaced the dummy query in my example with your INSERT query?

Here is my code…

Again, with your code, even though I had a typo creating a “failure”, your code (as I changed below), advised a “success” but didn’t insert the record. I have now fixed the typo, so it now inserts records ok, but with the deprecated error, using my original code below. Using your code now, I won’t know if it is a failure, even when advising “success”.??

[php]<?

// include provides the $serverName, $userName, $password, & opens database

include “include.php”;

// fields from my input form.

$field1 = $_POST[“field1”];
$field2 = $_POST[“field2”];
etc…

$Link = mysql_connect ($serverName, $userName, $password);

$Query = “INSERT into mastertable values
(’$field1’,
‘$field2’,
etc…)”;

print “The query is:
$Query

”;

//Replaced the following…

// if (mysql_db_query ($database, $Query, $Link)) {
// print “The query was successfully executed!”;
// } else {
// print “The query could not be executed!”;
// }

// With your…

$result = mysql_query(‘SELECT * FROM mastertable’, $Link);

if($result) {
print “The query was successfully executed!”;
} else {
print “The query could not be executed!”;
}

mysql_close ($Link);

?>
[/php]

Which version of PHP are you running? It may be that it has all mysql_ functions put as depreciated - the PHP developers are trying to stop people using them.

Two alternatives to the mysql_ functions:

PDO

[ul][li]http://php.net/manual/en/book.pdo.php[/li]
[li]http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/[/li][/ul]

MySQLi

[ul][li]http://php.net/manual/en/book.mysqli.php[/li]
[li]http://www.willfitch.com/mysqli-tutorial.html[/li][/ul]

It’s returning success without inserting anything, because you’re checking the success of a SELECT QUERY:

[php]$result = mysql_query(‘SELECT * FROM mastertable’, $Link);[/php]

Change it to this:
[php]$result = mysql_query($Query, $Link);[/php]

Then you should actually be using the INSERT query you made higher up.

To All,

Thanks, that did the trick.

Regards,
Dave

Sponsor our Newsletter | Privacy Policy | Terms of Service