Accessing MySQL database from PHP code - php

I am making a small database application for the experience, but I am having a problem with actually accessing the database from my PHP code. I've tried accessing the file name, accessing the separate mysql file. But whatever I do, I get an error message
<?php
$password = md5("passhash");
$user = array("name"=> "JohnDoe", "password" => $password, "email" => "email#email.com");
try {
$db = new PDO("mysql:host=localhost; dbname=users.ibd; port=8889", "root", "root");
...
}
Unable to connectSQLSTATE[HY000] [1049] Unknown database 'users.ibd'
Do you guys have any experience with this error message and what I could possibly be doing wrong when it comes to declaring the database name in my PDO initialization?

please check on your db name... if exist on your list of database.
Try to check out on mysql console with command SHOW DATABASES;
You should connect with the table which apears on mysql console, not with the file on mysql folder.

I looked. The .ibd was actually throwing off what was database name. Thanks Guys for the help.

Related

I want to correspond a database,php told me that unknown database but when I use cmd it is worked

The specific situation is that when I try to use the database named"maskorder", I find that I can't connect to the database. I can guarantee that I have completed the establishment of the database and named it maskorder, and there is no problem using the database through CMD. However, when when I need to use the database, the error message displays "unknown database 'maskorder'", After repeatedly consulting the data and trying, I found that this error occurs in all databases except the test database, and no similar situation is found in the Q & A that can be found. My codes as belows:
$link = mysqli_connect("127.0.0.1","root","root","maskorder");
$link or die('error:'.mysqli_connect_error());
mysqli_select_db($link, "maskorder")
or die('error:'.mysqli_error($link));
mysqli_query($link, "set names utf8");
and the next is my MySql database:
then is screenshot of CMD

Database is created but return error as unknown database

I already have created my database, and already have the pages in PHP ready to be connected to the database. My code of connection is the follow :
<?php
$host = "localhost";
$db = "clients";
$user = "root";
$pass = "";
$con = mysqli_connect($host,$user,$pass,$db) or die('Could not connect to MySQL: ' .mysqli_connect_error());
?>
When I try to execute the page with WAMP, it returns me the message :
Warning: mysqli_connect(): (HY000/1049): Unknown database 'clients'
Even the database being already created and the queries executed, it still gives me this error, as if the database was with the wrong name in the PHP or something.
Maybe is the error from not having some password from the user root or using localhost ? How can I fix this ?
I came across this error sometime ago while using MySQL database with node.js, I just deleted the database and created it again. It's funny though, but that was how I solved the error. Try creating the database again with another database name.

connecting a mysql database using php to a host

Okay I'm having trouble connecting my database to my host..
Im using myPHPAdmin and I made a database on my host
the problem is in my php code where I define my connection to the host
everything worked fine when I did this on the localhost.
But now that I want to use it on my personal domain it wont work
it doesnt access the data in the database.
Im new at this so dont shoot me ;)
$connect = mysql_connect("host", "user", "pass");
so for my host/domain I enter my domain the-bloodgod.com ?
what do I enter for user and pass? is it just the login I normally use to access the Cpanel? Or do I have to create special permissions in myphpadmin?
also on my hosts myphpadmin it shows that it stores all created tables in the_bloodgod_com database collection
so would this be correct if I put it the code bellow?
mysql_select_db("the_bloodgod_com");
$sql="SELECT * FROM tablename";
MOST hosting companies you have to use "localhost" as the host name for your database connection code. PHP:
$link = mysqli_connect("localhost","username","password","database") or die("Error " . mysqli_error($link));

Connecting .php file to mySQL database

What I am trying to do is get mysql database to load up my .php file. I am using hostgator to run mysql database server. So far what i have for sql is a table with three columns.
int: id (primary key / A.I.)
varchar: name
text: message
I save the table and name it "test" and the database is called "testdb"
My php file (tutorialTest.php) looks like this:
<?php
$username = "nfoggia_nick";
$password = "imnick";
$database = "nfoggia_testdb";
mysql_connect(localhost, $username, $password);
#mysql_select_db($database) or die("Unable to find database");
$name =$_GET["name"];
$message = $_GET["message"];
$query = "INSERT INTO test VALUES (' ', '$name', '$message')";
mysql_querry($query) or die(mysql_error("error"));
mysql_close();
?>
I added the .php file in my file directory on hostgator and now my problem is this:
I know that this code will do nothing, but when i type in
http://localhost/tutorialTest.php
the web browser says "browser cannot connect to local host" when it should just show a blank screen. Any help? What did i do wrong?
EDIT:
I moved my php file to the document root for my website and now when i run the
http://myWebsiteName/tutorialTest.php this shows up:
Fatal error: Call to undefined function mysql_querry() in /home2/nfoggia/public_html/tutorialTest.php on line 15
Before mentioning all the PHP errors, your URL is wrong.
localhost means your local computer, instead of your hosting environment, which you mentioned is hostgator.
Do you upload your PHP to hostgator server?
Is the MySQL database schema exist in hostgator environment?
Your URL should look like : http://www.hostgator.com/whatever/tutorialTest.php (or under your domain name). Anything but not http://localhost
First of all, remove the # to reveal the error.
For MySQL connection, the first parameter is a string, so you have to enclose it with single quotes, that is:
mysql_connect('localhost', $username, $password);
Last, you have multiple PHP errors :
mysql_querry($query)
You misspelled the function. Also, mysql_error() accepts link identifier as optional parameter instead of a string.
As a side note, stop using deprecated mysql_* functions. use MySQLi or PDO instead. Also, your code is subjected to SQL Injection attack, as you directly allow GET values to be inserted in your query.

mysql_connect - Access Denied to DB or DB doesn't exist?

So I connect to my MySQL database using the following code:
function dbConnect($h,$u,$p,$n) {
if (!$con = #mysql_connect($h,$u,$p)) {$err = err("There is a problem connecting to the database. ".mysql_error());}
else if (!#mysql_select_db($n,$con)) {$err = err("The database \"{$n}\" could not be found. Check your spelling, make sure the database exists, and that your database credentials allows you access to this database.");}
return (isset($err)) ? $err : "";
}
The problem is, if they put in a wrong username, mysql_connect will not see anything wrong with it and try to connect to the database, which outputs an error for the mysql_select_db().
So I found this link. Now normally removing the "any" user would be doable but I'm creating a phpMyAdmin-like tool and will be connecting to different databases with all types of configurations. How can I tell which username can actually connect? And how can I differentiate between the database name not existing and the username not having access to it?
use mysql_errno()
see error codes here

Categories