I found a system that can generate a report I would like to study this system but when I try to generate the system the login was OK but when I put the password and username this error comes out:
Fatal error: Call to undefined function mysql_select_db() in
C:\xampp\htdocs\inventory\inventory\db.php on line 8
Can anyone else tell me what is the problem in this system?
image
I'm currently using XAMPP V3.2.2
If you are using PHP 7 within your XAMPP then it wont work because that code is too old. mysql_select_db was removed in PHP 7.
http://php.net/manual/en/function.mysql-select-db.php
You will need to install PHP 5 to use that software.
Edit: Pratik Solanki is correct also. The database is being connected using mysqli so depending on the other code in the system you can either change the database connect statement to mysql_connect and use PHP 5 (old mysql connector not recommended) or change all the database statements to use mysqli instead in which case no PHP changes are needed (recommended)
You forgot to include the database while connecting using mysqli_connect function.
In your code, you declared a $mysql_database but you didn't use that.
Code:
<php
$mysql_hostname = "localhost";
$mysql_user = "root";
$mysql_password = "";
$mysql_database = "liveedit";
$bd = mysqli_connect ($mysql_hostname, $mysql_user, $mysql_password, $mysql_database) or die ("Opps something went wrong ");
?>
You connected to database via mysqli module of php and you are trying to select your database via mysql module of php.
Correct way to use it would be like this :
mysqli_connect(dbhostname,dbusername,dbpassword,dbname)
mysqli can be used with the mysql native drivers and mysql can be used via the default libraries provided.
Both are different and the only small mistake you made was making connection with mysqli and selecting db with mysql.
Solution to this is making the whole connection along with selection of db via mysqli function
Related
I'm making a user login system with php. Obviously to do that I need to have databases and I do using PHP MyAdmin. The connection in my php to mysql didn't work at first so I looked it up online and found this code:
<?php
$mysql_host = 'localhost';
$mysql_user = 'root';
$mysql_pass = '';
$mysql_db = 'a_database';
if(!mysql_connect($mysql_host, $mysql_user, $mysql_pass)||!mysql_select_db($mysql_db)){
die(mysql_error());
}
?>
And this would be enough to connect to SQL. So here is my question: how can it be that you don't have to actually connect to the database in order for it to work? Because to my knowledge of php all that is doing is checking if that works, it isn't actually connecting. So is this code correct? Or do you need to add
mysql_connect($mysql_host, $mysql_user, $mysql_pass)||!mysql_select_db($mysql_db))
to this code to make it work? Thanks in advance.
(I'm using XAMPP btw)
mysql_connect tries to connect to to database. If it's false (connection failed) - your script dies. Otherwise - connection established succesfully and we need to check second condition
mysql_select_db selects db. It is false - your script dies too, if not - db selected successfully.
I'm working for an e-commerce that has the db on phpmyadmin. In another website I'd like to connect to that database. I have password, username and db name, so I'm using this "connection string":
<?php
$nomehost = "localhost";
$nomeuser = "user";
$password = "pass";
// connection
$conn=mysql_connect($nomehost,$nomeuser,$password);
if (!$conn) exit ("Error connection<br>");
// db name
if (!mysql_select_db("db_name",$conn)) exit("Error db name<br>");
?>
The result is "Error db name". What can I do? Have I to set some oprion in the phpmyadmin?
First of all:
this error is caused by the fact that you are selecting the wrong database in your MySql server. Is your db called db_name???
EDIT: based on the comments you are making: is the server that hosts the php page the same as the mysql server?
Then:
phpmyadmin is just a tool to connect and handle MySql databases and is not a database server itself.
Last but most important:
you are using a deprecated library (mysql) in php to connect to a MySql server. Please consider moving to mysqli or better to PDO
I am using mysql. And i want to connect with database using aura sql.
<?php
$connection_factory = include '/Aura/scripts/instance.php';
$connection = $connection_factory->newInstance(
// adapter name
'mysql',
// DSN elements for PDO; this can also be
// an array of key-value pairs
'host=localhost;dbname=db_aura',
// username for the connection
'root',
// password for the connection
''
);
$result = $connection->fetchAll('SELECT * FROM tbl_test');
?>
The above code shows the error
Parse error: parse error in C:\wamp\www\1\Aura\src\Aura\Sql\Connection\AbstractConnection.php on line 65
Finally I got it...
The code given above in my question is correct. Error occured due the version of my php i used. PHP version above 5.4 only support this. Now am using php 5.4.12 and aura sql is able to connect with mysql and working well.. Thanks.
Perhaps this is the connection string to connect to an sql server
<?php
$myServer = "localhost";
$myUser = "your_name";
$myPass = "your_password";
$myDB = "examples";
?>
//connection to the database
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
or die("Couldn't connect to SQL Server on $myServer");
but i do not know what to input in the $myServer, $myUser and $myPass
if I wanna use windows authentication
This is my Server details kindly help me please
Microsoft SQL Enterprise Manager
Microsoft Corporation
Version: 8.0
If you have PHP 5.3 or later, you can no longer use the ms_sql extension, according to the PHP documentation page. I'm not sure of the extent of that, but you may want to consider using PDO instead. It allows for a generic way to access various database types, including MSSQL. To connect to a MSSQL database using PDO, you first need SQLSRV, the driver, which you can download from Microsoft here. To connect, you would use the following:
$handle = new PDO("sqlsrv:Server=$server;Database=$database", $username, $password);
Above, and to answer the main part of your question, $server is probably localhost, and your username and password can be accessed and changed from within the Microsoft SQL Server access panel. I believe you can use "sa" for $username and "" for $password. For $database, just put in the name of the database you want to connect to.
To query:
$queryRef = $handle->query($query);
To read results, declaring $results as a two-dimensional associative array, the first being the result number, the second being the column name:
$results = $queryRef->fetchAll(PDO::FETCH_ASSOC);
So $results[3]['id'] would be the value of the column 'id' of the third result.
You can find more examples on the PHP documentation page for PDO here. Much of this was taken from my previous answer here.
Your server is usually localhost, and your username and password could be changed and viewed by following this tutorial:
http://blog.sqlauthority.com/2007/12/29/sql-server-change-password-of-sa-login-using-management-studio/
I am trying to connect to a MySQL server using PHP's 'mysql_connect()' function, but the connection fails. This is my code:
$con = mysql_connect("example.net", "myusername","") or die("Could not connect: ".mysql_error());
I placed this code inside a PHP script, which I try to open using a web browser (the script is stored on a remote host which has PHP enabled) but it doesn't work. It doesn't return the die error either. Echoing something before the $con successfully outputs in the browser, whereas nothing outputs after that line. If I type:
mysql -h example.net -u myusername
from a remote machine, I could connect to the DB without any problem and do queries and other modifications.
Update :
I also tried this after some suggestion, but no improvement:
<?php
$usern = "myusername";
$dbh = new PDO('mysql:host=servername.net;dbname=test', $usern, "");
echo $usern;
?>
What operating system is the remote host running PHP using? Perhaps MySQL isn't enabled in php.ini. Also, please don't use mysql_* functions for new code. They are no longer maintained and the community has begun the deprecation process (see the red box). Instead, you should learn about prepared statements and use either PDO or MySQLi. If you can't decide which, this article will help you. If you care to learn, this is a good PDO tutorial.
Have you tried using PDO or the MySQLi interface? If you're trying to learn PHP, you should not be using the mysql_* functions regardless. See if you can access the database by using a line similar to this:
$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
If you need more detailed documentation, this code comes directly from the documentation itself.
EDIT: Also, try using PDO's error checking functionality. This example creates a database connection using PDO and tries to perform a simple query. It doesn't use prepared statements or any of those features, so it's not production-ready code (i.e. *don't just throw this into your code without understanding how to improve it) and you'll need to edit it to include a SELECT query that's relevant to your database, but it should at least tell PDO to provide more information about the errors it encounters.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$dbhost = "localhost";
$dbname = "test";
$dbuser = "root";
$dbpass = "admin";
// database connection
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);
// query
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT * FROM booksa";
$q = $conn->query($sql) or die("ERROR: " . implode(":", $conn->errorInfo()));
$r = $q->fetch(PDO::FETCH_ASSOC);
print_r($r);
?>
Is the php file located on the same server as the mysql database, if so you might have to use 'localhost' as the first argument for mysql_connect() instead the external address.