Wampserver Upgrading issue - php

I recently updated my wampserver 2.0 to wampserver 2.5.
And while i am running the php smarty code i am getting this error.
"Fatal error: Class 'DB' not found in
C:\wamp\www\livehrm.new\product\common.php on line 63"
I think it might be a pear issue of old wampserver.
Please help me
//session_start();
require_once 'DB.php';
$dbHost = $dbconfig['db_hostname'];
$dsn1[0] = array('type'=>'DB', 'dsnstuff'=>"mysql://$dbUser:$dbPassword#localhost/$dbName"); $dbdsn = "mysql://$dbUser:$dbPassword#$dbHost/$dbName";
$db = DB::connect($dbdsn);
if (DB::isError($db)) {
die ($db->getMessage());
}
?>

MySQL extension is deprecated as of PHP 5.5.0, and is not recommended for writing new code as it will be removed in the future. Instead, either the mysqli or PDO_MySQL extension should be used. Ref: http://php.net/manual/en/function.mysql-select-db.php
Update your DB.php with following code
<?php
$con = mysqli_connect("localhost","root","");
$db="livehrm";
if (!$con)
{
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con, $db) or die ("Database connection error:\n" . mysqli_error($con));
?>
There is not Class found in you provided code of file DB.php. I will update my answer when you send class code of DB

Its not the prefered solution, but this should get you working again.
I strongly recommend that you do in fact convert your code from using the mysql_* extension ot use either the mysqli_* or PDO instead as eventually this approach will not work as the developers of PHP will actually no longer provide the mysql_* extension.
But you can remove the warning messages by changing your php.ini configuration like this :-
Edit php.ini
Find this line
error_reporting = E_ALL
and change it to
error_reporting = E_ALL & ~E_DEPRECATED
Or you can just add this PHP code at the top of your db.phpscript if you dont have access to your php.ini file
error_reporting( E_ALL ^ E_DEPRECATED );
If you are using Virtual Hosts you can actually add this to the specific VH for the site that is still using the old mysql_* extension therefore not affecting other site you may be developing/maintaining
Unfortunately in a VH definition you have to use an integer value.
<VirtualHost....>
. . .
php_value error_reporting 24575
</VirtualHost>

Related

Use mysqli or PDO instead?

I just uploaded my very old scripts on my hosting account. When I login to my website, I found this note. I read about mysqli but still don't work on changing Anyone please help!
$con = mysql_connect('localhost', 'user_log', '12345');
$db = mysql_select_db('name_log',$con);
The entire ext/mysql PHP extension was officially deprecated in PHP v5.5.0 and removed in PHP v7.There are two other MySQL extensions that you can consider: MySQLi and PDO_MySQL, either of which can be used instead of ext/mysql.
it's possible to suppress deprecation errors by setting error_reporting in php.ini or in your each pages or config file to exclude E_DEPRECATED.
error_reporting = E_ALL ^ E_DEPRECATED
or
you can use
error_reporting(0)
Change your code From
$con = mysql_connect('localhost', 'user_log', '12345');
to
$con= mysqli_connect($db_host,$db_user,$db_pass,$db_database);

MySQL error message along with PHP error message

<?php
//database connectivity
$connect_error='Sorry We could not able to connect to the database';
mysql_connect('localhost','root','') or die($connect_error);
mysql_select_db('beehive_intwebpage') or die ($connect_error);
?>
We have this in setup this in our localhost. When there no connection with the database we get the error along with the error message.
Warning: mysql_connect(): No connection could be made because the target machine actively refused it. in D:\core\database\connect.php on line 3
Sorry We could not able to connect to the database
How to show only the message and not the default sql error.
Thanks!
First: Don't use mysql_*-methods anymore, they're deprecated. Instead use mysqli or PDO.
For your error, disabling the error-reporting according to this documentation should help.
Just add error_reporting(0); to the beginning of your file.
Hey default errors are caused due to php itself, you can turn them off in the php configuration file and you have to turn them off really because watching errors give an attacker knowledge of how your site works and your sites internal structure. So just turn off the error_reporting in php configuration

Compiled PHP 7 missing mysql extension in WordPress

