So this is the error message:
PHP Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead
This is the affected piece of code:
class wdClient {
var $dbLink; // The database link
var $prefix; // Table prefix
var $script; // The script running
/**
* Construct a new directory object.
*/
function wdClient() {
error_reporting(E_ALL ^ E_NOTICE);
$this->prefix = WDDBPREFIX;
// Connect to the database
$this->dbLink = mysql_connect(WDDBHOST, WDDBUSER, WDDBPASSWD);
// Select the database
mysql_select_db(WDDBNAME, $this->dbLink) or die('Sorry, The site is currently unavailable!');
}
where WDDBPREFIX, WDDBHOST, WDDBUSER, WDDBPASSWD, WDDBNAME are already defined in a config file.
I have tried simply using mysqli_connect instead of mysql_connect but it's not working.
Note: Never use MySQL, use this method!
//MySQLi information
$db_host = "localhost";
$db_username = "username";
$db_password = "password";
//connect to mysqli database (Host/Username/Password)
$connection = mysqli_connect($db_host, $db_username, $db_password) or die("Error " . mysqli_error());
//select MySQLi dabatase table
$db = mysqli_select_db($connection, "table") or die("Error " . mysqli_error());
Good luck!
well, as pointed in here http://php.net/manual/en/function.mysqli-connect.php , you should make something like this:
$link = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");
Apparently, in your case it will look like this:
$link = mysqli_connect(WDDBHOST, WDDBUSER, WDDBPASSWD, WDDBNAME);
And then you can continue with your code...
if (!link){
die('Sorry, The site is currently unavailable!');
} else{
// write your SQL here and fetch it
}
Related
i need to know how to declare the database connection using mysql_connection in php
and this is sample of code
$password = "";
$localhost = "localhost";
$username = "root";
$this->connection = mysql_connect($this->$localhost,$this->$username,$this->$password);
Note i am using wamp server
Use mysqli instead. Start like this:
$db = new MySQLi("host", "username", "password", "db");
if(!$db)
{
die("your_error_msg" . mysqli_error($db));
}
$db->set_charset("utf8");
I am trying to copy an array into a table in the test database. I get this fatal error: Call to undefined function pg_insert() in C:\wamp\www\Lessons\GMS-DB.php on line 31. Can u please help me!
Here is my code:
<?php
$servername = "localhost";
$username = "root";
$password = "password";
$database = "test";
// Create server connection
$con = mysql_connect ($servername, $username, $password, $database) or die('cant connect server');
mysql_select_db('test',$con);
// Create database connection
$db_selected = mysql_select_db('test',$con);
if (!$db_selected) {
die ('Can\'t use foo : ' . mysql_error());
}
//-----------------------------------------------------------------------
// Specify directory
$dir = 'C:/Users/Desktop/data';
$files1 = scandir($dir);
$data = array_diff($files1, array('.', '..')); //remove the dots or periods
// print_r($data);
// put in a string
$matstring = implode("','",$data);
$matstring="".$matstring."";
$table_name = `test table 1`;
$res = pg_insert ($con , $table_name , $data);
if ($res) {
echo "POST data is successfully logged\n";
} else {
echo "User must have sent wrong inputs\n";
}
?>
You have two different kinds of databases there. One is MySQL and another is PostgreSQL. Your using a mysql database so use a mysql query to insert.
You need to use mysql query
mysql_query("INSERT INTO $table (column_name, column_name2) VALUES('$value', '$value2')");
Also, you are using mysqli syntax to connect to db. In mysql, the syntax is
$con = mysql_connect ($servername, $username, $password);
mysql_select_db('foo', $con);
And I strongly suggest you to use mysqli. Ancient mysql is no longer supported!
Here's the code in MySQLi
$db = new mysqli("host", "user", "password", "database");
$db->query("INSERT INTO $table (column_name, column_name2) VALUES('$value', '$value2')");
When should I use MySQLi instead of MySQL?
MySQLi Query
I have a problem with my code. I'm trying to add new post to the table events. I'm confused because I have used this code in other place on the same website (but it was using mysqli_query to register new user). mysqql_error returns "No database selected"
This is the code:
<?php
$add_title = $_POST['add_title'];
$add_happen = $_POST['add_happen'];
$add_created = date('Y-m-d');
$add_content = $_POST['add_content'];
$add_author = $_POST['add_author'];
//connect to
//localhost
$db_host = "localhost";
$db_username = "root";
$db_password = "";
$db_dbname = "zhp2";
$db_con = mysql_connect($db_host, $db_username, $db_password, $db_dbname);
$query = "
INSERT INTO events ( title, happen, created, content, author )
VALUES ( '$add_title', '$add_happen', '$add_created', '$add_content', '$add_author') )
";
$retval = mysql_query($query, $db_con);
if(! $retval ){
die('Could not enter data: ' . mysql_error());
}
else{
echo "Entered data successfully\n";
}
mysql_close($db_con);
//header('Location: ../../index.php?link=events');?>
I've tried to fix it using trial and error method playing with different combinations both mysql_query and mysqli_query
You are confusing mysql_connect and mysqli_connect functions in the way you pass those parameters. In your example:
$db_con = mysql_connect($db_host, $db_username, $db_password, $db_dbname);
you are passing a fourth parameter which is the database name but that wont work as you should only pass the three first (host,username,password) and then call mysql_select_db():
$db_con = mysql_connect($db_host, $db_username, $db_password);
mysql_select_db( $db_dbname, $db_con );
In mysqli which is the BETTER way of doing it since mysql_ functions are very vulnerable and being deprecated from php you could pass four elements like here:
$db_con = mysqli_connect($db_host,$db_username, $db_password, $db_dbname) or die("Error " . mysqli_error($link));
which is close to what you are trying to do, but in a correct mysqli_ way.
Well then, you need to select the database! ;) The fourth parameter of mysql_connect() is not the database name. You need to do this separate of connecting to the MySQL server.
Using mysql_select_db() function:
$db_host = "localhost";
$db_username = "root";
$db_password = "";
$db_dbname = "zhp2";
$db_con = mysql_connect($db_host, $db_username, $db_password );
mysql_select_db( $db_dbname, $db_con );
And of course all the obligatory warnings about SQL injection, sanitizing your data, deprecation of mysql_* functions.
You need to select which database to connect to using the mysql_select_db function:
// make $db_dbname the current db
$db_selected = mysql_select_db($db_dbname, $db_con);
if (!$db_selected) {
die ("Can't use $db_dbname : " . mysql_error());
}
See the PHP manual for more info: http://php.net/manual/en/function.mysql-select-db.php
I want to connect to mysql database using php and following is my configuration file.
<?php
$host = "localhost";
$db = "payroll"
$username ="root";
$password = "";
mysql_connect ($host,$username,$password);
mysql_select_db($db,$username);
?>
but when I run my program it gives me this error:
SQL error: No database selected SQL errno: 1046
SQL: select language, admin from user where username='admin' and password='abc123'
What's wrong with my code?
You forgot a semicolon here
$db = "payroll";
^--- Here
Don't forget to enable error reporting on your code. This is how you do it.
This(mysql_*) 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. Switching to PreparedStatements is even more better to ward off SQL Injection attacks !
Switch to Prepared Statements..
A kickstart example..
<?php
$dsn = 'mysql:dbname=payroll;host=localhost';
$user = 'root';
$password = '';
try
{
$dbh = new PDO($dsn, $user, $password ,array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e)
{
echo 'Connection failed: ' . $e->getMessage();
}
Read more here on PHP Manual
$mysqlhost="localhost"; // MySQL-Host
$mysqluser="user"; // MySQL-User
$mysqlpwd="password"; // Password
$connection=mysql_connect($mysqlhost, $mysqluser, $mysqlpwd) or die
("CouldnĀ“t connect");
$mysqldb="database"; // Your Database
mysql_select_db($mysqldb, $connection) or die("Couldnt select database");
I always use this. Here you get every errormessage you need to find your error.
Try this
<?php
$host = "localhost";
$db = "payroll"
$username ="root";
$password = "";
$con = mysql_connect ($host,$username,$password);
mysql_select_db($db,$con);
?>
I'm following a tutorial on the net on PHP and MySQL.
I'm using Linux. I'm trying to establish a connection to a database but it is not working.
I have my database:
create database test;
create table user(name text, pass text);
insert into user values('john', '123');
and then my php:
<?php
$_host = "localhost";
$_dbuser = "root"
$_dbpass = "";
$_dbname = "test";
#mysql_connect("$_host", "$_dbuser", "$_dbpass") or die("could not connect");
#mysql_select_db("$_dbname") or die("no database");
echo "connection stablished";
?>
And the output of my file is just a blank tab on the browser.
What should I do to solve this? What am I doing wrong?
Thank you in advance. I'm very new to web programming.
$_host = "localhost";
$_dbuser = "root"
$_dbpass = "";
$_dbname = "test";
$link = mysqli_connect($_host, $_dbuser, $_dbpass, $_dbname);
if (!$link) {
die('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
echo "connection stablished";
Now...
Use mysqli_* functions as mysql_* are deprecated.
You will need $link variable later for queries.
As you're newbie read this: How can I prevent SQL injection in PHP?
Prepared statements FTW! Remember!