How do I replace "mysql_db_query" with more efficient or better phrase?

Hello;

The code below works just fine but I’m wondering if it could be more efficient. The area that I’m most interested in possibly improving is the line just below the “INSERT INTO…” line.

The PHP Manual says that mysql_db_query is deprecated. If I replace the mysql_db_query function with mysql_query or mysql_select_db the whole thing won’t work.

I don’t fully understand why they have deprecated mysql_db_query when it seems to work fine.

I would appreciate if anybody could help me improve the code below.


// Database connection parameters
$db_info['host']   = "myserver.net";
$db_info['username'] = "myusername";
$db_info['password'] = "mypassword";
$db_info['dbname']   = "mydatabase";


// Connect to the database.
$db_connection = mysql_connect($db_info['host'], $db_info['username'], $db_info['password']);
if (! $db_connection )
	die( "Couldn't connect to MySQL" );
mysql_select_db($db_info['dbname'], $db_connection)
	or die ("Couldn't open ".$db_info['dbname'].": ".mysql_error() );


// Reads lines from a text file.
$Lines = file('CodeBPA.txt');

// Loops through and inserts each line into the db table.
foreach ($Lines as $LineNum => $Line) {
   echo "Line #<b>{$LineNum}</b> : " . htmlspecialchars($Line) . "<br>";

// Is there a better way to do this?
$Query = "INSERT INTO BusinessTypes (BPACode) VALUES ('$Line')";
if ( mysql_db_query ($db_info['dbname'], $Query, $db_connection) ) {	
	echo "ok";
	}		
}

Thanks.

just curious, but why doesn’t mysql_query work? or mysql_select_db? it seems like you use mysql_select_db just fine earlier in your script.

I didn’t describe it properly in my first posting. I said the ‘whole thing won’t work’. What I should have said was the ‘insert into’ statement won’t work if I replace mysql_db_query with mysql_query.

What I’m getting at is what do I replace the mysql_db_query with in the ‘insert into’ clause below:


$Query = "INSERT INTO BusinessTypes (BPACode) VALUES ('$Line')";
// What do I replace mysql_db_query here with?
if ( mysql_db_query ($db_info['dbname'], $Query, $db_connection) ) {
    echo "ok";
    } 

I’ve used mysql_db_query in a bunch of places in a content management program I wrote about 1-1/2 years ago (when I was just learning PHP). I’m going to have to go through and replace a bunch of em.

Thanks.

As long has you have mysql_select_db() then you can just do:
mysql_query($Query);

You don’t need to specify the connection parameter if you only have one connection.

Look into Adodb. It will help make working with databases easier.

Thanks.

Hello,

I have the same problem with this function “mysql_db_query()” in PHP ver. 4.3.10.

Before, in PHP 4.0, i used this code:

$conn=mysql_connect($server,$dbuser,$dbpass);
$query= “CREATE TABLE linkscats (uid VARCHAR(10) NOT NULL, heading VARCHAR(50) NOT NULL,description BLOB NOT NULL, PRIMARY KEY(uid))”;
$result=mysql_db_query($dbname,$query,$conn);

…to create a table in the database. Everything was OK. Now, i don’t have any result in 4.3.10.

I replaced that code with:

$conn=mysql_connect($server,$dbuser,$dbpass);
mysql_select_db($dbname);
$query= “CREATE TABLE linkscats (uid VARCHAR(10) NOT NULL, heading VARCHAR(50) NOT NULL,description BLOB NOT NULL, PRIMARY KEY(uid))”;
$result=mysql_query($query);

…and still nothing happen.

Where is the mistake?

Thanks!

Where is the mistake?

If there is a mistake then some debugging will help :wink:


mysql_connect($server,$dbuser,$dbpass) or die(mysql_error());
mysql_select_db($dbname) or die(mysql_error());
$query= "CREATE TABLE linkscats (uid VARCHAR(10) NOT NULL, heading VARCHAR(50) NOT NULL,description BLOB NOT NULL, PRIMARY KEY(uid))";
$result=mysql_query($query) or die(mysql_error());

What might be the problem is that you’re calling the mysql_query function too late in your code. What I would suggest is that you pare down or simplify your CREATE TABLE statment to the bare basics and see if you can get it to work.

Here’s how I re-wrote my code (from above):

$db_info['host']   = "myserver";
$db_info['username'] = "myusername";
$db_info['password'] = "mypassword";
$db_info['dbname']   = "mydbname";

// Connect to the database.
$db_connection = mysql_connect($db_info['host'], $db_info['username'], $db_info['password']);
if (! $db_connection )
	die( "Couldn't connect to MySQL" );
mysql_select_db($db_info['dbname'], $db_connection)
	or die ("Couldn't open ".$db_info['dbname'].": ".mysql_error() );

// I incorporated the mysql_query function dirtectly into the SELECT statement.
$Result = mysql_query ("SELECT ID,BusinessCategory FROM BusinessTypes WHERE ID = '$Line[value]'");

I got it working fine now.