Page 1 of 1

Connect directly to mysql db

Posted: Tue Mar 22, 2011 2:02 pm
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

Re: Connect directly to mysql db

Posted: Tue Mar 22, 2011 3:17 pm
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");
?>

Re: Connect directly to mysql db

Posted: Tue Mar 22, 2011 4:15 pm
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.

Re: Connect directly to mysql db

Posted: Tue Mar 22, 2011 4:54 pm
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.