Using mysqli_query with mysql_query arguments - php

I'm migrating from PHP 5 to PHP 7, and I made a grep to get all mysql_query to change them to mysqli_query. The problem is, apparently the parametres changed using the procedural style.
mysql_query($query, $link_identifier)
mysqli_query($link_identifier, $query)
Even tho using the object oriented style they're still the same parametres.
Question is, will it work if I leave the parameters of mysqli_query as $query, $link thinking that the function is smart enough to detect which is which or I need to change them all to match the right parameters ?

Order of arguments isn't interchangeable.
Php.net mysqli_query.
Php.net mysql_query.
I also tested it.mysql_query
$link = mysql_connect("localhost","login","pass");
$db_selected = mysql_select_db('db', $link);
$sql = "SELECT * FROM table LIMIT 5";
$result = mysql_query($sql, $link);
while($row = mysql_fetch_array($result)) {
echo $row['col_name'];
}
mysql_close($link);
mysqli_query with with wrong order of arguments will produce: Warning: mysqli_query() expects parameter 1 to be mysqli, string given code here:
$link = mysqli_connect("localhost", "login", "pass", "db");
//arguments in wrong order
if ($result = mysqli_query("SELECT * FROM table LIMIT 5", $link)) {
while($row = mysqli_fetch_array($result)) {
echo $row['col_name'];
}
}
//produce
//Warning: mysqli_query() expects parameter 1 to be mysqli, string given in
mysqli_close($link);

Related

how can i correct this warning error im getting

How can I correct this error I'm getting in my function, below is my error
Warning: mysql_query() expects parameter 2 to be resource, null given in C:\xampp\htdocs\how are things\15_06_widget_corp-final\includes\functions.php on line 40
Database query failed:
the code on line 40 is this
$subject_set = mysql_query($query, $connection);
This is my function code
function get_all_subjects($public = true) {
global $connection;
$query = "SELECT *
FROM subjects ";
if ($public) {
$query .= "WHERE visible = 1 ";
}
$query .= "ORDER BY position ASC";
$subject_set = mysql_query($query, $connection);
confirm_query($subject_set);
return $subject_set;
}
It requires $connection to be reference to connection to mysql server.
Try
$connection = mysql_connect('host','user','pass');
You must assign $connection to a
mysql_connect()
function
Anyway you should not use mysql functions anymore because they are deprecated.
You can use PDO instead.

How do you see inside a mysqli_query object to trouble shoot?

