Solved

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

Posted on 2015-01-15
16
225 Views
Last Modified: 2016-05-27
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?
0
Comment
Question by:sammySeltzer
  • 7
  • 5
  • 2
  • +1
16 Comments
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 40551967
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
0
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 40551980
Ray's well-written artice about the conversion necessary for such an upgrade might help:

http://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&
0
 
LVL 28

Author Comment

by:sammySeltzer
ID: 40552106
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.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40552727
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.
0
 
LVL 28

Author Comment

by:sammySeltzer
ID: 40552783
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.
0
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 40552824
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 .
0
 
LVL 28

Author Comment

by:sammySeltzer
ID: 40553414
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.
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 40554199
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.
0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 40555137
Couple of thoughts here... Let's say you choose the OOP version of MySQLi.  Your connection looks like this:

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

That statement results in the assignment of an object to $mysqli.  The object represents the MySQLi connection.  Then your scripts call methods on that object to accomplish queries.  The man pages are a lot to read, but it's required reading for PHP developers.
http://php.net/manual/en/book.mysqli.php

Now let's say you want to use a class or function to wrap around the query - an abstraction layer, or whatever.  With the old MySQL extension, the connection was a global element (see Dave's note above) and you could just assume that the connection existed in every scope and namespace after it was created.  But the $mysqli object will only exist in the scope of its creation.  It will have to be made global or passed into the functions that need to use it.  My advice is to pass it as an argument rather than make a global declaration inside the function.  Here's why.

Let's say you want to do some automated testing.  In order to do that, you need to understand dependency injection ("DI").  DI is a fancy term for passing data into your functions instead of creating the data inside them.  Here are two ways of accessing a $MySQLi object inside a function.

This one uses a dependency creation inside the function
function db_abstraction($sql)
{
    global $mysqli;
    /* use $sql for some queries here */
    return $rows;
}

Open in new window


This one uses a dependency injection from outside the function
function db_abstraction($mysqli, $sql)
{
    /* use $sql for some queries here */
    return $rows;
}

Open in new window


Why is the second method preferred?   Because you can use assertion and mocking systems like PHPUnit to make automated tests and prove that your code works correctly!  You can "swap out" the $mysqli object on a function-by-function and case-by-case basis to test each function with a mock version of $mysqli that uses another database, or that otherwise returns a predictable result (either success or failure) every time.

If your dependency on the $mysqli object is created inside each function (either by a global keyword or an object instantiation), you cannot mock one instance of $mysqli without also mocking all the other instances, and that somewhat defeats the ability to test each function in isolation.  PHP's goal with OOP, DI designs and type hinting is to bring some of the current best practices to a programming language that is decades old and that was originally created before the PHP community understood very much about computer science.
0
 
LVL 28

Author Comment

by:sammySeltzer
ID: 40555160
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.
0
 
LVL 28

Author Comment

by:sammySeltzer
ID: 40560535
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?
0
 
LVL 82

Accepted Solution

by:
Dave Baldwin earned 250 total points
ID: 40560562
You also need the SQL Native Client installed on your system.   Or the ODBC driver as mentioned in the system requirements here http://www.microsoft.com/en-us/download/details.aspx?id=20098

For version Version 3.1 of the driver requires Microsoft ODBC Driver 11 (or higher) for SQL Server version 11 or greater. You can download the Microsoft ODBC Driver 11 for SQL Server from the Microsoft® ODBC Driver 11 for SQL Server® - Windows page.
0
 
LVL 28

Author Comment

by:sammySeltzer
ID: 40560580
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?
0
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 40560714
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.
0
 
LVL 28

Author Comment

by:sammySeltzer
ID: 40560780
Ok, I will try and install that driver tonight.

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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now