mysql_connect error - php

In the following code I see echo1 statement, after which I do not see anything printed on the UI.The username and password is correct. But PHP doesn't seem to connect to MySQL. Don't even see the die statement what am I doing wrong. After mysql_connect is encountered the rest of the code doesn't work:
<?php
echo "echo1==========";
$con = mysql_connect("localhost","root", "xxxx123") or die("Error connecting to database");
echo "+++++++++ echo2";
echo $con;
mysql_close($con);
?>

You should be mising an error. Add :
ini_set('display_errors', 1);
error_reporting(E_ALL);
at the beggining of your script

No output means a fatal error. The only possible fatal error is "undefined function mysql_connect (unless something's really messed up somewhere). This means the mysql library is not installed, or it might just not be enabled in the php.ini file.
Check said file, and while you're at it turn error_reporting on.

If you use your code just like this then it's vulnerable for SQL Injection. I would strongly recommend using mysql_real_escape_string as you insert data into your database to prevent SQL injections, as a quick solution or better use PDO or MySQLi.
If you are going to use mysql_* then I'd recommend reading the PHP manual chapter on the mysql_* functions,
where they point out, that this extension is not recommended for writing new code. Instead, they say, you should use either the MySQLi or PDO_MySQL extension.
I also checked mysql_connect and found a weird regularity which is - if you use " on mysql_connect arguments, then it fails to connect and in my case, when I was testing it, it happened just described way, so, please try this instead:
$con = mysql_connect('localhost','username','password');
Replace " to ' as it's shown in the PHP Manual examples and it might work!
EDITED
For those who downvote - TRY first! I tested it on my server with " and it gave me an error: Warning: mysql_connect(): Access denied for user. I use PHP version 5.4.6!
Login to your server with SSH and run php --modules - if you don't see mysql in the list - then it's the reason of your fatal error.

The issue was that the ph5-mysql driver was not installed .Installed it and got it working..Now mysql_connect() function works..Thanks for all the help

For Tomcat 7, the default dir for php.ini is actually C:\windows. So modified (enabling modules, extensions etc.) php.ini should be copied to C:\windows. Then run phpinfo();. It should work fine even on Tomcat 7 along side PHP 5.x. I had this problem and struggled a lot when I wanted the mysql statements to work.
Even after following all instructions from answers in [779246] (Run a php app using tomcat?), mysql statements didn't work (Tomcat 7, PHP 5.2.16, PECL 5.2.5, MySql 5.6 on Windows 7). So I started looking into "phpinfo();" output carefully and used solution as mentioned in earlier paragraph. Please be sure to enable mysql dll extension and also PDO extensions in php.ini if you would like to use PDO (which is future of plain mysql_* methods in PHP).

Related

db2_connect to as400 with php and wamp server

I have been trying to connect to a ibm db2 database but it seems impposible. The as400 is in a different server than the one running php.
Everytime I do a db2_connect I get the following error:
Fatal error: Call to undefined function db2_connect()
How can I make this function work?
Tip: I've already tried with odbc and it was a lost of time, but I'm open to suggestions on that path as well.
[EDIT]
I finally changed to java... it was impossible with php...
Check your php.ini file and make sure it has the DB2 extension enabled. http://www.php.net/manual/en/install.pecl.php
Try this link:
http://www.theregister.co.uk/2006/08/09/db2_udb_part2/
Deals with:
Installing the PHP DB2 extension,
Creating a connection,
Obtaining a result set
Also gives as an alternative the PDO option.
But as for DB2 on the AS400, am not sure if something still needs to be installed on the
AS400 for this to work?
Anyway, have used the ODBC Client Access with no problems at all. What difficulty did you run into?

mysql_real_escape_string stopped working when I moved my code to another server

