I am trying to select data from a local database on my PC using PHP but i am getting this error when i run 127.0.0.1/test.php which is what the file is called.
error:
Fatal error: Uncaught Error: Call to undefined function mysql_connect() in
C:\xampp\htdocs\test.php:11 Stack trace: #0 {main} thrown in
C:\xampp\htdocs\test.php on line 11
Here is my PHP script:
<?php
$servername = "Windows local servername";
$username = "Windows username";
$password = "windows password";
$dbname = "dbname";
// Create connection
$conn = mysql_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn){
die('error connecting to database');
}else{
mysql_select_db(dbname, $conn);
}
$sql = "SELECT UserId, UserEmail, UserPassword FROM User";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["UserId"]. " - UserEmail: " . $row["UserEmail"]. " " . $row["UserPassword"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
I have looked around online but i cant figure out what is wrong here. I can run a hello world php script and it works so i am assuming its a connection issue with the database but what have i missed here? Thanks
EXTRA:
i have:
Try to run
phpinfo()
And look for the mysql extensions, in ubuntu you can install them by typing
sudo apt-get install php-pdo-mysql
Anyway the instruction you're trying to use was deprecated in HP 5.5.0 and deleted in PHP 7.0.0 so may be this is why its missing... depending on you PHP version, you could try with the mysqli extension instead like:
$mysqli = mysqli_connect("example.com", "user", "password", "database");
$res = mysqli_query($mysqli, "SELECT 'Please, do not use ' AS _msg FROM DUAL");
$row = mysqli_fetch_assoc($res);
echo $row['_msg'];
You could get more info on the actual way of performing this with PHP7 at http://php.net/manual/en/mysqli.quickstart.dual-interface.php for instance
From the docs
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.
You'll probably want to use Microsoft's drivers for PHP for SQL Server rather than the ODBC driver in PDO.
In place of
$conn = mysql_connect($servername, $username, $password, $dbname);
use
$connectioninfo=array("Database"=>"dbname", "UID"=>"userid", "PWD"=>"password", );
$con = sqlsrv_connect($server,$connectioninfo);
Related
I am working on a new project and I am getting an error when I connect with my DB I don't know why it is happening my DB name and user name is all correct but why I am getting this error
Fatal error: Uncaught Error: Call to undefined function mysql_query() in C:\xampp\htdocs\treeview\treeview.php:9 Stack trace: #0 {main} thrown in C:\xampp\htdocs\treeview\treeview.php on line 9
Code:
<?php
mysql_connect('localhost', 'root');
mysql_select_db('test');
$qry = "SELECT * FROM treeview_items";
$result = mysql_query($qry);
$arrayCategories = array();
while ($row = mysql_fetch_assoc($result)) {
$arrayCategories[$row['id']] = array("parent_id" => $row['parent_id'], "name" => $row['name']);
}
I'll assume you are on PHP 7+, which means that mysql_query is deprecated. Please use PDO or mysqli.
More info on that function's man page: https://www.php.net/manual/en/function.mysql-query.php
You can obtain the connection to your database through:
$connection = new mysqli($hostname, $db_username, $password, $db_name);
if ($connection->connect_error){
$error = "Failed to connect to db: " . $connection->connect_error;
// Do whatever you want with $error
}
$connection->query("SET NAMES utf8mb4"); // mostly, people use utf8 instead
I can not recognize the SQL Server command in XAMPP 1.8.1 and PHP 5.4.7, this code had already used it and it worked before but now it doesn't.
I have already tried to put the php_mssql.dll dll and still don't recognize the command:
$con = mssql_connect('187.164.1.2/base','pag','123') or die('Could not connect to the server!');
mssql_select_db('aguacom') or die('Could not select a database.');
I expected it to connect to the other server's database.
You are trying to connect to MS SQL Server using MSSQL PHP extension (mssql_ functions), but this extension is not available anymore on Windows with PHP 5.3 and removed in PHP 7.0.0.
What you can do is to install PHP Driver for SQL Server (sqlsrv_ functions). You need to download:
The appropriate version of this driver - for PHP 5.4 you need version 3.2 (32-bit or 64-bit depending on the PHP version)
The appropriate ODBC driver.
Note, that MSSQL PHP extension (php_mssql.dll) and PHP Driver for SQL Server (php_sqlsrv_54_ts.dll) are two different PHP extensions.
Example using mssql_ functions:
<?php
$server = "187.164.1.2/base";
$username = "pag";
$password = "123";
$database = "aguacom";
$conn = mssql_connect($server, $username, $password);
if ($conn === false) {
echo "Unable to connect. ".mssql_get_last_message()."</br>";
exit;
} else {
echo "Connected.</br>";
}
mssql_select_db($database, $conn);
// ...
mssql_close($conn);
?>
Example using sqlsrv_ functions:
<?php
$server = "187.164.1.2/base";
$username = "pag";
$password = "123";
$database = "aguacom";
$connectionInfo = array(
"UID" => $username,
"PWD" => $password,
"Database" => $database
);
$conn = sqlsrv_connect($serverName, $connectionInfo);
if ($conn === false) {
echo "Unable to connect. ".print_r(sqlsrv_errors(), true)."</br>";
exit;
} else {
echo "Connected.</br>";
}
// ...
sqlsrv_close($conn);
?>
you should enable mssql in xampp
things you need [download these files]: https://www.microsoft.com/en-us/download/details.aspx?id=20098
Add downloaded files in php.ini like this way
extension=php_sqlsrv_56_ts.dll
extension=php_pdo_sqlsrv_56_ts.dll
restart apache and then check
<?php
echo "<pre>";
print_r(PDO::getAvailableDrivers()); ?>
credits goes to rray
In PHP 7, As I try to connect to the Beanstalk I have set up, I receive the error:
Fatal error: Uncaught Error: Class 'mysql' not found in C:\Apache24\htdocs\php_file.php, Stack trace: #0 {main} thrown in C:\Apache24\htdocs\php_file.php on line 41.
This server uses AWS RDS running MYSQL, and I am using apache 2.4 to localhost (for testing).
The code I'm using is:
$servername = "MY BEANSTALK CONNECTION";
$username = "Username";
$password = "PSSWD";
$dbname = "DBNAME";
$conn = new mysql($servername, $username, $password, $dbname);
$sql = "SELECT * FROM column";
$result = $conn->query($sql);
$conn->close();
My updated code uses mysqli, but is still getting the same error.
From PHP documentation mysql class is deprecated since version 5.5 and removed in version 7. that's why you're receiving the class not found error.
Try using mysqli. The PHP documentation has good examples on this. Look at example 2. I've pasted part of the example in this post for you.
https://www.php.net/manual/en/function.mysql-connect.php
<?php
$mysqli = new mysqli("example.com", "user", "password", "database");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: " . $mysqli->connect_error;
}
$res = $mysqli->query("SELECT 'choices to please everybody.' AS _msg FROM DUAL");
$row = $res->fetch_assoc();
echo $row['_msg'];
?>
Use mysql_connect($servername, $username, $password) instead.
hello everybody I'm new to coding world and I need your help I would like to connect to a server with xampp (3.2.2) and php version (7.2.4) windows 10... I have installed the drivers php_pdo_sqlsrv_72_ts_x86.dll and php_sqlsrv_72_ts_x86.dll to php.ini but also to php\ext
and when I am trying to connect to a server with this code doesn't respond
thank you very much for your time
$servename = "new";
$username = "user";
$password = "tree";
$database = "tree";
$connectionInfo =array("Database" =>"tree","USER" => "user", "PWD" => "tree");
$conn=sqlsrv_connect($servename,$connectionInfo);
if($conn)
{echo"connection established";}
else
{echo"connection failure";die (print_r(sqlsrv_errors(),true));}
Error message:
Fatal error: Uncaught Error: Call to undefined function sqlsrv_connect()
Here you can use it with help of PDO,
Please check below code and try to replace with your original data
<?php
$dsn = "sqlsrv:Server=**servernamehere**;Database=**Db name here**";
$conn = new PDO($dsn, "", "");
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$sql = "SELECT * FROM table_name";
foreach ($conn->query($sql) as $row) {
print_r($row);
}
?>
Folks, Im trying to run a basic php script in cmd prompt which connects to sql server 2012 on my local machine, its gives me the following error..."PHP Fatal error: Call to undefined function mssql_select_db() in c:...appscript.php on line 16"
Here is my script, im not very familiar with php but have been playing around with it and im unsure if my script is correct..
<?php
$Server = "";
$User = "";
$Pass = "";
$DB = "";
//$SQLKEY = "";
//connection to the database
//$dbconn = sqlsrv_connect($Server, $User, $Pass)
$connectionInfo = array("UID" => $User, "PWD" => $Pass, "Database" => $DB);
$conn = sqlsrv_connect( $Server, $connectionInfo);
//or die("Couldn't connect to SQL Server on $Server");
//select a database to work with
$selected = mssql_select_db($DB, $connectionInfo)
or die("Couldn't open database $myDB");
//declare the SQL statement that will query the database
$query = "SELECT name from test ";
//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);
?>
Be great if someone could help or least give me a few pointers.
It seems you are using wrong functions. You should use only functions from http://www.php.net/manual/en/function.sqlsrv-begin-transaction.php
It seems there is no need to select db (there is no sqlserv_select_db function).
You should change your mssql_query to sqlsrv_query and the same for other functions and you should look at PHP manual because some functions may missing.
You need to enable the MSSQL extension.
http://www.php.net/manual/en/mssql.installation.php (dead link)
Last working version captured by archive.org:
https://web.archive.org/web/20200920214312/http://www.php.net/manual/en/mssql.installation.php