answersLogoWhite

0


Best Answer

Simple way to connect to mysql server using PHP..

$hostname = 'localhost';

$username = 'root';

$password = 'anypassword';

//Connecting to mysql server..

$connect = mysql_connect($hostname, $username, $password);

if(!$connect)

{

die('Connection Failed'. mysql_error());

}

//Selecting the database..

$select = mysql_select_db('any_database_name');

if(!$select)

{

die('Database not Selected'. mysql_error());

}

?>

User Avatar

Wiki User

13y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

14y ago

You'll need to set Dreamweaver to "Split" or "Code" view, then add the following code somewhere near the top of your script (assuming MySQL):

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password') or die(mysql_error());

mysql_select_db('foo', $link) or die(mysql_error());

?>

You can then reference $link when running queries.

For more info visit the following pages:

  • http://au2.php.net/manual/en/function.mysql-connect.php
  • http://au2.php.net/manual/en/function.mysql-select-db.php
  • http://au2.php.net/manual/en/function.mysql-query.php
This answer is:
User Avatar

User Avatar

Wiki User

13y ago

to connect with server:

$connection1 = mysql_connect($server_ip, $user_name_of_database, $users_password);

to select the table

mysql_select_db($connection1,$name_of_database);

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

PHP has built in functions to access a MySQL database. A quick example would be as follows:

<?php

$c = mysql_connect("my_server", "my_username", "my_password");

$db = mysql_select_db("my_database", $c);

$q = mysql_query("SELECT * FROM bar");

while($row = mysql_fetch_assoc($q)){

print_r($row);

}

?>

In this example, you would be connecting to a server called "my_server" with the username "my_username" and the password "my_password". The database "my_database" is selected, and all records on the table "bar" are selected.

If the table "bar" has two fields in it, "id" and "foo", with three records, then the output might look like this:

Array

(

[0] => Array

(

[id] => 1

[foo] => John Jacob

)

[1] => Array

(

[id] => 2

[foo] => Jingleheimer

)

[2] => Array

(

[id] => 3

[foo] => Schmidt

)

)

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

PHP has many functions to allow users to work with MySQL database. To open a connection to a MySQL Server, one should follow three steps. First, one should connect MySQL itself by setting up variables such as username, password and server. The next step is to specify the database that one needs to open. Once it is completed, use the closing function in PHP to close the connection.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

Then you can use mysqli to query the database with

$results = $mysqli->query("SELECT a FROM b");

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the correct way to connect to a MySQL database?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How you can take backup of database in mysql?

It is quite easy to back up a database using dbForge Studio for MySQL. To do this, open the tool and connect to the MySQL server. In the database explorer, right-click the database you want to back up and select Backup and Restore &gt; Database Backup. Additionally, the tool allows you to automate the backup process using Task Scheduler.


Is Wikipedia a database?

The coding of Wikipedia relies heavily on PHP and MySQL with almost everything but the images in a MySQL database. The locations of the images are in the database. So yes, in a way, Wikipedia is a database. However a database is a type of software that stores data, while Wikipedia is a wiki, a collaborative editing site.


You would like a free database program?

If you are looking for a Web Database program, then MySQL is the way to go. If you're looking for a free database program (like Microsoft Access) then you can try http://www.openoffice.org/


What are some MYSQL backup methods?

Some of the methods for backups using the MYSQL method are to download programs specifically for ones needs like AutoMySQLBackup. To manually backup the database, the 'noupe' website offers information on how to do this.


How can you connect to an Oracle database using PHP?

Is there an ODBC driver available for your Oracle database? If so, the ODBC functions may be the way to go.


What do you need to do to make PHP and MySQL work together?

