I think it is a foolish question but I cannot understand why it is happening... I want to connect to my MySQL database with this simple code:
<html>
<head>
<title>File1</title>
</head>
<body>
<?php
$con=mysql_connect("localhost","root","") or die("Failed to connect to MySQL: " . mysql_error());
$db=mysql_select_db("Users",$con) or die("Failed to connect to MySQL: " . mysql_error());
if (mysql_error()) {
echo "Failed to connect to MySQL: " . mysql_error();
}
?>
</body>
</html>
As user, I use root. The problem is that root needs a password. So, with this code I should obtain on my screen "Failed to connect to MySQL", right? However, the screen is white.
Why is white instead of appearing "Failed to connect..."?
Many thanks!
I think errors are not displayed in your server , Inside your php.ini add or change value of this line:
display_errors = on
Then restart your web server.
Probably a simple case of fatal errors being hidden?
Use this to turn errors on:
error_reporting(E_ALL);
ini_set('display_errors', true);
In your page, Your connection got established, there is no success message, there is no other browser output aswell. So that it seems blank screen.
if ($con) {
echo "Connection successful!!";
}
The reason you're not getting any output is because of the or die.
die() stops the execution of the script.
$con=mysql_connect("localhost","root","");
$db=mysql_select_db("Users",$con);
The difference between error display on or off, is the warning you get before the script exits through the die() command.
You will always see the message printed by die() at the end of the output as that is unrelated to any php error handling:
Failed to connect to MySQL: Access denied for user 'root'#'localhost'
(using password: NO)
The only reason I can think of that the message not shows, is that the php is not getting executed; for example if your file is an .html file instead of a .php file. That would lead to a blank page as the opening < of the php section will be seen as the opening of an html tag and the same applies to the closing tag.
Checking the source of the output would confirm that.
Your $db variable is unassigned.
Use this instead, by removing $db=
mysql_select_db("Users",$con) or die("Failed to connect to MySQL: " . mysql_error());
Plus, mysql_ functions are deprecated. For more information on this, visit the PHP.net Website.
http://www.php.net/manual/en/function.mysql-db-query.php
I suggest you start using mysqli_ with prepared statements or PDO instead.
WARNING
This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Related
I'm trying to connect database.
Here's the details of my work:
$con=mysql_connect("localhost", "root","");
mysql_select_db("employees",$con);
if(!$con)
{
die("cant establish database connection".mysql_error());
}
else
{
echo "connection Created";
where in xamp my database,which i created in phpmyadmin in localhost xamp, is on the following directory:
C:\xampp\mysql\data
Here a folder named Employees is created then it has some files in it.
when i run php file to create connection it shows nothing on the screen according to my code it must show :
connection created
now can anyone tell me where im wrong? why its not showing any output?
If that's your whole script, you're missing a closing brace at the end }.
Turn on error display, it will be useful in the future: How do I get PHP errors to display?
Also have a read of this: Why shouldn't I use mysql_* functions in PHP?
I am currently using this code to hide mysqli_connect() errors.
error_reporting(E_ALL);
ini_set('display_errors',0);
But with reporting ON, when I introduce an incorrect setting into a mysqli_connect() param the usual gibberish appears. Is it possible to display a message of my own such as "Check DB connection settings" rather than the garble you usually get IF mysqli_connect() fails?
I presume this is done with try/catch. Can anyone shed some light on this with of how I might do the above?
Could be along the lines of pseudo If mysqli_connect bad settings, echo "bad settings please check".
Thanks.
Try this:
mysqli_connect("myhost","myuser","mypassw","mybd") or die("THIS IS THE ECHOED MESSAGE");
You can also use this:
$conn= mysqli_connect("myhost","myuser","mypassw","mybd") or die("SOME PREAMBLE: " . mysqli_error($conn));
My PHP code works well in connecting remote windows system mysql database and returns the output. But, when I'm using the same to connect remote linux system's mysql database, I got the following error:
"mysql_query() expects parameter 2 to be resource, boolean given in
C:\wamp\www\mysqldb.php on line 88"
That line 88 have the following content "$this->resultQur =
mysql_query($query, $this->connID);"
Help me to solve this.
yes. The resource is null in this case. But the same works in windows mysql connection. I got the error only in linux. Need to do any change for linux environment?
While putting "print mysql_error();" i got the following error
"A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond."
The resource you're providing comes from a mysql_connect and that did not succeed!
Put error reporting on
Build some basic error handling in your script
Do not use mysql_* but mysqli_* or even better PDO with parameter binding
Output returned string from mysql_error() after your query. In that way you will see actual error.
$this->resultQur = mysql_query($query, $this->connID);
print mysql_error();
mysql_connect returns a resource on success, but a boolean "false" on error.
Your connection attempt probably failed an so the mysql_query won't be successful.
Try something like the following to see what exactly is causing the error.
mysql_connect(..) or die(mysql_error());
Additionally, it seems that the "old" mysql-library get's deprecated in PHP and it's recommended to switch to a more modern version, eg mysqli or PDO.
1 . Firstly put ini_set(‘display_errors’,1);error_reporting(E_ALL|E_STRICT); in your code at the start of the page
2 . Put Try catch around the query and print the Exception message
try { enter code here }catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
3 . Use PDO then the mysql object for query it's a modern and better approach
Use try catch around the the query and try
I am trying to retrieve data from MySQL for a flash application via PHP, but I am having trouble connecting to the server. I created the following .php to test whether it is functioning correctly:
<html>
<body>
<?php
$link = mysql_connect('localhost', 'root', 'password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
if (mysql_query("CREATE DATABASE testphp",$link))
{
echo "Database created";
}
else
{
echo "Error creating database: " . mysql_error();
}
?>
</body>
</html>
When I try and access this from my browser I don't get anything back, it just remains blank? (when I just do localhost/xxx.php is just displays the code, don't know if that's helpful? -aside from entering file path) I tried some other .php files, but I either just get an error back or white web browser screen, as above? How can I test to see whether PHP is setup correctly? I know for sure it is working when I login using the mysql shell and do "show databases;" and testphp is listed, it is currently not. Thanks!
SOLUTION:
Turns out that I had been using the installer while I think there were a lot of assumptions that I was using binaries (or vice versa). Anyhow I have it up and running...expletive, expletive! :) Thanks everyone!
put the following script somewhere within your document root. Then go to that address it should print out a lot of useful information.
<?php
phpinfo();
?>
Your problem is obviously (from the comments) that of a web server setting: you php pages are not being processed.
Try instead this
<html><body>
Hi
<br>
<?php
echo "This line comes from php";
?>
<br>
Bye
</body></html>
When you see three lines in your browser, then you know that you PHP pages are being processed. After then, try the phpinfo() and only after then try to connect to the database. Step by step...
I have a php file that has to be loaded as a web page.
This page is 17Kb in size.
It has a php mysql query script inside.
the problem now, sometimes my mysql_query() lines gives an error.
When being refreshed, it works again.
It just have an error sometimes on that same line.
I check the query string and it was okay, for if that was the problem the error should happen all the time.
any idea?...
I was thinking maybe it was the file that has not been loaded completely.
And if that so, anyone to help me?... thanks.
You can use mysql_error() to get more details about the error that's currently occuring.
Just below the line that triggers the error, add:
echo mysql_error();
"Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource."
Sound like you have a problem with mysql connection. Try the the following code, after you called connect function, try to print out the error message.
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}