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());
}
Related
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.
I have a server and it performs actions every 10 seconds, but sometimes, it returns the error 'mysql has gone away'.
I wonder when this error is returned for the page to be refreshed.
I tried the following way but it did not work:
$remote_db = mysql_pconnect($remote_db_host, $remote_db_user, $remote_db_pass) or die (mysql_error());
if(!$remote_db) {
//error on connect
echo '<meta http-equiv="refresh" content="1">';
}
mysql_select_db($remote_db_name, $remote_db) or die (mysql_error());
That's a timeout message. Your PHP code took too long to do anything with the MySQL server and therefore it went away.
The issue is that the persistent connection you are using have died.
So, stop using mysql_pconnect and switch to mysql_connect.
For most cases it is actually speedier to do a normal connect than to try to use a persistant connection.
(And as a side-note: you should really take a look at PDO. mysql_ usage is strongly discouraged.)
I'm trying to use PHP to connect to SQLite. I created a database by importing a CSV file into the tables for three tables. However, I'm unable to connect using the following code:
$dbhandle = sqlite_open('db/pokedex.db', 0666, $error);
if(!$dbhandle) die ($error);
This returns the following error:
Warning: sqlite_open() [function.sqlite-open]: file is encrypted or is not a database in /pokedex/configpokedexdb-sqlite.php on line 12
file is encrypted or is not a database
Googling told me I might have a version mismatch. Despite finding some SQLite3 mentions in my phpinfo(), I decided it might still be a problem so I tried the following suggested code:
try
{
//connect to SQLite database
$dbhandle = new PDO("sqlite:db/pokedex.db"); //sqlite:VPN0.sqlite
// echo "Handle has been created ...... <br><br>";
}
catch(PDOException $e)
{
echo $e->getMessage();
echo "<br><br>Database -- NOT -- loaded successfully .. ";
die( "<br><br>Query Closed !!! $error");
}
After which I received the following error:
Warning: sqlite_exec() expects parameter 1 to be resource, object given in /home/rawdco81/public_html/pokedex/index-sqlite.php on line 53
Before this, I tried running new PDO("sqlite:VPN0.sqlite"); which was what the site provided, but that was obviously wrong because it didn't point to my .db file at all. You'll see this piece of code in the comments beside the function call.
I'm having a hard time just connecting to the database...What is the proper way to do this?
Also, I'm running PHP Version 5.2.13.
EDITED: I pasted the wrong error message in the wrong place.
Now that I look more closely.., I think you are connecting successfully in the second code segment! You shouldn't be using sqlite_exec in tandem w/ PDO though; those are two different PHP interfaces into SQLite.
The reason the first bit of code bombed is because the legacy library doesn't support PDO v3. The second bit of code is bombing once you try to run sqlite_exec against the PDO object I presume.
I bet if you put a var_dump($dbhandle); after the try/catch you'll see you've got an initialized PDO object.
Just read up on using PDO to run queries and you should be golden!
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
Few days back i suddenly came up with getting empty result, I was surprised and then check out the mysql_error function and then got this 2006: Mysql server has gone away. The problem is that it doesn't comes everytime, but sometimes only for the same query.
I have checked out many questions here at stackoverflow as well as many blogs on the web, but I found no possible solution for this error
Today, I wrote this function for that and the error gones away,
$conn = mysql_connect($this->host, $this->dbUser, $this->dbPass);
$db_select = mysql_select_db($this->dbName, $conn);
for($i=0;mysql_error($conn) != 2006;$i++)
{
$result = mysql_query($sql, $conn);
if($i > 10){break;}
}
I wanna ask the professionals that if it it is the proper way of getting rid of this kinda situation or it is the wrong method for that
This error will occurr when the MySQL server has been restarted / unavailable. It could be happening on a specific query if the query is causing some kind of problem (can't say more without knowing what the query does).
Either way, this error is valid and shouldn't be ignored. You need to find out WHY it is being raised in the first place.
You can use the mysql_ping function to check if the server is available before running a query rather than catching the error.