The easiest way to get started is to install XAMPP. It comes with MySQL, PHP, Apache, phpMyAdmin (for database management,) and various other tools. To connect from PHP to MySQL database, you use this: $DBconnect = mysql_connect ("localhost","username","password"); mysql_select_db("yourDatabaseName", $DBconnect); ?&gt; That will just connect you ready to use the database, create your tables in phpMyAdmin, it's easier. When you are ready to start adding/viewing data, you could use the following to add data: $sql = "INSERT INTO myTable VALUES('value1', 'value2')"; mysql_query($sql); ?&gt; Then the following to view data: $sql = "SELECT * FROM myTable"; $queryResult = mysql_query($sql); if (mysql_num_rows($queryResult) == 0){ echo "Table Empty"; } else { while ($dbRecord=mysql_fetch_array($queryResult)){ $value1 = $dbRecord['field1']; $value2 = $dbRecord['field2']; echo $value1.', '.$value2.' '; } } } ?&gt; This is a solution and works fine, but if your looking to install it all on your own/manualy. You have to install MYSQL php module. Here is a very nice TUT that will help you if you chose to do so manually: http://www.php-mysql-tutorial.com/install-apache-php-mysql.php Every server I've setup I do it manually. Its just the way I am, I like having control over the whole process.


Why use mysql instead of oracle?

Most people who select oracle over mysql are doing so for a variety of reasons, not one specific feature. Price is usually the most important factor. MySQL is free, and that's why many people choose to use it. Oracle is time-tested/proven, and that is why many people choose it.However, many corporations purchase volume licenses and so the cost of adding an new database/user to an oracle database is not as much as it would be to start your first implementation.Reasons to use oracle instead of MySQL:1.Oracle has many built in functions such as decode that make some queries easier to build.2. Oracle is a private company, and some people argue this means that they will deliver releases/fix defects/etc more regularly, since they rely on the income to survive.3. Oracle has PL/SQL, a built in language that is very powerful. You can also have java stored procedures, this is much more powerful then what MySQL offers.4. Oracle has support for sequences, MySQL does not.5. Mysql does not support table snapshots6. Oracle can be customized and tuned, much more then MySQL can.7. Oracle provides Hot Backup/Standby facility, Mysql provides this facility only for selected engines.8. Oracle provides Clustering Mechanism, where as MYSql does not.9. Oracle Provides two way replication, Mysql provides only one-way replication.10. Oracle provides DB links to connect to a remote oracle server.


How do you connect oracle 10G developer to oracle 9I database?

I hope you can connect your Oracle9i database from Oracle10g as the same way how you connect from Oracle10g itself. But you have to add Oracle9i connection details at your Oracle10g TNSNAMES.ORA file. Simply you copy any other connection setting and change the name, Sid or source name, host name or ip address.


Where can one get a tutorial for Mysql select?

A good place to start for learning mysql is with the dev mysql site. This gives tutorials and a well indexed menu for finding your way around. It is easy to understand which is a bonus.


What are the advantages of PostgreSQL over MySQL?

The main difference that I see is Postgres' advanced support for things like Views, Stored Procedures, Transactions and other more advanced database functions. This is not to say that MySQL does not support these, however Postgres' advanced features are tuned to be highly performative. Also, Postgres supports more concurrent users than MySQL. That all being said, if you web application is a relatively simple, mostly read-only, and doesn't require large number of concurrent connections, MySQL is probably the better way to go, as it is performatively optimized for reading, and is simpler to install, maintain, and has better third-party tool suppport. :)


Can you connect to database in unix environment using c or c plus plus languages?

At the simplest level, a database is simply a data container. As such an array can be considered a database. However, when we think of a database we usually imagine a container that combines one or more related tables of data, that allows us to easily modify data, to search for data, and so on. Databases are typically disk-based centralised repositories (data servers) containing a massive amount of data, while end-users (data clients) are really only concerned with a small amount of that data (a subset). Data can also be generated by the database, such as when returning the number of records in the database that match a specified criteria. Database Management Systems (DBMS) are the simplest way to make and use a database as all the functionality you need is readily available, all you need do is connect to the DBMS and query it, typically using a text-based script such as SQL (structured query language). MySQL is a popular choice because it is open source and can be used under the terms of the Gnu Public Licence (GPL).


How do you connect to Postgresql in Linux?

Postgresql is a database server. It can run on any machine, not just linux. A variety of applications need databases, some of them connect to Postgresql. An application connects to the database in the same way, reguardless of which system you are running. You could be running Windows, OSX, Linux, BSD, Solaris, etc... it doesn't matter. To answer your question, you connect to the database using whatever programming language you are using. The connection will be the same reguardless of what operating system you are running, but will be different depending on what language you are writing your application in. For example, if you wanted to connect to a Postgresql database in python, you would do something like this: import pg con1 = pg.connect('testdb', 'myhost', 5432, None, None, 'bob', None)