This question already has an answer here:
PHP MySQLi doesn't recognize login info
(1 answer)
Closed 9 years ago.
So I have this SQL query and when I connect the php and MySQL it works but then when I try to run it, the query has an error saying it's using the wrong username/password. But the only place I entered the username and password was when connecting, and then I used the right information and it worked? Here is the code and the error:
<?php
$con = mysqli_connect("Correct Host","Correct Username","Correct Password","Correct Database");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysql_query("SELECT username, email from Profiles");
mysqli_close($con);
?>
Error:
Warning: mysql_query() [function.mysql-query]: Access denied for user 'wronguser'#'wronghost' (using password: NO) in /home/username/public_html/register.php on line 8
You're using the wrong function to query. You connected via mysqli but then you try and query with mysql. Change your code to:
mysqli_query($con, "SELECT username, email from Profiles");
You can't switch from mysqli_* extension to mysql_* like that. Use mysqli_query.
You've used MySQLi to connect to the database but you used MySQL to run the query. Take note, in PHP, MySQL and MySQLi are different. MySQLi is the newer and improved version of MySQL. So use mysqli_query() instead of mysql_query since you've connected to the database using MySQLi.
Hope this helps!
Related
This question already has answers here:
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(1 answer)
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 5 years ago.
So I had my website working but the hoster forced me to migrate, so I had to backup all I had and change it to the new host. The code was working but since I changed I cant get this code working. The connection to the database seems fine.
The code had to be changed from mysql to mysqli
All I get from mysqli_num_rows or mysqli_query is blank.
I checked the encoding and its UTF-8.
The query works fine on phpmyadmin
Since I only have ftp access to the publish folder Im not sure what options i have to find the problem.
$link = mysqli_connect($hostname,$username, $password) OR DIE ("Unable to connect to database! Please try again later." . mysqli_connect_error());
mysqli_set_charset($link, "utf8");
mysqli_select_db($dbname, $link);
$result = mysqli_query($link, "SELECT * FROM $usertable ORDER BY Date DESC");
//echo "result:" . $result; returns blank
echo "results:" . mysqli_num_rows($result); returns blank
This question already has an answer here:
The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead [duplicate]
(1 answer)
Closed 8 years ago.
My host recently upgrade the PHP version and a certain part of my website now shows the following error:
Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in url/structure/here on line 49
That is referencing the below code:
function DBConnect() {
$this->connectCount ++;
//echo "$this->connectCount<br>";
if ($this->dbType == 'mysql') {
$dbConnect = mysql_connect($this->dbHost, $this->dbUser, $this->dbPasswd) or die ("MySql Connection Failed: " . mysql_error());
mysql_select_db($this->dbName, $dbConnect);
}
if ($this->dbType == 'postgresql') {
$dbConnect = pg_connect("host=$this->dbHost port=$this->dbPort dbname=$this->dbName user=$this->dbUser password=$this->dbPasswd") or die ("PostgreSQL Connection Failed: " . pg_errormessage($dbConnect));
//$dbConnect = pg_pconnect("host=$this->dbHost port=$this->dbPort dbname=$this->dbName user=$this->dbUser password=$this->dbPasswd") or die ("PostgreSQL Connection Failed: " . pg_errormessage($dbConnect));
}
return $dbConnect;
}
I'm aware the fact that this is because the current way my site connects to MYSQL is now outdated in the new version of PHP but does anyone know how I would update the above code to make this work?
The easier way is to use mysqli_connect(). The syntax is very similar to what you would had with mysql_connect(),which means the changes in your code will be minor and easy to make.
Pdo would be the safest, but if you are trying to get you site back on quickly, the mysqli_* commands will achieve that.
Google (or check on stackoverflow) mysql vs mysqli. You ll find plenty of examples.
Hope this helps.
Good luck
--
Sorry after re-reading i see you asked what needs to be change on your source code. Afraid i cannot help right now as i am responding from a mobile phone :(
I have been working for days now and I am at a dead end. After talking with GoDaddy support I am positive that I have the correct hostname, username/password when I run the script but it still cannot get past die().
Ultimately I am attempting to pull a single question from a database. I have combed this website but nothing i found seems to answer my question. Please help.
<?php
$hostname='localhost';
$username='username';
$password='password';
$dbname='qod';
$usertable='Questions';
$userfield='question';
mysql_connect($hostname,$username, $password) or die ("<html><script language='JavaScript'>alert('Unable to access the Question of the Day! Please try again later.'),history.go(-1)</script></html>");
mysql_select_db($dbname);
# Check If Record Exists
$query = 'SELECT $.userfield FROM $.usertable ORDER BY RAND() LIMIT 1';
$result = mysql_query($query);
if($result){
while($row = mysql_fetch_array($result)){
$name = $row[$yourfield];
echo "Name: ".$name;}
}
?>
You are using dots for your SELECT variables where there shouldn't be any.
SELECT $.userfield FROM $.usertable, then calling them with:
$usertable='Questions';
$userfield='question';
Remove them from your SELECT and use proper error reporting techniques such as :
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of code
and
or die('Could not connect: ' . mysql_error());
also a dollar sign in [$yourfield] for your column name; remove it also.
You should be using quotes: I.e.: ['yourfield'] - yourfield being the column name in your table that you wish to show.
Footnotes:
mysql_* functions deprecation notice:
http://www.php.net/manual/en/intro.mysql.php
This extension is deprecated as of PHP 5.5.0, and is not recommended for writing new code as it will be removed in the future. Instead, either the mysqli or PDO_MySQL extension should be used. See also the MySQL API Overview for further help while choosing a MySQL API.
These functions allow you to access MySQL database servers. More information about MySQL can be found at » http://www.mysql.com/.
Documentation for MySQL can be found at » http://dev.mysql.com/doc/.
Use mysqli_* with prepared statements, or PDO with prepared statements.
This question already has answers here:
Warning: mysql_query(): 3 is not a valid MySQL-Link resource
(4 answers)
Closed 9 years ago.
I am new to programming and following online tutorial.
I got an error please help.
Connected successfully
Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/www/divitiae.net.co/make_my_tables.php on line 22
below is line 22
include_once("php_includes/db_connect.php");
$query = mysql_query($db_connect, $tbl_users);
if ($query === TRUE) {
echo "<h3>user table created OK :) </h3>";
} else {
echo "<h3>user table NOT created :( </h3>";
}
The error you are getting means that the $db_connect variable you are passing to the mysql_query function is not a MySQL connection object, as would be returned by the mysql_connect function.
Your php_includes/db_connect.php should contain something like:
$db_connect = mysql_connect('hostname', 'username', 'password');
mysql_select_db('databasename');
This will connect to the database on server hostname using the username and password provided. It then selects the database databasename as the database to perform operations on.
Warning!
You really, really, REALLY should not use the mysql_* set of functions because they are deprecated and you should use either the mysqli_* functions or implement PDO, as those are more flexible, safer, supported, and have better features.
This question already has answers here:
what is the alternate function mysql_list_tables() in php 5
(5 answers)
Closed 9 years ago.
I'm trying to add a voting system for my website, and I get this error:
Deprecated: Function mysql_list_tables() is deprecated in /home/yokoscap/public_html/vote/classes/vote.class.php on line 11
Access denied for user 'yokoscap'#'localhost' (using password: NO)
I'm new to PHP and MySQL, so if you could dumb it down/help me fix it it'd be great.
Here's the code paste, I thought it was too big to go in here so I uploaded to pastebin.
Did you check the manual? There is a complete example on a replacement for it:
<?php
$dbname = 'mysql_dbname';
if (!mysql_connect('mysql_host', 'mysql_user', 'mysql_password')) {
echo 'Could not connect to mysql';
exit;
}
$sql = "SHOW TABLES FROM $dbname";
$result = mysql_query($sql);
if (!$result) {
echo "DB Error, could not list tables\n";
echo 'MySQL Error: ' . mysql_error();
exit;
}
while ($row = mysql_fetch_row($result)) {
echo "Table: {$row[0]}\n";
}
mysql_free_result($result);
use this sql query
show tables from dbname
hope this will help
The mysql_list_tables() function has been deprecated since PHP 4.3.7 (!) because it's entirely too specific.
To get a list of tables in the current database, run a SHOW TABLES query. It's a perfectly normal query; you don't need a special function just to do that.
I says here in the manual
This 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. See also MySQL: choosing an API guide and related FAQ for more information. Alternatives to this function include:
According to mysql_list_tables ...changelog secton
This function became deprecated from version 4.3.7
Use SHOW TABLES