Mysqli getting values from table error - php

I connected my website to a database and the connection was succesful, but when i tried to get values from a table in the database i got this error:
Warning: mysql_query(): Access denied for user ''#'localhost' (using
password: NO) in /home/username/public_html/root/connecti.php on line
17
Warning: mysql_query(): A link to the server could not be established
in /home/username/public_html/root/connecti.php on line 17
Connected successfully
Line 17:
$sql = mysql_query("SELECT * FROM 'tblusers' LIMIT 0, 30");

My educated guess is that you stablish the connection using mysqli_connect() but then attempt to use mysql_query(). As per the docs:
mixed mysql_query ( string $query [, resource $link_identifier = NULL
] )
link_identifier
The MySQL connection. If the link identifier is not specified, the last link opened by mysql_connect() is assumed. If no such link is
found, it will try to create one as if mysql_connect() had been called
with no arguments. If no connection is found or established, an
E_WARNING level error is generated.
If you check the manual page for mysql_connect() you'll see that it's possibly trying to use system-wide credentials you haven't set.
You cannot mix database extensions to your liking. Stop using the legacy extension and stick to mysqli.

Look at your SELECT query and you will see that quoting around the table name with single quote is the issue since DB engine then considering it as string literal and not a DB object.
You mean to escape it using backtique
"SELECT * FROM `tblusers` LIMIT 0, 30"

Related

mysql_query(): 3 is not a valid MySQL-Link resource? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Warning: mysql_query(): 3 is not a valid MySQL-Link resource
I don't know why the $GUconnection resource isn't working on queries since I set it up as a global variable. The db connection is
$GUconnection = mysql_connect(serverip, username, password);
#mysql_select_db(dbname, $GUconnection) or die('Cannot connect to the database.');
The following query is located in an include file that is included in the file containing the mysql connection above:
global $GUconnection;
$GUresult = mysql_query("SELECT field FROM `tablename` WHERE field = 'hey' LIMIT 1", $GUconnection);
if(mysql_num_rows($GUresult)) {
$GUfile = mysql_fetch_assoc($GUresult);
}
The errors I got where
Warning: mysql_query(): 3 is not a valid MySQL-Link resource in /home/ on line 49
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/ on line 50
If I don't have the include and just paste the query directly underneath the connection then it works. What seems to be the problem?
Feel the power of google.
While writing your question on SO brings you no help, exact error message pasted into google's search box finds you complete solution. In much less time.
The connection may fail, so you should check for connection errors.

What's the best way to access a MS Access database using PHP?

I need to access some data from an MS Access database and retrieve some data from it using PHP.
I've looked around the web, and found the following line which seems to correctly connect to the database:
$conn->Open("DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=C:\wamp\www\data\MYDB.mdb");
However, I have tried to retrieve some data in the following way:
$query = "SELECT pageid FROM pages_table";
$result = mysqli_query($conn, $query);
$amount_of_pages = 0;
if(mysqli_num_rows($result) <= 0)
echo "No results found.";
else
while($row = mysqli_fetch_array($result, MYSQL_ASSOC))
$amount_of_pages++;
And was presented with the following errors:
Warning: mysqli_query() expects parameter 1 to be mysqli, object given in C:\wamp\www\data\index.php on line 19
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in C:\wamp\www\data\index.php on line 23
No results found.
I don't really understand the connection to the Access database, is there something I should be doing differently?
Thanks in advance for any help.
I don't think you can use MySQLi with anything other than a MySQL db. You'll probably need to use an ODBC connection.
The mysqli_* functions are for MySQL databases only, and can't be used for Microsoft Access databases. See PHP's ODBC documentation for details on how to use these.

mysql_result() error happening all of a sudden

