mysql vs mysqli error when running php code in wamp stack - php

( mysql vs mysqli error (advait) )
Hi All, See code. This code runs on my localhost WAMP Win7 php ver 5.5.12 but it gives an error:
---
Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in C:\wamp\www\get_data02.php on line 9
Call Stack
# Time Memory Function Location
1 0.0010 135216 {main}( ) ..\get_data02.php:0
2 0.0010 135680 mysql_connect ( ) ..\get_data02.php:9
---
I tried replacing mysql with mysqli but that just gave more errors. How do I fix this? Thanks,
<?php
// 1. Enter Database details
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$dbname = 'iamdb_copy_04';
// 2. Create a database connection
$connection = mysql_connect($dbhost,$dbuser,$dbpass);
if (!$connection) {
die("Database connection failed: " . mysql_error());
}
// 3. Select a database to use
$db_select = mysql_select_db($dbname,$connection);
if (!$db_select) {
die("Database selection failed: " . mysql_error());
}
$query = mysql_query("SELECT * FROM users WHERE city = 'city' ");
while ($rows = mysql_fetch_array($query)) {
$f_name = $rows['first_name'];
$address_01 = $rows['address_01'];
$city = $rows['city'];
echo "$f_name<br>$address_01<br>$city<br><br>";
}
?>

There are two options to get out from this error.
Set display_errors off in php file using ini_set("display_errors",0) after start of php <?php.
Replace mysql with mysqli in mysql_connect, mysql_select_db,mysql_query, mysql_fetch_array.
Let me know if you still face any problem and also comment your error.

Related

ini file not being ready properly

