I have installed my php website with database in IIS, windows 2012, The problem is it is accepting if i mention db name before table name for each query in my website. example below
select userid,profile_id,email,first_name,last_name,password,login_flag,usertype
from reqsbook.user
where email='$email'
and password='$password'
and usertype='$usertype'
actually i used to run this website in linux web server but as per my client requirement i have installed it in iis server.
please let me know is there any possibility for remove db name before table in all queries in my project or can i keep db name at once place for universal
Three ways to do this, assuming your database name is reqsbook.
Give the SQL command USE reqsbook; immediately after connecting from php to your dbms, before you give any SELECT or other queries.
if you're connecting with mysqli_, use something like this:
$mysqli = new mysqli('hostname', 'username', 'REDACTED_PASSWORD', 'reqsbook');
If you're connecting with PDO, try something like this
$db = new PDO('mysql:host=hosthame;dbname=reqsbook', 'username', 'REDACTED_PASSWORD');
If you're connecting with mysql_, well, stop doing that unless you want your customer's application pwned by cybercriminals.
Related
I have a webpage that has to use multiple databases at a time. In localhost, it works perfectly fine but while hosting in online free hosting sites, I get error if I use more than one PDO connection and only the first database connection works. Is there any possible way to use multiple databases using a single PDO connection?
Please HELP!
When connecting to a host, you don't have to specify a database:
$dbh = new PDO('mysql:host=127.0.0.1','USER','PASS');
Just make sure to include the database name when running your queries as such:
select * from db1.table1
select * from db2.table2
Hey guys the code below is what I am trying to run. I am trying to run a practice MySQL server because I am going to host it on my schools free database which is MySQL
I am using phpmyADMIN but I am a bit confused because yall are saying I am not connecting to MySQL
so what is this code to create a database? Do I need to download MySQL or something? I thought phpmyadmin is the same syntax to create a database/tables/ and values? since I have MySQL turned on?
//creation of database
$sql= 'CREATE DATABASE project';
if(mysql_query($sql,$con)){
echo 'DB created succesfully';
}
else{
echo 'error creating DB' . mysql_errno();
}
PHPMyAdmin is a web-based software program created with PHP to help you administrate MySQL databases. It has its own internally built code to connect to your MySQL databases and perform standard functions such as insert, select, delete, update, etc.
PHPMyAdmin does not really have anything to do with you creating your own PHP program. You will still need to connect to MySQL (as PHPMyAdmin does behind the scenes). You can still use the same code that PHPMyAdmin generates for you for use in your own PHP programs, but you must already be connected to MySQL as a user with valid permissions.
In your PHP code it is important to note that you are using the mysql_* functions which have been deprecated and you should be using the mysqli_* functions instead.
Your code should actually look something like this:
//connect to db
$link = mysqli_connect("localhost","my_user","my_password");
//creation of database
$sql= 'CREATE DATABASE project';
if(mysqli_query($link, $sql)){
echo 'DB created succesfully';
}
else{
echo 'error creating DB' . mysqli_errno();
}
MySQL is the database server you connect to in order to execute queries such as CREATE DATABASE project. phpmyadmin is a PHP-based front end to MySQL to allow easier browsing and manipulation of the database.
Think of text documents on your computer. notes.txt is a text file, but you could open it with any text editor such as Notepad or TextEdit.
Your specific snippet of code is missing the database connection string, so something more appropriate might look like:
$connection = new mysqli('db_host', 'db_user', 'db_pass');
$sql = "CREATE DATABASE project";
$results = mysqli_query($connection, $sql);
//check for errors
Without connecting to the database, your SQL is all just strings of text with no meaning.
I tried a lot hoping to get a good job here
If you are logged into phpmyadmin, then the only code you can execute is just SQL statements. Like: create database my_db1
I'm relatively new to PHP and databases. Currently I'm working with an existing Access database and am able to read and display the data just fine. However, I'm wanting to update a user record through an email verification link. I have the server send an email with a link of www.domain.com/verify.php?userID=#
I am able to read this GET variable just fine, but I'm completely lost as to how I update the record. Everything I search for is for updating MySQL databases whereas I'm using odbc.
Does anyone know how I could set this up?
something like this:
odbc_exec($conn, "UPDATE table SET field=value"); // $conn is your connection identifier
You need to use a ODBC database client library in PHP to connect to a ODBC datasource. For example you can use PDO with the driver ODBC and DB2 Functions (PDO_ODBC).
I don't know much about programming, so I hired someone to create a simple MySQL database with an online form that I can enter information into.
I already setup the MySQL database at my hosting account and gave the programmer the login details as well as an FTP account.
He says he needs my hosting username/password in order to create the database because he needs to use PHPMyadmin to create the tables and test the database.
This seems kinda shady to me. Can't the programmer create the tables using PHP commands alone? I don't want to give out my main username/password to a stranger.
I'm hosted at 1and1.com if that matters any
If you give the MySQL user sufficient database permissions, they can use the CREATE TABLE statement to create the tables without having to use PHPMyadmin. If they don't know how to create tables without using the GUI, then I would be suspicious of their overall database programming abilities as being the lead on your project.
PHP can execute any SQL statement on a MySQL server which it has a valid connection too, including the CREATE TABLE command, however, these will be subject to the "privileges" of the user that PHP used to gain the MySQL connection.
The MySQL user that you provided the login credentials for may not have create table privileges. In this case, PHP could do nothing, as the SQL commands executed would fail.
That said, I don't know why your hosting username/password would be needed to use PHPMyAdmin. Usually a login to PHPMyAdmin is done with the MySQL user, not the hosting user. Though, that user would be subject to the same privilege restrictions, so we're back at part 1:
Does the MySQL user you created for the programmer have create table privileges?
If yes, they should be able to create tables from PHP alone, and if you have PHPMyAdmin installed, they should be able to log in using that same user already (and not your hosting user!)
If no (probably unlikely, though I don't know 1and1), then you will need to provide a MySQL user that does have create table privileges before they can do anything; then see above.
It's possible to submit the necessary CREATE TABLE queries via php to the database, but the developer is likely more familiar with the way PHPMyAdmin displays what is set up on the server.
Sounds viable to me. Technically he can do everything with PHP alone, but it is much much easier to use PHPMyAdmin.
I think you should hire someone you can trust and give them the tools to help them do a good job.
You don't have to give him your user/pass. You can create a new user in PHPMyAdmin, with less privileges. When he finishes installing your database, just delete that user.
Yes, he can create databases using only PHP, but it is slower than just use PHPMyAdmin.
Probably the person you hired is not very skilled in software development and the only way he know for modifying MySQL is using that web interface.
You can do it this way:
http://www.w3schools.com/php/php_mysql_create.asp
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
// Create database
if (mysql_query("CREATE DATABASE my_db",$con))
{
echo "Database created";
}
else
{
echo "Error creating database: " . mysql_error();
}
// Create table
mysql_select_db("my_db", $con);
$sql = "CREATE TABLE Persons
(
FirstName varchar(15),
LastName varchar(15),
Age int
)";
// Execute query
mysql_query($sql,$con);
mysql_close($con);
?>
I might also create a DBUser that has access to the database and tell him to download a mySQL client (such as HediSQL) to do his database work. Then after they are done, just remove the DBUser (or take away all permissions).
I am using one db file to connect to the database. However, i need to show something else from another db database on this site. I can't use two db connect files. So, how do i go about display one websites content on another website with two different connections.
For one thing, you don't need separate database connections to the same server. Simply specify the database name before each table name in your SQL statements. For example:
SELECT foo, bar FROM db.table;
rather than:
SELECT foo, bar FROM table;
For another, you shouldn't be storing the database connection in a global for just the sort of reason you're running into. I suspect you're using the outdated mysql extension, which uses an implicit DB connection resource whenever one isn't explicit. Switch to PDO and create a simple connection manager class. One big advantage to PDO is it supports prepared statements, which can be safer and more performant than what the mysql extension provides. If you need a PDO tutorial, try "Writing MySQL Scripts with PHP and PDO"
If it is a separate db server you might need to look into the mysql federated engine which allows you to link to databases on separate servers as if they were on the same host.