I work with xampp. I performed MySQL connection:
$connection = mysql_connect($host , $user , $passw);
mysql_select_db($db, $connection);
I received output with echo command (by check the boolean returned values) that connection is established and the database $db is found.
But the simplest query like:
$query = mysql_query("SELECT * FROM 'users'");
returns false. How can I debug why?Thanks.
An obligatory update: as mysql ext is no more, here are answers for two remaining MySQL APIs which I written on my site based on the experience from answering 1000s questions on Stack Overflow:
How to report errors in mysqli
How to connect to MySQL using PDO (with the aim of the proper error reporting).
In short, for mysqi the following line have to be added before mysqli_connect() call:
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
while for PDO the proper error mode have to be set, for example
$pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
As of the old mysql ext,
To get an error from mysql_query() you have to use mysql_error() function.
So always run all your queries this way, at least until you develop a more advanced query handler:
$query = "SELECT * FROM 'users'";
$result = mysql_query($query) or trigger_error(mysql_error()." ".$query);
the problem with your current query is 'users' part. Single quotes have to be used to delimit strings while for the identifiers you have to use backticks:
SELECT * FROM `users`
In order to see these errors during development, add these lines at the top of your code to be sure you can see every error occurred
ini_set('display_errors',1);
error_reporting(E_ALL);
on the production server, however, the value on the first line should be changed from 1 to 0
Use the mysql_error() function:
$query = mysql_query("SELECT * FROM 'users'") or die(mysql_error());
EDIT: Per Col. Shrapnel's comment: you should never use die() outside of a test environment. In general it's bad practice when writing code that's even intended for production.
Here is some more information: http://www.phpfreaks.com/blog/or-die-must-die
Based on Your Common Sense answer this is an object oriented style:
$query = "SELECT * FROM 'users'";
$result = $mysqli -> query($query) or trigger_error($mysqli -> error." ".$query);
Related
I got the warning:
Warning: mysqli_query() expects parameter 1 to be mysqli, string given in (...) on line 6
My code is here:
<?php
require_once 'conn.php';
$sql = "SELECT user_id, access_lvl, name FROM cms_users ";
$result = mysqli_query($sql, $conn);
It's exactly as the error states as you're passing arguments to mysqli_query() incorrectly. Assuming $conn is your mysqli connection generated at some point by new mysqli() it should be:
$result = mysqli_query( $conn,$sql) or trigger_error(mysqli_error($conn)));
The way you were calling it you were passing a string, $sql as the first argument.
I was having this this problem but after swiping
$result = mysqli_query($sql, $conn) to $result = mysqli_query( $conn,$sql)
I manage to solve this error
I am 3 years too late but all you need to do is swap the parameters of mysqli_query()
$result = mysqli_query($conn, $sql)
I have the same error, although the $result = mysqli_query($conn, $sql) is the correct way around.
I var_dump() the $conn object and it is a set object at the time I run the query, but still returns a 'string given' error.
I was accessing the $conn object after being parsed into a function that I was using it with, in the same way I've done throughout the whole project without error.
Re-declaring the $conn object inside the function, instead of passing it into the function stopped the errors, although this behaviour doesn't occur anywhere else in my project. This isn't an ideal solution either.
To note: I'm using a .env for local development, which causes no issues and helps with deployment locally/remotely via .git.
After many hours, I honestly believe there is a PHP bug here, I'm using 7.3.0, but occurred in 7.2.5 as well as I'm definitely parsing it a db connection object, not a string.
Just posting this for information purposes, in case anyone else runs into it. Thanks.
PS. Passwords shouldn't be stored in the database in plain text and is a major security concern. If the author hasn't adjusted this yet (I know it's an old post), it's important to read:
Secure hash and salt for PHP passwords
My test code:
$connection = mysql_connect('localhost', 'root', '') or die(mysql_error());
mysql_select_db("chaoge", $connection);
mysql_query("SET NAMES UTF8", $connection);
$rs = mysql_query("SELECT * FROM babel_node WHERE nod_pid = 2101", $connection);
$nu = mysql_affected_rows();
echo $nu;
It says that mysql_affected_rows works with INSERT, UPDATE, REPLACE and DELETE。
Why I can also get the right result through mysql_affected_rows ?
Any help and suggestions will be highly appreciable。
Here is what I found on the internet.
mysql_affected_rows() for a SELECT indicates the number of rows which
were found. mysql_num_rows() indicates how many rows were actually
returned. They may not be the same, IIRC, if you have a LIMIT clause
or similar. GROUP BY may also cause a difference.
Source
See answers on the source at the bottom.
I suggest you must use
MySQLi http://php.net/manual/en/book.mysqli.php
or
PDO_MySQL http://php.net/manual/en/ref.pdo-mysql.php
for updated method of accessing database since your method was deprecated in PHP 5.5.0
I am querying a mysql database with php but cannot get it to work on my iMac. In particular, php is unable to connect to the mysql DB. It connects to mysql and selects the DB but then fails. See code below:
if (!mysql_connect($db_host, $db_user, $db_pwd)){
die("I cannot connect to database");
}
if (!mysql_select_db($database)){
die("I cannot select database");
}
$sql = "SELECT FROM ${table} ORDER BY $sql_orderBy";
$result = mysql_query($sql);
if (!$result) {
die("I cannot execute query to show fields from Table: {$table}. Query failed.");
}
For reference, I installed apache/mysql/php with macports. The same php code works on my laptop (same installations), and the query works when I invoke it from within mysql on both computers. All variables are declared. Something with the system config is my best guess, but I even went through a uninstall/install.
Any help would be appreciated!
Your issue is ${table} . This should be {$table} or better still, ".$table."
You also need to say what you are SELECTING:
So:
$sql = "SELECT * FROM ".$table." ORDER BY ".$sql_orderBy;
You can discover issues by using Mysql_error() at the end of the query, for example:
mysql_query($sqlString) or die("line: ".__LINE__.":".mysql_error());
this will output a clear error message regarding your SQL statement. This is not for production and public situations but for development.
Also:
MySQL is deprecated and is no longer supported by PHP or the wider community, it is VERY strongly recommended you take up MySQLi or PDO and use these methods as they are much stronger, less flawed and more efficient delivery of results. They will also be supported in future updates and developments whereas MySQL will not.
I have some trouble getting data from a mysql database.
Both the mysqli and mysql option to retrieve data using php from a database return zero rows, while they should return a couple of rows.
This is the code (mysql, the deprecated option):
$connection = mysqli_connect("localhost","username","pass","dbname") or die("Some error occurred during connection " . mysqli_error($connection));
$sql = "SELECT * FROM Persons";
mysql_query($sql,$connection);
printf("Select returned %d rows.\n", mysql_num_rows($sql));
mysql_close($connection);
This also did not work:
$data= mysqli_query("SELECT * FROM name");
printf("Select returned %d rows.\n", mysqli_num_rows($data));
Can anyone please help me out and explain why does not generate any result? If I pass the SQL into phpMyAdmin than it perfectly works and shows me all the data I want.
ini_set('display_errors',1);
error_reporting(E_ALL);
are essential options in development mode.
without ability to see error messages it's just useless to try any code.
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
also helps when dealing with mysqli.
I work with xampp. I performed MySQL connection:
$connection = mysql_connect($host , $user , $passw);
mysql_select_db($db, $connection);
I received output with echo command (by check the boolean returned values) that connection is established and the database $db is found.
But the simplest query like:
$query = mysql_query("SELECT * FROM 'users'");
returns false. How can I debug why?Thanks.
An obligatory update: as mysql ext is no more, here are answers for two remaining MySQL APIs which I written on my site based on the experience from answering 1000s questions on Stack Overflow:
How to report errors in mysqli
How to connect to MySQL using PDO (with the aim of the proper error reporting).
In short, for mysqi the following line have to be added before mysqli_connect() call:
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
while for PDO the proper error mode have to be set, for example
$pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
As of the old mysql ext,
To get an error from mysql_query() you have to use mysql_error() function.
So always run all your queries this way, at least until you develop a more advanced query handler:
$query = "SELECT * FROM 'users'";
$result = mysql_query($query) or trigger_error(mysql_error()." ".$query);
the problem with your current query is 'users' part. Single quotes have to be used to delimit strings while for the identifiers you have to use backticks:
SELECT * FROM `users`
In order to see these errors during development, add these lines at the top of your code to be sure you can see every error occurred
ini_set('display_errors',1);
error_reporting(E_ALL);
on the production server, however, the value on the first line should be changed from 1 to 0
Use the mysql_error() function:
$query = mysql_query("SELECT * FROM 'users'") or die(mysql_error());
EDIT: Per Col. Shrapnel's comment: you should never use die() outside of a test environment. In general it's bad practice when writing code that's even intended for production.
Here is some more information: http://www.phpfreaks.com/blog/or-die-must-die
Based on Your Common Sense answer this is an object oriented style:
$query = "SELECT * FROM 'users'";
$result = $mysqli -> query($query) or trigger_error($mysqli -> error." ".$query);