Select database by user? - php

I've create a php based program from where the user start program by selecting database year like 10-11, 11-12, 12-13 etc.. and my database name is like DB1011 for 10-11, DB1112 for 11-12 and so on.
now i have connect.php which is connection file which have
<?php
$link = mysql_connect("localhost","root","123098ABC")
or die("Could not connect to the Server. Check your network connection : " . mysql_error());
$Db = "DB1112";
?>
now how can i do this that when user select database 11-12 then my program work in DB1112 until user not close program. and i want to do this without session if it's possible in PHP?
Thnx..

you can use mysql_select_db() to select the database.
$Db = "DB1112";
mysql_select_db($Db, $link);
more info here : http://in2.php.net/mysql_select_db

You can always change the database using mysql_select_db(dbname);.

Related

MySQL Log-In with PHP - How often?

Okay this might be an obvious question, but as soon as i call MySQL with PHP (whhich means i log in) and then close the PHP tag, (shown below) Do I have to call the database again later?
$mysqli = new mysqli("host", "username", "pw", "dbname");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
$res = $mysqli->query("SELECT EmbedURL FROM Videos ORDER BY RAND() LIMIT 8");
etc...
And then I need to call it again later in the script to fetch something else fromt he database. Do i have to log into the database again?
Thank you!
The database connection will stay open if you do not close it manually. The only possible cause I can imagine that you could loose the connection is the connection timed out or was closed by the databse because you have to many active connections.
It looks like the database variable is nested deeply in your code. You will also loose the reference of the connection if you close the current code block (}).
Here is a post about MySQL connection pooling which could be insteresting for your problem: Connection pooling in PHP

how to create database using wampserver and to connect with the php page

I have created the webpage frontend design for a login page and saved it as .php file.
I have even installed wampserver.
Give me some idea how to connect a mysql database wampserver and my login page.
Is it possible to use the database option given in wampserver inside the tools?
If it is possible, how we can connect to the login page? Please give some idea...
It is possible to create the database in wampserver in Phpmyadmin and can create the tables also.
The following is the code to connect to database
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
//select a database to work with
$selected = mysql_select_db("database1",$dbhandle) // "database1" is the database name
or die("Could not select database1");
//execute the SQL query and return records .. insert ur login info also
$result = mysql_query("SELECT uid FROM login"); // "login" is table
if(mysql_num_rows($result) != 0){
$result1 = INSERT INTO login (id, name, password) VALUES
('".$_POST['id']."','".$_POST['name']."','".$_POST['password']."'); // The values which
//you entered in the login form
} else {
echo "This login name already exists";
}
?>
This is the simple way to connect the database and insert the data into tables. But
dont forget to create database and tables in phpmyadmin before inserting the data

PHP - Cannot connect to MySQL database

I am trying to connect to my MySQL database through php, I am managing my Database with phpmyadmin. To login the username is root and I dont have a password. My problem is I cant connect, I get the "Could not connect to mySQL database" message when I try to
Below is my Code
<?php
session_start();
$server = 'localhost';
$db_usernmae = 'root';
$db_password = '';
$database = 'househockey';
if(mysql_connect($server, $db_usernmae, $db_password)){
die('Could not connect to mySQL database');
}
if (mysql_select_db($database)) {
# code...
die('couldnt connect to database');
}
?>
Im not sure if it matters, but I am using WAMP and I put my phpmyadmin folder into my htdocs folder.
Thanks
As you have written your code :
if(mysql_connect($server, $db_usernmae, $db_password)){
die('Could not connect to mySQL database');
}
This will when connection is true print the following: die('Could not connect to mySQL database'); I think what you need to test your connection, which sounds like it should work:
if(!mysql_connect($server, $db_usernmae, $db_password)){
die('Could not connect to mySQL database');
}
The ! will negate the returned value of your mysql_connect and tell you if you're connected.
As far as I know, PMA explicitly needs a username and password. Set a root password through
mysqladmin -u root password NEWPASSWORD and then change your PMA config, followed by a server restart. Alternatively, use MySQL workbench. It does more than create entity relationship diagrams (ERDs).
You may run into this problem if you have an anonymous user defined on your database.
"When a client tries to connect to the database, the MySQL server looks through the rows in the user table in a sorted order.
The server uses the first row that matches the most specific username and hostname."
Delete the anonymous user and try again.

cannot connect to database test1

I can connect to mysql, but I get an error saying Could not select db.
Is there anything I am missing? Any help is much appreciated.
<?php
$link = mysql_connect('localhost','','');
if (!$link) {
die('Could not connect to MySQL: ' . mysql_error());
}
echo 'Connection OK';
$selected=mysql_select_db("test1",$link) or die("<br>Could q not select db");
mysql_close($link);
?>
Replace
"<br>Could q not select db"
with
"<br>Could q not select db" . mysql_error();
to show detailed mysql error.
Mostly you entered the wrong mysql details. Check if the database exist.
There could be multiple factors preventing you from selecting your MySQL database. The following could be the cause:
Your connecting to the wrong database server. In this case you are using localhost.
Your connecting using the wrong username. In this case you are using no username
Your connecting using the wrong password. In this case you are using no password.
Your trying to select a database that really does not exist. In this case your trying to select test1. If the database does not exist you will need to create it.
You can duplicate what your program is doing by logging onto your console and issuing the following command:
mysql -e "show databases"
This will list all of the databases available to that user.

how i can Import data from Database ( My Sql) to another website

I need to show data from another website in my website,
i had control panel for tow site but i need it dynamic way to connect them
website 1 website 2
***** *****
* A * <<<< =Data== * B *
***** *****
so i write
$link = #mysql_connect("Ip Addres to another website","username for mysql 2","passowrd 2 ")or die("Couldn't make connection.");
#mysql_select_db("qatarlab_test",$link)or die("Couldn't select database");
$factorRes = #mysql_query("SELECT count(id) FROM `factor` ");
$factorRow = #mysql_fetch_array($factorRes);
echo $factorRow[0];
but nothing happen
Firstly, you must enable remote database connection in the original site so that the new site can connect to it. you can do this in the control panel->Database(Remote Database Connection) of the original site.
You could specify the IP or domain of the new site to that it allows only the new site to connect or you could just add wildcard (%) to allow any external connection to the database.
when this is done, run ur query again.
hope it helps
Hi Please try code given below,
$link = #mysql_connect("Ip Addres to another website","username for mysql 2","passowrd 2 ")or die("Couldn't make connection.") or die(mysql_error());
mysql_select_db("qatarlab_test",$link)or die("Couldn't select database", $link) or die(mysql_error());
$factorRes = #mysql_query("SELECT count(id) FROM `factor` " , $link) or die(mysql_error());
$factorRow = #mysql_fetch_array($factorRes);
echo "<pre>";
print_r($factorRow);
echo "</pre>";
Here $link will be for specific database connection and die(mysql_error()) for if any error in or query or database connection.
thanks
In your first line: mysql_connect you are passing the IP Address and Username for connecting the DB.
Make sure that the user has the privilege to connect to the DB remotely (i.e. via a machine other than the Localhost). In general, when a user is added in the DB its default access is for the localhost. Check out this link: http://dev.mysql.com/doc/refman/5.1/en/adding-users.html
You basically need to add user using: CREATE USER 'admin'#'IP Address of Website 1'; All the CPanel's have this provision in their interface as well.

Categories