Connect directly to mysql db

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tdsrogers
Forum Newbie
Posts: 11
Joined: Sat Mar 06, 2010 12:44 pm

Connect directly to mysql db

Post by tdsrogers »

Hi everyone :D ,

Im having issues connecting to a mysql db I have created in Toad.

Im working directly from a server and am connecting directly to a database (swdata) using the function 'database_connect'.

I can connect to the database with this function but when I attempt to execute a query it fails.

Does anyone have a general script I can use to connect and execute a query in this way?

Its not just as simple as a mysql_query() function because I have tried this numerous times and it doesnt work.

Sorry if Im being stupid and perhaps a bit vague but Im going insane because it is probably so simple.

Any help would be very much appreciated.

Thanks,
tdsrogers
User avatar
angelicodin
Forum Commoner
Posts: 81
Joined: Fri Nov 13, 2009 3:17 am
Location: Oregon, USA

Re: Connect directly to mysql db

Post by angelicodin »

This is what I found a couple years ago when I was littlery just starting and I've been using the same format for a vary long time. Hope this helps

Code: Select all

<?
$host="";//Host IP or localhost
$username="";
$password="";
$db_name="";
// Connect to server and select database
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
?>
tdsrogers
Forum Newbie
Posts: 11
Joined: Sat Mar 06, 2010 12:44 pm

Re: Connect directly to mysql db

Post by tdsrogers »

Thanks for the reply :D , but this is how I normally connect to a mysql database.

The problem I am having is that Im already connected to the server and am uploading live - I dont need to access it using PHP.

I can connect to the db but cannot query. This is what Im doing to query the db:

$dbCon = database_connect("swdata", "", ""); //This part works

$sql = ("SELECT ...");
$dbCon->Query($sql); //This part doesnt

No data is grabbed and when I echo $dbCon, 'Object' is returned???

Im very confused.
User avatar
angelicodin
Forum Commoner
Posts: 81
Joined: Fri Nov 13, 2009 3:17 am
Location: Oregon, USA

Re: Connect directly to mysql db

Post by angelicodin »

tdsrogers wrote:$sql = ("SELECT ...");
That is your problem I believe.

try $sql = "SELECT...";
same as you had before but drop the "()"

I'm not the best help when it comes to these things but I try.
Post Reply