I keep getting this error:
Warning: mysql_connect(): Unknown MySQL server host 'elephant' (1) 4
My ini file is this:
[Database]
DBHostName = "localhost"
DatabaseName = "elephant"
DBUserName = "elephant"
DBPassword = "practice"
[Config]
ConfigTableName = "stage2_config"
Skin="purple"
My file that reads the ini file and gives the error is this on the line with the double bold stars. I have no idea what is going on here and why it says the host isn't localhost.
<?php
$gv_SystemTest = "<h2>system.php has been included</h2>";
$gv_SystemFileInfo = pathinfo(__FILE__);
$gv_SystemDirectory = $gv_SystemFileInfo['dirname'] . '/';
$gv_SystemINIpath = $gv_SystemDirectory . '';
$gv_SiteGlobals = parse_ini_file($gv_SystemINIpath . $_SERVER['WIA_SYSTEM_INI'], true);
function echoContent($p_ContentID) {
global $gv_SiteGlobals;
$fv_dbLink = mysql_connect($gv_SiteGlobals['Database']['DBHostName'],
$gv_SiteGlobals['Database']['DBUserName'],
$gv_SiteGlobals['Database']['DBPassword']);
**mysql_connect($gv_SiteGlobals['Database']['DatabaseName']);**
$fv_TheQuery = "select *from wia_content where content_id = $p_ContentID";
The function mysql_connect does not allow to select a database as you tried on this line.
mysql_connect($gv_SiteGlobals['Database']['DatabaseName']);
To connect and select a database, you need to use the function mysql_select_db in addition to mysql_connect.
$link = mysql_connect('host','username','password');
$selected = mysql_select_db('database_name', $link);
http://php.net/manual/en/function.mysql-select-db.php
Dont use mysql use only mysqli or pdo
I think you want to select db
to select db you should be mysql_select_db()
mysql_select_db('foo', $link);
your error in this line
mysql_connect($gv_SiteGlobals['Database']['DatabaseName']);
it should be of the form
mysql_connect('localhost', 'mysql_user', 'mysql_password');

WAMP PHP Error: Can not connect to MySQL

I need help with problem that appear on my website while I tried to connect to mysql.
so, this is the error:
Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in C:\wamp\www\sites\mysite\core\connection.php on line 9
Call Stack
Time Memory Function Location
1 0.0007 163008 {main}( ) ..\index.php:0
2 0.0012 165376 include( 'C:\wamp\www\sites\mysite\core\connection.php') ..\index.php:1
3 0.0015 166400 mysql_connect ( ) ..\connection.php:9
( ! ) Warning: mysql_connect(): in C:\wamp\www\sites\mysite\core\connection.php on line 9
Call Stack
Time Memory Function Location
1 0.0007 163008 {main}( ) ..\index.php:0
2 0.0012 165376 include( 'C:\wamp\www\sites\mysite\core\connection.php') ..\index.php:1
3 0.0015 166400 mysql_connect ( ) ..\connection.php:9
MySQL Error: Accטs refusי pour l'utilisateur: 'root'#'#localhost' (mot de passe: OUI)
the php code to connect that I wrote, is:
<?php
session_start();
$dbhost = "localhost";
$dbname = "users";
$dbuser = "root";
$dbpass = "root";
mysql_connect($dbhost, $dbuser, $dbpass) or die("MySQL Error: " . mysql_error());
mysql_select_db($dbname) or die("MySQL Error: " . mysql_error());
?>
cause you are using deprecated extension so
Use mysqli or PDO (The mysql extension is deprecated)
try
mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
By default, your MySQL credentials for WAMP are:
username: root
and no password.
$dbhost = "localhost";
$dbname = "users";
$dbuser = "root";
$dbpass = "";
And as other users mentioned, try to use mysqli or pdo as mysql is deprecated, but it's not your issue here.
Good luck!
If your using the default user of phpmyadmin and didn't set a new password than the password would be an empty string (no password), unless you set your password to root.
Your code is correct, although you should look into mysqli when you feel like you can handle it.
mysql_connect — Open a connection to a MySQL Server.
This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. Alternatives to this function include:
mysqli_connect()
PDO::__construct()

Issues with PHP connection script to MSSQL database

Good morning,
I am quite new to php and I am trying to create a connection to a MSSQL server, I've been able to do it through MYSQL php connection but what I thought would be a simply change to MSSQL is proving to be much harder than expected.
The below code is basically what I am using after much googleing and search in this website this is what i've come up with:
<?php
$Server = "127.0.0.1";
$User = "BOB123";
$Pass = "BOBPASS";
$DB = "BOBDB";
//connection to the database
$dbconn = mssql_connect($Server, $User, $Pass)
or die("Couldn't connect to SQL Server on $Server");
//select a database to work with
$selected = mssql_select_db($DB, $dbconn)
or die("Couldn't open database $myDB");
//declare the SQL statement that will query the database
$query = "SELECT CustomerName from tblCustomer ";
//execute the SQL query and return records
$result = mssql_query($query);
$numRows = mssql_num_rows($result);
echo "<h1>" . $numRows . " Row" . ($numRows == 1 ? "" : "s") . " Returned </h1>";
//display the results
while($row = mssql_fetch_array($result))
{
echo "<br>" . $row["name"];
}
//close the connection
mssql_close($dbconn);
?>
As you can see the above script is very basic and there are very similar ones knocking around on the web could anyone help in connecting to the server this script doesn't seem to want to connect. I've changed the log on details as you'd probably know.
Thanks
Kris
You have a typo on:
$dbconn = mssql_connect($Server, $User, $Pass);
Should be:
$dbconn = mysql_connect($Server, $User, $Pass);
You're typing mysql wrong on each mysql_ function you create, change all mssql_ to mysql_
Note:
You shouldn't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. Learn about prepared statements instead, and use PDO or MySQLi.
#Daniel Gelling Doesn't look like a typo, looks like he is trying to connect to Microsoft SQL Server using mssql. You are correct about the API being outdated however.

Parse error: syntax error, unexpected 'mysql_select_db' (T_STRING)- cannot see my error?

I am trying to connect to a simple database on XAMPP using php- I know the database exists as I can see it on PHPMyAdmin and have created a table called students and added some data.
I have tested that I can run a simple test.php file ( from the htdocs folder on the XAMPP drive) and get a response. I cannot spot what is stopping me connecting to my database- can anyone help?
<?php
// connect to the database
$user_name = "root";
$password = "";
$database = "computing";
$host_name ="localhost";
$con=mysql_connect($host_name,$user_name,$password);
mysql_select_db($database);
//check connection
echo "Connection opened";
mysql_close($con);
?>
Could you please try the following code if it works?
<?php
// connect to the database
$user_name = "root";
$password = "";
$database = "computing";
$host_name = "localhost";
$con = mysqli_connect($host_name ,$user_name ,$password,$database) or die("Error " . mysqli_error($con));
//check connection
echo "Connection opened";
mysql_close($con);
?>
mysql commands are not going to be supported in future releases, so it would be best perhaps to use mysqli or PDO connections.
Also PDO uses parameters (the syntax might take a bit to make sense), so it is great to reduce risk from SQL Injections.
Mysqli: http://php.net/manual/en/function.mysqli-connect.php
PDO: http://php.net/manual/en/class.pdo.php
The code above should work. Maybe try mysql_select_db($database, $con);

PHP Can't Connect to MS SQL Express 2008

I'm struggling to connect to my MS SQL Express 2008 edition from my PHP files, but it seems virtually impossible.
I've found several guides/notes on teh intarweb solving the issues, but none of these have helped me any further.
I've taken a script from another site and entered my database information, but I still get errors:
<?php
$myServer = "localhost";
$myUser = "demo_basic";
$myPass = "1234";
$myDB = "demo_basic";
//connection to the database
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
or die("Couldn't connect to SQL Server on $myServer");
//select a database to work with
$selected = mssql_select_db($myDB, $dbhandle)
or die("Couldn't open database $myDB");
//declare the SQL statement that will query the database
$query = "SELECT nitid, nisname ";
$query .= "FROM navitems";
//execute the SQL query and return records
$result = mssql_query($query);
$numRows = mssql_num_rows($result);
echo "<h1>" . $numRows . " Row" . ($numRows == 1 ? "" : "s") . " Returned </h1>";
//display the results
while($row = mssql_fetch_array($result))
{
echo "<li>" . $row["nitid"] . $row["nisname"] . "</li>";
}
//close the connection
mssql_close($dbhandle);
?>
I get this error:
Warning: mssql_connect()
[function.mssql-connect]: Unable to
connect to server: localhost in
C:\inetpub\vhosts\dexterholding.dk\httpdocs_rip\sql.php
on line 8 Couldn't connect to SQL
Server on localhost
You can see for yourself at: http://www.dehold.net/_rip/sql.php
Any ideas?
I'm running Windows Server 2008 with PLESK and PHP5 as FastCGI.
I found this guide which actually made it work for me:
http://samsami2u.wordpress.com/2008/06/30/how-to-connect-mssql-with-php/
With MSSQL, your server name will look like this: machinenameoraddress\serverinstancename
and example would be 192.168.14.201\MSSQLEXPRESS or TESTMACHINE\MYTESTDBSERVER.
It could be that the user account does not have permission to access SQL Server.
Although looking at your code you are using SQL auth and not Windows authentication, presumably this account is set up in SQL Server and it is configured to allow SQL Auth?

Categories