Trouble with PHP mysql_select - php

Ok, I've just started learning php, and for some reason the mysql_connect() and mysql_select_db() do not appear to connect to the database.
I also need a good example of a mysql table, as I am not sure what to put in my table. How do I manage to connect my site to the database?!
Here is the code I am using in my connect.php page.
<?php
//not working
$connect = mysql_connect('localhost',
'myuser', 'password', 'mypass')
or die("This function should work.");
//still a lingering error
$db_select = mysql_select_db("mydbname", $connect);
?>

If you just start with PHP then a good idea would be to get Apache and MySQL on your own computer. There are plenty of easy installable packages like WAMP (http://www.wampserver.com/en/). There are examples and HOWTOs there. The good in this package, is that there is a PHPMyAdmin that comes packed, and you can use it to make a sample table, or just to play around.
Next, for the concrete code, there is a nice example at the PHP documentation site (http://php.net/manual/en/function.mysql-connect.php).
<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Not connected : ' . mysql_error());
}
// make foo the current db
$db_selected = mysql_select_db('foo', $link);
if (!$db_selected) {
die ('Can\'t use foo : ' . mysql_error());
}
?>
When using the local server you can use the ROOT user in MySQL. For a test table you can make one called, for example friends, with two VARCHAR fields for name and phone number. And you can play around to list them edit them and so on.
Good luck.

Related

Access denied for user 'consult'#'localhost' (using password: NO)

I am using bluehost domain. I want to connect database but I don't know password. After the long war I found the username and localhost name. I am a beginner. Help me to find the phpmyadmin password.
This my php database conection code.This code is worked in localhost.
$link = mysql_connect('localhost', 'consult', '');
if (!$link) {
die('Not connected : ' . mysql_error());
}
// make foo the current db
$db_selected = mysql_select_db('consultv_hrms', $link);
if (!$db_selected) {
die ('Can\'t use foo : ' . mysql_error());
}
Only the admin can reset database password. In this case you need to follow the instructions given by your hosting provider
Log into the cPanel
Click the MySQL Databases icon under the "Databases" category
Change the password
Refer this for detailed instructions

How to execute queries in remote servers

I am trying to insert data to a web site from the code of another web site. How can I use the mysql_connect() command for that. Can I use the IP address of the first web site in the second web site code?
Please help me
Yes, you can use a remote MySQL server with mysql_connect(). Use mysql_connect() according to the PHP manual (http://php.net/manual/de/function.mysql-connect.php) and use your server's IP as $server. However, make sure the MySQL port is accessible from remote on the MySQL server (port 3306).
For inserting data in the database you need to connect the database
Selecting DATABASE
you need to just connect the server on which the db exist in which we are inserting
<?php
$link = mysql_connect('ip or server of the db in which inserting', 'mysql_user', 'mysql_password');
if (!$link) {
die('Not connected : ' . mysql_error());
}
// make myDB the current database
$db_selected = mysql_select_db('myDB', $link);
if (!$db_selected) {
die ('Can\'t use myDB: ' . mysql_error());
}
?>
for inserting records in another website , you need to just connect the server and DB of that particular website, and fire Queries.
let me know if more..

PHP connecting to DB : OOP vs Procedural 1 dont work 2 works

I am scratching my empty head with this issue:
When I have my logging data as procedural, everything works fine. I require the file once and can proceed with my select queries. If the connection to the DB file is however written as OOP. I get the error that No database selected
====
<?php
// SETTING VALUES AS CONSTANTS
DEFINE('DB_USER', 'root');
DEFINE('DB_PASSWORD', '');
DEFINE('DB_HOST', 'localhost');
DEFINE('DB_NAME', 'tra');
// CONNECTING JUST TO THE SERVER
$dbc = #mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$dbc) // IF IT CAN'T CONNECT, ISSUE A MESSAGE
{
die('Could not connect: ' . mysql_error());
}
// CONNECTING NOW TO THE DB
mysql_select_db("tra", $dbc);
?>
So that up there works fine. I have got that in a separate file, (out of the root path)
But if I try to do it OOP, then, the rest of the queries that are to work once I have the access to the DB won't work. I sometimes get the query written as output, I mean as if not executing, just the plain text:
$mysqli = new mysqli("localhost", "root", "");
$mysqli->select_db("tra");
I have tried tons of varieties with the OOP version, first instantiating separately such as
$mysqli = new mysli();
$mysqli->connect("127.0.0.1", "root", "", "tra");
or also
$mysqli = new mysqli("localhost", "root", "");
$mysqli-> select_db("tra");
but none of the tries with OOP will work. I mean, I have the book on my lap, so I am writing it ad litteram. The issue must be elsewhere.
The PHP version is 5.3.8 about the latest one.
The sql query that follows after the connection and selection of the DB (which works if the connection file is procedural, as I say) is:
$sql ="SELECT FName
FROM work_assignment, developer
WHERE developer.country = '".$country."'
AND work_assignment.from_language = '".$from."'
AND work_assignment.into_language = '".$into."'
AND work_assignment.developer_id = developer.developer_id
";
Any ideas as to why?
Thanks a lot
From your comment I surmise you're mixing the mysql and mysqli extensions. They are two separate extensions with nothing in common, except that they both connect to MySQL. If you're connecting to the database using a mysqli object, use that object to run your queries. mysql_query will have no active connection, because you did not establish one using a mysql function.

unable to connect to mySQL Db

I am a newbie to PHP and to the net world, and trying to connect to a mySQL Db using this PHP code:
<?php
echo "Hello World \n";
$mysql_host = "MySQL Host"; // this is what specified to use in mySQL management page at my host
$mysql_database = "mysql_database";
$mysql_user = "mysql_user";
$mysql_password = "mysql_password";
echo $mysql_host;
$conn = mysql_connect($mysql_host, $mysql_user, $mysql_password);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db($mysql_database);
if (!$db_selected) {
die ('Can\'t use '.$mysql_database.' : ' . mysql_error());
}
?>
please, what can be wrong with this code? I couldn't find any syntax error and still - I am not able to connect to the Db
can it be and the problem is related to the host only?
The following variables should contain values that are respectively:
$mysql_host - IP or host name of the server with your database,
$mysql_database - name of the database,
$mysql_user - name of the database user,
$mysql_password - password for specific user to the specific database,
Make sure all of them are correct and then the problem should be resolved. If it is not, let us know.
If you are actually typing MySQL Host in your code, then you're not providing a valid hostname. You should try with localhost instead.
Otherwise, you current host must have provided you with the needed information.
You should also tell us what error your script gives you, when it dies. That enables us to give a qualified answer instead of guessing.
as #Michael says,
$mysql_host = "MySQL Host";
is definitely wrong, that field must have a either a valid host/socketname or localhost..
These guys are spot on. Also, the password is usually blank on a localhost program.
But, this is just something I noticed:
Your $conn variable is missing an 'n' in your 'if' statement.
You are storing connection in $conn but in if condition you are checking with ($con).
(There might be other errors. But I can see this error)

Correct Procedure for mysqlConnect

I am new to php syntax and am looking for advice on creating the most acceptable or correct code. I focus on front end design, but I like to make sure my code is proper. I am in a digital media program, my instructor has given us this code for connecting to our MYSQL databases.
<?php
mysql_connect("localhost", "root", "root")or die("Cannot Connect to DB");
mysql_select_db("Example")or die("cannot select the DB Example ");
?>
However when I look at connect scripts online they set the mysql_connect function as a variable lets say $connect and run an if statement stating; if not $connect produce error, and the same for mysql_select_db. They also close the script with mysql_close($connect); like below
<?php
$connect = mysql_connect("localhost", "root", "root");
if (!$connect)
{
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db("Example", $connect);
if (!$db_selected)
{
die ("Can\'t use test_db : " . mysql_error());
}
mysql_close($connect);
?>
Is either one better? What problems can I have if I dont close the connect with mysql_close?
Regarding using an if instead of an or, it doesn't matter. The or short-circuits, so if the connection worked, die(...) won't be executed. Using either is a matter of preference. If you want to use the or version while keeping the result of the mysql_connect() call, simply assign the whole expression to a variable:
$connection = mysql_connect(...) or die('Connection failed.');
mysql_close() should be used after you're done with all your database communication.
The other mysql_*() functions will use the connection created by the latest call to mysql_connect() or mysql_pconnect(). If for some reason you want more than one connection, trusting the implicit connection object in this manner will fail. Better is to store the connection object yourself and passing it in wherever you need it.
Also before starting to work with databases, you should be aware that mysql is not secure anymore and is deprecated, you should use mysqli. You can use it also as object oriented language.
more details : http://www.php.net/manual/en/book.mysqli.php
example :
<?php
$con = mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>

Categories