Link to home
Start Free TrialLog in
Avatar of sammySeltzer
sammySeltzerFlag for United States of America

asked on

mysqli_query() expects at least 2 parameters 1 given...

We upgraded to windows 7 64 bit and as a result, we had to reinstall apache, php and MySQL.

This is creating the following error when attempt to run our existing app:

mysqli_query() expects at least 2 parameters 1 given..

Initial error was that  mysql_query() is now deprecated and will be removed.

I have seen a lot of materials about this out there but have not been able to use them to resolve my current code below:

function getEventStats($day_i,$month_i,$year_i){
global $CAT_TB,$EVENTS_TB,$USER_TB,$showeventstats,$userid,$userview,$userlogin,$catview,$ugroup,$uname,$caleventapprove;
  // now get number of events on $day_i approved
  if ($showeventstats==1) {
     $query = "select count(id) from ".$EVENTS_TB." left join ".$CAT_TB." on ".$EVENTS_TB.".cat=".$CAT_TB.".cat_id where day='$day_i' and month='$month_i' and year='$year_i' and approved='1' " ;
     if (($userview==1)&&($userlogin==1)) {  // view user specific events only
     if ($ugroup!=0) $query = $query." and ".$EVENTS_TB.".user='".$uname."' ";
     }
  $result = mysql_query($query);
  $row = mysql_fetch_row($result) ;
  echo "<b>".$row[0]."</b> ".translate("confirmed events for today")." <br/>" ;
  if ($caleventapprove==0) {
    // now get number of events on $day_i not approved
    $query = "select count(id) from ".$EVENTS_TB." left join ".$CAT_TB." on ".$EVENTS_TB.".cat=".$CAT_TB.".cat_id where approved='0' order by day,month,year ASC";
    $result = mysql_query($query);
    $row = mysql_fetch_row($result) ;
    echo "<b>".$row[0]."</b> ".translate("events awaiting approval") ;
    }
  }
}

Open in new window


Is there any MySQL_query to mySQLI_query converter tool out there?
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Not that I have ever heard of.  Part of the problem is that the syntax between 'mysql' and 'mysqli' is not exactly the same.  I have had to hand edit all my code to change from 'mysql' to 'mysqli'.  Examples here:  http://php.net/manual/en/mysqli.query.php
Ray's well-written artice about the conversion necessary for such an upgrade might help:

https://www.experts-exchange.com/Programming/Languages/Scripting/PHP/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html

Moving up to the latest version of wampserver means you are now in a world where the mysql API is replaced in favor of either mysqli or PDO, I did the more difficult conversion to PDO and it was relatively painless and move up to production with it was almost a no-brainer. I would suspect thet converting to mysqli is even easier because the syntax is pretty close.

Cd&
Avatar of sammySeltzer

ASKER

There are some conversions to mysqli_...that are pretty simple.

For instance, this was dead simple:

$connection = mysqli_connect($dbhost,$dbuser,$dbpass) or die("could not connect");
$db_select = mysqli_select_db($connection, "$db");
if (!$db_select) {
    die("Database connection failed: " . mysqli_error());
}

So instead of MySQL_select_db("$db"), It became what it is above.

Not exactly same with the original code I posted.
conversions to mysqli_...that are pretty simple
Yes, if you discount the issues related to automated testing.  For example, MySQLi uses the same queries but puts the function arguments in different order.  It's not rocket science, but there are a lot of lines of code that probably need to change.
Ray,

I have been reading your article on the link posted by COBOLdinosaur.

Very good article, very long but very interesting and engaging and I intend to read it all.

I will be asking questions as relates to the code I posted as I go along.

One such question has to do with your example ( I know, you did mention your examples could havd bugs).

However, it seems to me that everything (insert, select) is engineered by this line of code:

$mysqli = new mysqli($db_host, $db_user, $db_word, $db_name);

Example, to run a query using your example:

$sql = "SELECT id, lname FROM my_table WHERE fname='$safe_fn' ORDER BY lname, fname";
$res = $mysqli->query($sql);

Open in new window


You reference  $mysqli->query($sql) to make it msqli compliant.

The equivalence of my code to that would be:

     $query = "select count(id) from ".$EVENTS_TB." left join ".$CAT_TB." on ".$EVENTS_TB.".cat=".$CAT_TB.".cat_id where day='$day_i' and month='$month_i' and year='$year_i' and approved='1' " ;
     if (($userview==1)&&($userlogin==1)) {  // view user specific events only
     if ($ugroup!=0) $query = $query." and ".$EVENTS_TB.".user='".$uname."' ";
     }
  $result = $con>mysql_query($query);

The issue I am having though is that while your connection string is on same page as the query, my connection string is on a separate file.

I have read a lot so far on some articles posted online, one of those is the link posted by Dave but I am yet to find an example of how to handle situations similar to mine.
One of the things that you will run into is that unlike the old 'mysql' connection method which was 'global', the 'mysqli' connection ($con in your example) is Not.  I had to add a 'global' statement in a few functions so the connection data would be available to that function.

Note that there are actually 2 different methods available in the 'mysqli' driver.  You need to pick one and stick with it.  $mysqli->query is the Object oriented style and mysqli_query is the Procedural style.  Note that the first one has a '$' because it is a value in an OOP class.  The second one does not because it is a 'function'.

In your situation, you will have to go thru your code and change the functions and sometimes even the order of the code by referencing the docs in http://php.net/manual/en/book.mysqli.php .
Dave,

One more question.

When I reinstalled php, apache and MySQL, I used wamp.

As a result, I don't see any extension to sql server database.

Any ideas how I can modify php.ini file to add MSSQL extension?

It is beginning to look like I would rather be using sql server database as a backend than MySQL.

Not sure if it makes a difference in terms of using mysqli or not but willing to try that.

Thanks for your help.
MSSQL extension is obsolete and not supported as of PHP 5.3 on Windows.  You have to use the 'sqlsrv' extension from Microsoft.  If your WAMP install doesn't have it, you can download it from Microsoft.  http://www.microsoft.com/en-us/download/details.aspx?id=20098   The syntax is somewhat different also but the Help file that comes with the driver has good examples.
SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ray,

It can be a lot of fun sometimes reading your write-ups, articles, posts.

This one is certainly very, very informative.

The biggest issue I have had working with php so far is that I have never really dedicated alot of time to understand the intricacies and nuances and that's because 99% of the work I do/have done so far are sql development,  .net and before .net, classic asp.

However, I do have run into situations such as now when I am asked to do some php mods.

I appreciate you guys always coming to the rescue.

I am glad that I do contribute here too, though not as much as I used to due to heavy work load.
Dave,

I have been trying to configure php.ini to interface with sql server and I have had no luck so far.

You are correct that the help files that came with the link you provided are pretty good but they don't address configuring sql server driver to work with php.

This is what I have tried so far:

I copied the following to the ext directory:

php_sqlsrv_55_ts.dll
php_pdo_sqlsrv_55_ts.dll


According to documentation, those drivers are for PHP Version 5.5.12

Then I created extensions for them and added to them php.ini:

extension=php_sqlsrv_55_ts.dll
extension=php_pdo_sqlsrv_55_ts.dll

Open in new window


I fired up phpinfo but I don't see the sql server entries there.

From you guys's experience, what am I missing?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If I am not wrong, ODBC is just for connecting or for setting up connection string to sql server.

It shouldn't have anything to do with sql server info appearing on phpinfo, no?
It has everything to do with it.  The extension will not run if it is not installed.  And if it doesn't run, it doesn't show up in 'phpinfo()'.  That is an ODBC Driver, not the ODBC manager.  In previous versions of SQL Server, it was called the SQL Native Client.
Ok, I will try and install that driver tonight.

I am being chased out of here right now, :)