Jump to content

[SOLVED] How to know if you are connected to MYSQL within PHP Script


jlgray48

Recommended Posts

I have WAMP installed and all appears to be working fine but when I try to execute a sql command from php it does nothing. I need help.....

 

Here is my code.....

 

<?

 

mysql_connect("127.0.0.1");

mysql_select_db("uah") or die( "Unable to select database");

$sql="create table yahoo(id int, name char(10), primary key(id));";

mysql_query($sql,$conn);

 

 

?>

 

Any help is greatly appreciated. I'm new so go easy on me.

Left that out, it is in my code......

 

$conn = mysql_connect("localhost","jlgray48","peanut99");

mysql_select_db("uah") or die( "Unable to select database");

$sql="create table yahoo(id int, name char(10), primary key(id));";

mysql_query($sql,$conn);

 

Hi friend

You your code mysql_connect method needs 3 param as hostname, username n password.

You can try with the following code . i think it must works.

 

$link = mysql_connect("localhost", "mysql_user", "mysql_password");
mysql_select_db("database", $link);

$result = mysql_query("create table yahoo(id int, name char(10), primary key(id))", $link);

if($result)
{
   echo 'Successfully created';
}
else
{
  echo 'Could not create ';
}

[/code]

<?php

//MySQL information MODIFY IT HERE.

 

$host = "";//Host

$db_name = "";//Database

$db_user = "";//Username

$db_password = "";//Password

 

//Connect to the database

$dbnews = @mysql_connect("$host", "$db_user", "$db_password");

if (!$dbnews)

{

    die("

            <center>Could not connect to MySQL Server($host). <br>" .

        mysql_error() . "</center>

        ");

}

 

//Select the database

if (!@mysql_select_db("$db_name"))

{

    die("

            <center>Could not select MySQL Database($db_name)<br>" . mysql_error

        () . "</center>

        ");

}

 

mysql_query("create table yahoo(id int, name char(10), primary key(id))", $link);

?>

 

Try that and see how it works out.

Here is the error I get when I ran this code..... When I start mysql from windows or the cmd prompt it doesn't ask me for my username and password. Where does it pull the @localhost part from on my user? 

Could not connect to MySQL Server(127.0.0.1). Access denied for user: 'jlgray48@localhost' (Using password: YES)


<?php

//MySQL information MODIFY IT HERE.

 

$host = "127.0.0.1";//Host

$db_name = "uah";//Database

$db_user = "jlgray48";//Username

$db_password = "peanut99";//Password

 

//Connect to the database

$dbnews = @mysql_connect("$host", "$db_user", "$db_password");

if (!$dbnews)

{

    die("

            <center>Could not connect to MySQL Server($host).

" .

        mysql_error() . "</center>

        ");

}

 

//Select the database

if (!@mysql_select_db("$db_name"))

{

    die("

            <center>Could not select MySQL Database($db_name)

" . mysql_error

        () . "</center>

        ");

}

 

mysql_query("create table yahoo(id int, name char(10), primary key(id))", $link);

?>

 

Hi mates

according to your code i have a question to u that is

can you connect to your mysql server from command promt using those credentials (userName : jlgray48  pswd: peanut99)

 

if you can connect with those credentials then you can also connect from the php

 

but as you cannot connect with the credentials you can try with your mysql root user

 

 

Thanks

ShaymoL

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.