I have been trying to setup a Sql database, but I can't get it to connect in php. here's what my code looks like:
$conn = mysql_connect("my_sql_here.net","root",'my_password_here');
print $conn;
mysql_select_db("my_database",$conn);
$created = mysql_query("SELECT * FROM `inventory`);
if(!$created) {
print "error: ";
print mysql_error();
}
print json_encode($created);
mysql_close($conn);
When I run this code, I get:
error: Access denied for user 'dom710'#'localhost' (using password: NO)false
Why is it tryng to connect to localhost? and why is trying to use root as the password?
I am super confused.
Consider using PDO to make a connection:
// Establish a connection
$host = 'my_sql_here.net';
$name = 'my_database';
$user = 'root';
$pass = 'my_password_here';
$dsn = "mysql:dbname=$name;host=$host;charset=utf8";
$conn = new PDO($dsn, $user, $pass);
// Perform your query
$query = 'SELECT * FROM `inventory`';
$statement = $conn->prepare($query);
$statement->execute();
$resultSet = $statement->fetchAll();
// Do stuff with your $resultSet
You have configured safe mode. This is why it tries to connect to localhost.
https://dev.mysql.com/doc/apis-php/en/apis-php-function.mysql-connect.html
$server "In SQL safe mode, this parameter is ignored and value 'localhost:3306' is always used."
$username "In SQL safe mode, this parameter is ignored and the name of the user that owns the server process is used."
And as someone stated in comments you shouldn't use this function because it's deprecated.
Related
I created a function to connect to the db in php:
function fn_connect($is_write = false)
{
$host = '127.0.0.1';
$db = 'name_db';
if ($is_write) {
$user = 'user_write';
$pwd = 'password_write';
} else {
$user = 'user_read';
$pwd = 'password_read';
}
$conn = new mysqli($host, $user, $pwd, $db);
if ($conn->connect_error) {
die('The database is not available. Please, try again later.');
}
return $conn;
}
When I need to connect, im calling it (and closing it) like this
$conn = fn_connect(true);
$stmt = $conn->prepare($q);
$stmt->execute();
....
$stmt->close();
$conn->close();
I thought it will be a good idea to verify if the connection exists, that way, I guess, I save connecting to the db every time for nothing, like this:
if (!isset($conn)) $conn = fn_connect(true);
$stmt = $conn->prepare($q);
$stmt->execute();
....
$stmt->close();
if (isset($conn)) $conn->close();
Is this a good idea, a good practice? Should I jut connect normally and let Apache/PHP do the rest (no need to verify nothing)?
It is good practiose and good style to check the connection, before letting php try to get or send data.
What is not good style is to use die in your connection, because it leaves a broken page.
Better is to design the page so that page still works when the connection is broken.
I have found record from login table but my mysql query is now executing.
following is my code.
$sqlQuery = "SELECT * FROM ".$table."
WHERE mobile_number='".$mobile_number."' AND
password='".base64_encode($password)."'";
// End
$select = mysql_query($sqlQuery);
$result = mysql_num_rows($select);
echo "<pre>Rest";
print_r($result);
It's always return 0 but same query is working in Phpmyadmin dashboard.
When i used mysql_error() function with mysql_query like following
$select = mysql_query($sqlQuery) or die ('Error updating database: '.mysql_error());
It's given error : Access denied for user ''#'localhost' (using password: NO)
Following is my connection code..
$dbname = "######";
$host = "localhost";
$user = "#####";
$password = "#####";
$connection = mysql_connect($host,$user,$password) or die("Error in database connection.");
if (!$connection)
{
return false;
}
if(!mysql_select_db($dbname, $connection))
{
return false;
}
I don't know why i faced the problem if anyone have idea about this pleas help me on this.
Thank You!!
Guess you gotta connect to your SQL Server with username and password. Error message says you didn’t even pass a username.
Simple example:
$link = mysql_connect('example.com:3307', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
…
// do your stuff here
Please be sure to – at least – secure your parameters be escaping them with mysql_real_escape_string() for instance:
http://php.net/manual/en/function.mysql-real-escape-string.php
Or, even better, use PDO:
$stmt = $pdo->prepare('SELECT * FROM your_table WHERE mobile_number = :mobile_number');
$stmt->execute(array('mobile_number' => $mobile_number));
…
You should check the host, user name, and password in the configuration file and make sure that the information is consistent with the information given by the MySQL server administrator.
Reset the password and try it out:
Enter set password for 'root'#'localhost'=password(' your password ');Then restart the mysql service.Enter mysql-u root-p
I have a PHP site that I uploaded in 000webhost. It needs a database to store data. But when I try to sign in it didn't sign in. When I check the connection it turns out the connection was fine. Still not able to access the database. So I wrote a small script to check database access and it failed. I have a table named songs which contains some songs.
Here is the script :
<?php
ob_start();
session_start();
$host = 'localhost';
$user = 'xxxxxxx';
$pass = 'xxxxxxx';
$db = 'xxxxxxx';
$timezone = date_default_timezone_set("Asia/Kolkata");
$con = new PDO("mysql:host=$host;dbname=$db", $user, $pass);
if($con)
{
echo "Connection success";
$query = mysqli_query($con, "SELECT * FROM songs");
if($query){
$tb = mysqli_fetch_array($query);
print_r($tb);
}
else {
echo " failed db access";
}
}
else {
echo " connection failed";
}
?>
The details have been kept hidden for security reasons.
The above script gives the following output:
Connection success failed db access
It doesn't work, because you're using PDO for connection and mysqli to query. Change your connection to mysqli
$con = new mysqli($servername, $username, $password, $dbname);
or change your query to PDO
$query = $con->query("SELECT * FROM songs");
The output you are showing is misleading, failed db access. As per below code, database was connected successfully and you were executing query, which didn't executed it seems and flow going to else block.
if($query){
// your code
}
else {
echo " failed db access";
}
You need to check,
If you are using same user credentials and has access to the table.
If same table exists
If you have provided correct database details where you have created tables.
==Updated==
As I mentioned earlier, user might not have sufficient privilege to access songs table. To GRANT access, you need to execute following SQL
GRANT ALL PRIVILEGES ON *.* TO '<username>'#'localhost; WITH GRANT OPTION;
I try to make a simple IOS app that can connect to mysql database and read one table. But my php code does't work and really have no idea why, it's seems correct to me. The database is in a raspberry phpmyadmin server and the server works great.
I will put my code here and please tell me what's wrong.
<?php
$host = "192.168.2.193";
$db = "produtos";
$user = "root";
$pass = "1234";
$connection = mysql_connect($host, $user, $pass);
if(!$connection)
{
die("Database server connection failed.");
}
else
{
//attempt to select the database
$dbconnect = mysql_select_db($db, $connection);
//check to see if we could select the database
if(!dbconnect)
{
die("Unable to connect to the specified database!");
}
else
{
$query = "SELECT * FROM produtos";
$resultset = mysql_query($query, $connection);
$records = array();
//loop throught all our records and add them to our array
while ($r = mysql_fetch_assoc($resultset))
{
$records[] = $r;
}
echo json_ecode($records);
echo $resultset;
}
}
?>
Based on the question:
use mysqli_connect rather than mysql_connect because mysql_connect is deprecated and will not work someday. Also what is the the error you are getting? change your die() statement to something more helpful die(mysqli_error($connection));
Based on your comment:
That error would suggest that you either A) don't have the right IP address or B) there is a network issue between your host server and the SQL server, is this code running on the same server that is hosting the SQL database? if so then you can probably just use localhost for your $host
I have a simple project and that is to create a function that will check for mysql and odbc connection. I'm already done in creating the function for mysql, here's my sample code:
function check() {
$serverName = 'localhost';
$userName = 'root';
$password = '123';
$db = 'sample';
$conn = mysql_connect($serverName, $userName, $password);
mysql_select_db($db, $conn);
$trans = 'SELECT * FROM Labels';
$trans_result = mysql_query($trans, $conn);
if(!$trans_result) {
die(mysql_error());
} else {
echo "connected";
}
}
Well this one works for me when checking for the mysql connection. Now, my question is, is it possible to create something like this for checking my odbc data source connection? So that would be like
$conn = odbc_connect("spmuse1","" ,""); # Open connection.
$trans = "SELECT French FROM Labels";
$trans_result = odbc_exec($conn, $trans);
if(!$trans_result) {
echo "error?";
} else {
echo "connected";
}
You know what I mean? When I use this code, I always have 2 this error
Warning: odbc_connect() [function.odbc-connect]: SQL error: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified, SQL state IM002 in SQLConnect
Warning: odbc_exec(): supplied argument is not a valid ODBC-Link resource
Please help! Thanks.
First you need to decide vendor of odbc driver, I hope below example will works for you
<?php
// Configure connection parameters
$db_host = "server.mynetwork";
$db_server_name = "Dev_Server";
$db_name = "Dev_Data";
$db_file = 'c:\dbstorage\dev.db';
$db_conn_name = "php_script";
$db_user = "dbuser";
$db_pass = "dbpass";
$connect_string = "Driver={Adaptive Server Anywhere 8.0};".
"CommLinks=tcpip(Host=$db_host);".
"ServerName=$db_server_name;".
"DatabaseName=$db_name;".
"DatabaseFile=$db_file;".
"ConnectionName=$db_conn_name;".
"uid=$db_user;pwd=$db_pass";
// Connect to DB
$conn = odbc_connect($connect_string,'','');
// Query
$qry = "SELECT * FROM my_table";
// Get Result
$trans_result= odbc_exec($conn,$qry);
if(!$trans_result) {
echo "error?";
} else {
echo "connected";
}
?>
I spent several days looking for a simple answer, and came up with this, which works for me:
if (#odbc_connect("DBName","un","pw",SQL_CUR_USE_ODBC) == FALSE){
echo "Database does not exist";
} else {
$connection=odbc_connect("DBName","un","pw",SQL_CUR_USE_ODBC);
echo "Database exists";
}
The # suppresses the basic error if the database does not exist, so the connection try will just return false. Of course if the connection is good, then it creates the connection object.