I've come into this problem a few times and it's a pain. Right now the line if(mysqli_num_rows($result) != 1) gives the error mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given and $result is the result of mysqli_query(). I can't echo $result and I tried the following code to find out what's inside $result with no success
if (!mysqli_query($link, "SET #a:='this will not work'"))
printf("Error: %s\n", mysqli_error($link));
while ($row = mysqli_fetch_row($result))
printf("%s\n", $row[0]);
I'm trying to trouble shoot but it's like I come to a dead end because I can't see inside the result of mysqli_query().
How can I find the source of the problem? I'm guessing it's bad SQL syntax but I need more details.
Here is the PHP code where $result is defined
$result = mysqli_query($link, 'SELECT `password`
FROM `ajax_login`
WHERE userid = \''.$userName.'\' LIMIT 1');
Are you checking the error code/message? Something like:
if(mysqli_errno($link)){
die(mysqli_error($link));
}
(of course, you wouldn't use die in production)

MySQLi Query will not execute

I am playing around with this trying to pull some articles from a database.
Here is the code i have so far: ($host etc are defined in config)
include ("admin/config.php");
$mysqli = mysqli_connect($host, $user , $pass , $database);
$query = "SELECT * FROM articles ORDER BY date LIMIT 5";
$result = mysqli_query($mysqli,$query);
while($row = mysqli_fetch_array($result))
{
Some code to display results
}
This is the error that gets generated but i just cant see what i am doing wrong. Any help will be much appreciated.
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /website....
there is error in your query.
to get it always run all your queries this way
$result = mysqli_query($mysqli,$query) or trigger_error($mysqli->error."[$query]");

PHP mysql_query parameter order

I'm a bit of a noob but I am having trouble finding the answer to this question. I have two pieces of code and both work but I'm not sure why.
// Connect to the database
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
// Retrieve the score data from MySQL
$query = "SELECT * FROM table";
$result = mysqli_query($dbc,$query);
mysqli_close($dbc); // close db
In the instance above the connection is the first mysql_query parameter and then the sql query. This came from a book and isn't how the PHP manual defines it as far as I can see.
In the next example the sql is entered first, then the connection and I have to specifically use mysql_select_db. Why do they both work?
//db connection
$dbc = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$db_selected = mysql_select_db("database",$dbc); // select the db
$sql = "SELECT * from table"; // sql query
$result = mysql_query($sql, $dbc); // make query on db
mysql_close($dbc); //close the connection after data is acquired
Thanks in advance for any illumination you can supply.
Your first example
$result = mysqli_query($dbc,$query);
Is a function call to the newer mysqli library, http://www.php.net/manual/en/mysqli.query.php
Your second example
$result = mysql_query($sql, $dbc); // make query on db
Is a function call to the mysql library, http://www.php.net/manual/en/function.mysql-query.php
Both calls match the signature and order of arguments to those functions.
$result = mysqli_query($dbc,$query); // MySQLi (link, query)
$result = mysql_query($sql, $dbc); // MySQL (query, link)
You're using mysqli_query in the first example, and mysql_query in the second.
Two different function with two different parameter order, that's why I "love" PHP.
Because mysql_query() expects query as first parameter and mysqli_query() expects your query as 2nd.
mysqli_query
mysqli_query ( mysqli $link , string $query [, int $resultmode ] )
mysql_query
mysql_query ( string $query [, resource $link_identifier ] )
mysql_select_db() is required because mysql_connect() doesn't have database as parameter, instead of mysqli_connect().
The function mysql_select_db let you choose another db along your code without the need of 'another' new connection, using the same connection you started with mysql_connect.

MySQL parameter resource error

Here is my error:
Warning: mysql_query() expects parameter 2 to be resource, null given...
This refers to line 23 of my code which is:
$result = mysql_query($sql, $connection)
My entire query code looks like this:
$query = "SELECT * from users WHERE userid='".intval( $_SESSION['SESS_USERID'] )."'";
$result = mysql_query($query, $connection)
or die ("Couldn't perform query $query <br />".mysql_error());
$row = mysql_fetch_array($result);
I don't have a clue what has happpened here. All I wanted to do was to have the value of the users 'fullname' displayed in the header section of my web page. So I am outputting this code immediately after to try and achieve this:
echo 'Hello '; echo $row['fullname'];
Before this change, I had it working perfectly, where the session variable of fullname was echoed $_SESSION['SESS_NAME']. However, because my user can update their information (including their name), I wanted the name displayed in the header to be updated accordingly, and not displaying the session value.
Your $connection variable is NULL that's what your error message is referring to.
Reason being is that you have not called mysql_connect. Once called it will assign you a resource where you can set it to the $connection variable, thus being non-null.
As an example:
$connection = mysql_connect('localhost', 'mysql_user', 'mysql_password');
// now $connection has a resource that you can pass to mysql_query
$query = "SELECT * from users WHERE userid='".
intval( $_SESSION['SESS_USERID'] )."'";
$result = mysql_query($query, $connection)
include the mysql connections on your class file, for example:
connections/mysql.php
<?
$hostname_MySQL = "localhost";
$database_MySQL = "database";
$username_MySQL = "user";
$password_MySQL = "password";
$MySQL = mysql_pconnect($hostname_MySQL, $username_MySQL, $password_MySQL) or trigger_error(mysql_error(),E_USER_ERROR);
mysql_select_db($database_MySQL,$MySQL);
?>
class.php
<?
include "Connections/MySQL.php";
class utils {
public function myFunction()
{
global $MySQL;
$sql = "select * from table";
$rs = mysql_query($sql, $MySQL) or die(mysql_error());
$filas = mysql_fetch_assoc($rs);
$totalFilas = mysql_num_rows($rs);
...
}
}
?>
You have two ways of doing this, you need to use mysql_connect to connect to your database, you can pass this to mysql_query if you desire, if you don't pass anything to mysql_query PHP uses the last link opened from mysql_connect
$conn = mysql_connect("localhost", "mysql_user", "mysql_password");
$sql = "SELECT id as userid, fullname, userstatus
FROM sometable
WHERE userstatus = 1";
$result = mysql_query($sql);
Have you connected to your database? If so please show this code too.
For now, just try removing the $connection variable, like this:
$result = mysql_query($query);
And see where that gets you.
$connection is assigned the value of the database connection resource id. You don't have that in your script, so the value of $connection is NULL, and that is why you are getting the error. You need to connect to the database before using mysql_query(). You should be okay after that.
You need to do:
$connection=mysql_connect('host','user','pass');
if($connection === false) {
echo "Error in connection mysql_error()";
}

Categories