MYSQLi queries returning blank [duplicate] - php

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

Related

Cannot connect to mySQL DB [duplicate]

This question already has an answer here:
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(1 answer)
Closed 2 years ago.
Our lovely domain host switched us to a new server, but didn't bother to mention that we need to upgrade all mySQL lines. :-(
We have double checked, but cannot figure out why we can connect to the DB with test code, but not with the live code. The connection works, below, but fails at the "mysqli_select_db" line below.
Can anyone help, please.
}
}if (!(mysqli_connect(CONFIG_SQL_HOST, CONFIG_SQL_USERNAME, CONFIG_SQL_PASSWORD, CONFIG_SQL_DATABASE))) {
exit('mySQL username/password incorrect.');
;
}
if (!(mysqli_select_db(CONFIG_SQL_DATABASE))) {
exit('mySQL Database incorrect.');
;*
}
First try to know the reason.
$conn = new mysqli(CONFIG_SQL_HOST, CONFIG_SQL_USERNAME, CONFIG_SQL_PASSWORD, CONFIG_SQL_DATABASE)
if ($conn->connect_errno) {echo "MySQLError " . $conn->connect_errno . " " . $mysqli->connect_error;exit(0);
}

PHP using wrong password for SQL query [duplicate]

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!

'Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in...' but my query is correct [duplicate]

This question already has answers here:
mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc... expects parameter 1 to be resource
(31 answers)
Closed 9 years ago.
Ok, so I know the question about 'why am I getting this warning with mysql_fetch_array...' has been asked several times, my problem is all the accepted answers state that the reasons the server is spitting this warning out is because the query itself is incorrect...this is not the case with me.
Here is the code below:
$dbhost = "host";
$dbuser = "user";
$dbpass = "pass";
$dbname= "db";
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname) or die ('<META HTTP-EQUIV="Refresh" CONTENT="0;URL=Failed.php?dberror=1">');
$token = mysql_escape_string($_GET['token']);
$query = "SELECT * FROM newuser WHERE token='$token'";
$result = mysql_query($query) or die(mysql_error());
while($row=mysql_fetch_array($result)) {
do stuff...
}
Everything within the 'while' statement is being executed just fine - it makes some changes to the DB which I can validate is happening. More importantly, the query never spits out any error details. I've tried testing for cases where $result===false and asking for error info but it won't return anything then either. From what I can tell, the query string is fine and is not failing.
What am I doing wrong here? Could there any other reason why PHP doesn't like my While parameters other than the SQL statement is bad (which again, I'm convinced it's not bad)?
Also, I know I should be using mysqli/PDO....I plan to switch over to that in the near future, but I'm pulling my hair out just trying to make this work and I have no idea why it won't. It's more of a personal thing at this point...
Thanks for your help, and let me know if you need any additional info. (PHP Version 5.3)
Here is an echo of the query string ($query):
SELECT * FROM newuser WHERE token='6e194f2db1223015f404e92d35265a1b'
And here is a var_dump of the query results ($result): resource(3) of type (mysql result)
$query = "SELECT * FROM newuser WHERE token='$token'";
$result = mysql_query($query) or die(mysql_error());
while($row=mysql_fetch_array($result)) {
do stuff...
}
If the die statement is not executed, $result is OK when you enter the while loop. The problem then is probably that you use $result for a query inside the loop as well, eventually leading to it being set to false.
So for now i can say that the problem is not the mysql_escape_string nether the using of mysql at all neither access privilege from user name and password and what i want to tell you is to test the $result and if it is a resource proceed with your while block like this
if(is_resource($result))
{
while($row = mysql_fetch_array($result))
{//process your code here}
}
and tell me if the code has been also executed :)
The query is not correct according to mysql_. The error you're receiving is telling you that $result is boolean (false).
Where is $token coming from? You best stop using mysql_ functions and use a prepared statement and a bound parameter.
your escape is wrong try this
$token = mysql_real_escape_string($_GET['token']);
instead of $token = mysql_escape_string($_GET['token']);
This extension is deprecated as of PHP 5.5.0, and will be removed in the future.
http://php.net/manual/en/function.mysql-real-escape-string.php

How do I fix Function mysql_list_tables() is deprecated? [duplicate]

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

PHP - Get list of databases names [duplicate]

This question already has answers here:
How to get a list of databases?
(4 answers)
Closed 11 months ago.
How can I get a list of all the MySQL databases that exist on a server using PHP?
$result = mysqli_query($db_conn,"SHOW DATABASES");
while ($row = mysqli_fetch_array($result)) {
echo $row[0]."<br>";
}
$dbcnx = mysql_connect ($dbhost, $dbusername, $dbpassword);
$result = #mysql_query('SHOW DATABASES');
while ($row = mysql_fetch_array($result)) {
print_r ($row)
}
At the MySQL prompt, SHOW DATABASES does what you want.
You can run this command as a query from PDO or the native PHP MySQL library and read the returned rows. Pretend it is a normal select.
You will only see the databases that the account used to connected to MySQL can see.
The MySQL command for this is
SHOW DATABASES
See the manual for more info on the SHOW command
Just use SHOW DATABASES.It will show all the databases present in your MySQL.
Write the SQL query:
show databases

Categories