The following code works perfectly fine in my local xampp installation (Windows 7), but when I ported it over to a Win2K8 R2 server, the mysql_real_escape_string piece does not work. When I comment it out, it works fine. I am pretty sure this has something to do with the php.ini file but cannot pinpoint what it is. Perhaps my code should have been written differently to begin with.
function add_asset($asset_type_ID, $org_ID, $asset_desc, $asset_cost, $asset_value, $purchase_date) {
global $db;
$asset_desc = mysql_real_escape_string($asset_desc);
$query = "INSERT INTO assets
(asset_ID, asset_type_ID, org_ID, asset_desc, asset_cost, asset_value, purchase_date)
VALUES
(DEFAULT, '$asset_type_ID', '$org_ID', '$asset_desc', '$asset_cost', '$asset_value', '$purchase_date')";
$add_asset = $db->exec($query);
$last_asset_ID = $db->lastInsertId();
return $last_asset_ID;
Specifically, when the record gets entered into mysql, the asset_desc field is blank.
Thank you!
UPDATE: After looking through the PHP error log, I found the following:
[function.mysql-real-escape-string]: Access denied for user ''#'localhost' (using password: NO)
Looks like whatever credentials you are using to connect to the database on your dev system are failing on your server.
mysql_real_escape_string() requires an active MySQL connection in order for it to work correctly; if you aren't connected to the database, it will fail.
Check wherever in your code you are calling mysql_connect(); from the error message, it seems like your dev system is relying on the runtime default values for the Mysql extension.
Alternatively, the connection resource might not be accessible to mysql_real_escape_string() on the server's system. From the documentation for mysql_real_escape_string() (emphasis mine):
link_identifier
The MySQL connection. If the link identifier is not specified, the last link opened by mysql_connect() is assumed. If no such link is found, it will try to create one as if mysql_connect() was called with no arguments. If no connection is found or established, an E_WARNING level error is generated.
Perhaps it's possible that your dev server is configured such that calling mysql_connect() with no arguments does establish a connection to the MySQL database server. The most likely reasons for this are either dev MySQL server allows anonymous connections, or php.ini specifies valid default credentials.
However, this fails on production, most likely because the production MySQL server does not allow anonymous connections, and no default credentials are specified in php.ini.
Check over your code for situations where it is possible to arrive at the call to mysql_real_escape_string() without mysql_connect() getting called first (note that establishing a connection with PDO doesn't count in this case).
Since you're using PDO, consider switching to prepared statements, which will eliminate the need for mysql_real_escape_string() entirely.
You're probably thinking of "magic quotes"
http://php.net/manual/en/security.magicquotes.disabling.php
This function has been deprecated in php 5.3
http://php.net/manual/en/function.mysql-escape-string.php
You will have to rewrite your code, ideally use PDO from now on and use prepared statements.
Quick Answer:
Connect to database first
Then use the mysql_real_escape_string.
Not the other way around......

mysql_connect not defined

I'm testing a connection to mysql 5.1, with php5 on apache2 (windows).
I have made a regular php test connection page from a tutorial but I'm getting the error "mysql_connect" not defined.
In eclipse for php, I see the function defined under the language library. However when I make a page:
<?php
phpinfo();
?>
I don't see mysql functions or anything there; can someone tell me why? I thought PHP5 at the least would support MySQL, no?
The MySQL extension is probably not enabled. If you look through the php.ini file, you should be able to find the appropriate line. phpinfo() will show the location of the php.ini file.
there is php documentation
http://php.net/docs.php
mysql_connect
http://php.net/manual/en/function.mysql-connect.php
however mysql extension should be enabled in php.ini
(linux: /etc/apache2/)

php cant define mssql_connect function

I am trying to make a connection between PHP and a SQL server's database. I'm running XAMPP, and I deleted the ; from extension=php_mssql.dll to enable the PHP extension by modifying the php.ini file.
When I run my script, however, PHP throws an error that says the function mssql_connect() doesn't exist.
What can I do to fix this?
So far xampp comes with MySQL and have not include Microsoft SQL Server (MSSQL)
As a general approach:
In the php.ini is an option
display_startup_errors = Off
Set that to On and call PHP (maybe from console).
It should tell you what went wrong then.

Call to undefined function sqlite_escape_string()

i am using sqlite_escape_string($str) to prepare sql statements for future use.
however, when i migrated my code to a new server, it says:
Call to undefined function sqlite_escape_string()
seems the Sqlite and everything is working in the new server, but i have error even for a sqlite_open().
Anyways, i just need to escape my $str to a sqlite safe sql statments, i dun even need to write to sqlite from PHP, so an
That sounds like the SQLite plugin is not enabled for PHP. Write <?=phpinfo()?> in a .php file, load it up on your server and see if SQLite is mentioned in the loaded plugins.
You need to uncomment the extension=php_sqlite.dll line by removing the semi colon right now your pdo_sqlite is enabled and sqlite_escape_string is a function of sqlite extension and sqlite_pdo will not work untill php_sqlite is enabled
I found the ans myself.
It is due to my server is a Centos 5.4, and sqlite support for php is not there.
The same case should exist in REL5 also.
see the 1st and 2nd latest comment in the link below:
http://www.php.net/manual/en/sqlite.installation.php
I solved it by recompiling my php and added the sqlite.ini file
For those who recently upgraded to 5.4+, note that the php docs say, this method is not available 5.4+
(PHP 5 < 5.4.0, PECL sqlite >= 1.0.0)
So you would either need to manually compile it in, or look at PECL.

Categories