I have built PHP 7 with a configuration that worked for a previous version of PHP.
Now my WordPress websites get the message:
Your PHP installation appears to be missing the MySQL extension which is required by WordPress.
Other websites using mysqli do work. What am I missing?
I've also included all .so files with mysql in the name:
extension=dba.so
extension=mysql.so
extension=mysqli.so
extension=mysqlnd_mysql.so
extension=mysqlnd_mysqli.so
extension=mysqlnd.so
extension=pdo.so
extension=pdo_mysql.so
extension=pdo_odbc.so
extension=odbc.so
PHP 7 has removed mysql_* completely.
You need to use PDO or mysqli. Wordpress seems not to support this.
mysql_* functions got deleted in PHP 7.0 update your code to mysqli or PDO
Also take a look at prepared statements if you are handling user input. To reduce the chance of SQL injections
An example of mysqli connection string:
<?php
$mysqli = new mysqli("localhost", "user", "password", "database");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
?>
An example of pdo connection string:
<?php
$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
?>
Note:
That mysqli example handles a connection error
As has been mentioned elsewhere, the ext/mysql functions have been removed. We've been talking about this for some time.
ext/mysql was built for MySQL 3.23 and only got very few additions since then while mostly keeping compatibility with this old version which makes the code a bit harder to maintain.
If you're hell-bent on putting them back in, you can add them back to PHP 7 by using the ext/mysql PECL Library
It's important to note that Wordpress 3.9 or later supports mysqli
In WordPress 3.9, we added an extra layer to WPDB, causing it to switch to using the mysqli PHP library, when using PHP 5.5 or higher.
Check if the Wordpress still using the Mysql extension that was removed in PHP7.
http://php.net/manual/en/migration70.removed-exts-sapis.php
The Mysqli and PDO extensions were kept. This is the reason your other websites are working.
This issue is caused by php 7.1.0-dev.
I built another one with the same configuration version 7.0.0 and the issue was resolved.
This has nothing to do with WordPress since it will automatically try to use MySQLi when MySQL is not found. At least in WP 4.4.
On Ubuntu, I fixed this error by running
sudo apt-get install php-mysql
And then restarting my server (caddy, but you might be using apache or nginx).
source

mysql_connect error

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).

php with postgresql database

i wanna to connect to postgresql database using php but it display a blank screen when submit and doesnt execute the query
Thanks in advance
Update: this is my code
function execute_query($dbName,$query){
$host = "localhost";
$user = "postgres";
$pass = "";
$db = "test";
echo "before create connection";
$con = pg_connect ("host=$host dbname=$db user=$user password=$pass");
echo "After connection is created";
if (!$con) {
echo "not connected";
// die('Could not connect: ' . mysql_error());
}
$result = pg_query($con, $query);
pg_close($con);
return $result;
}
The output:
display the message "before connection" but doesn't display the message "After connection is created" or "not connected".
The problem is likely a PHP error that’s getting recorded to a log file somewhere. Locate the log file, or enable showing the log errors on your page by using the following at the top of your script:
ini_set('display_errors', '1');
error_reporting(E_ALL | E_STRICT);
This is a short-term solution and can’t be used in deployment (where you want to set display_errors to 0). For a long-term solution, you really want to locate the Apache or PHP error log and tail it.
To try to find the error log, run the following script:
<?php phpinfo(); ?>
In the Configuration > PHP Core section, look for error_log. If that’s not set, you can set it in your php.ini file. All errors will be recorded to that file, even if you have display_errors set to 0.
Add a php file to your server and put this in that file:
<?php
echo phpinfo();
?>
When you open that file from the browser, check if postgres support is setup for php.
you should something like this on the page:
pgsql
PostgreSQL Support enabled
PostgreSQL(libpq) Version 8.2.3
Multibyte character support enabled
SSL support disabled
Active Persistent Links 0
Active Links 0
Try checking your web server's error log (for instance, /var/log/apache2/error.log is a common location for the Apache2 log) and see if there is an error reported from PHP there.
You may want to set the php error reporting level in your ini file if this is a local development server.
It seems, that your php does not have postgresql support, cf. http://us.php.net/manual/en/ref.pgsql.php:
Note: Not all functions are supported
by all builds. It depends on your
libpq (The PostgreSQL C client
library) version and how libpq is
compiled. If PHP PostgreSQL extensions
are missing, then it is because your
libpq version does not support them.

Categories