I recently rebuilt my website and now I'm getting this error:
Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /home/xx/public_html/asdf.com/comsel.php on line 28
Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /home/xx/public_html/asdf.com/comsel.php on line 29
Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /home/xx/public_html/asdf.com/comsel.php on line 30
Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /home/xx/public_html/asdf.com/comsel.php on line 31
Those lines are as follows:
mysql_connect($dbhost,$dbuser,$dbpass)
or die("Error: Failed to connect to database");
mysql_select_db($dbname)
or die("Error: Failed to select databse");
$query = "SELECT * FROM foxlose ORDER BY RAND ( ) LIMIT 1";
$result = mysql_query($query);
$title = mysql_result($result,$i,"title");
$link = mysql_result($result,$i,"link");
$nomen = mysql_result($result,$i,"name");
$text = mysql_result($result,$i,"text");
mysql_close();
The last four lines before mysql_close(); are lines 28-31.
Only here's the thing: those lines were the same in the old version of my site, and they worked. I can't figure out what's changed. The code also works fine when I use my computer as a server and host it locally.
Any idea what this error is trying to tell me?
Edit: Added all mysql in the document.
It seems to me odd that you close the connection to a database by:
mysql_close();
and then you use
$variable = mysql_result(...);
Generally, if you want to know the reason of the error use the function mysql_error.
Try removing the space after RAND:
$query = "SELECT * FROM foxlose ORDER BY RAND() LIMIT 1;
There are several things you could do here.
Check the mysql_query connection is actually set. Alternately youcan ensure the connection is being passed by passing it to the mysql query
i.e.
mysql_query($query, $connction);
Additionally, Thomas Clayson pointed out, ensure you are getting results. If there are no results mysql_result will throw an error
Ensure mysql_select_db (assuming you are using this), is connecting to the database properly.
Depending on versions and such you may need a semicolon to close your mysql statement
i.e.
$query = "SELECT * FROM foxlose ORDER BY RAND () LIMIT 1;";
Much of this has been taken from other people with similar issues. full credit (and more ideas) here: http://www.daniweb.com/forums/thread26425.html

error while using mysql_real_escape_string()

my code-
require 'database.php';
$title = mysql_real_escape_string($_POST['title']); //line 48
$cat = $_POST['cat'];
$txtart = mysql_real_escape_string($_POST['artbody']); //line 50
$date = date("d-m-y");
$q = "INSERT INTO tblarticle (art_title, art_cat, art_des, art_date) VALUES ('$title', '$cat', '$txtart', '$date')";
ERROR-->
Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'ODBC'#'localhost' (using password: NO) in C:\xampp\htdocs\shizin\admin\newArticle.php on line 48
Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in C:\xampp\htdocs\shizin\admin\newArticle.php on line 48
Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'ODBC'#'localhost' (using password: NO) in C:\xampp\htdocs\shizin\admin\newArticle.php on line 50
Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in C:\xampp\htdocs\shizin\admin\newArticle.php on line 50
But data is getting saved in DB but null titile and artbody fields
mysql_real_escape_string tries to connect to the local database, so it can fetch the settings it needs to escape the string correctly for that system.
You can tell the function which connection to use by passing in a link identifier as a second argument:
mysql_real_escape_string($string, $link)
Please check your MySQL username and password and make sure you're entering them properly in mysql_connect. According to the error you didn't run mysql_connect with a password, which may be causing these problems.
Check database.php connection strings, and the MySQL server is running as mysql_real_escape_string needs a valid open database connection to work.

Warning: mysql_real_escape_string()?

my code-
$username = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string($_POST['password']);
but it throws warning-
Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'ODBC'#'localhost' (using password: NO) in C:\xampp\htdocs\pics\confirm_login_credentials.php on line 3
and
Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in C:\xampp\htdocs\pics\confirm_login_credentials.php on line 3
mysql_real_escape_string requires an established link to the database to distinguish the actually used character encoding:
Note: A MySQL connection is required before using mysql_real_escape_string() otherwise an error of level E_WARNING is generated, and FALSE is returned. If link_identifier isn't defined, the last MySQL connection is used.
It seems that you don’t have that when calling mysql_real_escape_string.
http://www.webmasterworld.com/php/3120893.htm
You need the database link before anything else.
mysql_real_escape_string() takes a connection handler and escapes the
string according to the current
character set. Although depreciated,
mysql_escape_string [us3.php.net]
doesn't need a connection

Categories