Hi all,

Over the last few months forum members have been telling me to start using MySLQi, get away from MySQL.

I now have MySLQi installed on my server.

I the past I would write a query like:

mysql_select_db($database_iMaint, $iMaint);
$query_Avail = sprintf("SELECT * FROM NXLHR_Active"));
$Avail = mysql_query($query_Avail, $iMaint) or die(mysql_error());
$row_Avail = mysql_fetch_assoc($Avail);
$totalRows_Avail = mysql_num_rows($Avail);

Using MySQLi I am now writing it like:

$query_Avail = mysqli_query($Con,"SELECT * FROM NXLHR_Active");
$Avail = mysqli_query($query_Avail, $Con) or die(mysqli_error());
$row_vail = mysqli_fetch_assoc($Avail);
$totalRows_Avail = mysqli_num_rows($Avail);

The connection I have is:

$Con = mysqli_connect($hostname, $username, $password or trigger_error(mysqli_error(),E_USER_ERROR); 

When I run the MySQLi query I get an error: Warning: mysqli_query() expects parameter 1 to be mysqli, boolean given.

Can anyone see where I am going wrong or how should I be constructing the query using mySQLi.

Many thanks in advance for your time.

Blackbox

    Your mysqli connection is failing. You're not passing the database name, nor are you closing the function call before attempting to trigger the error on fail.

    $Con = mysqli_connect($hostname, $username, $password**, $database_name)** or trigger_error(mysqli_error(),E_USER_ERROR);
    

    The part between the '**'s is what you're missing.

    Also, if you're making the change from mysql, you might want to check out PDO. In my experience it's more consistent and much easier to deal with.

      The problem is in your connection code. You are OR'ing the $password variable with the trigger_error() statement, inside the mysqli_connect() statement. That makes no sense. The php.net documentation has examples showing how to use mysqli_connect(). You would also typically select the database when you make the connection, as a parameter the connection call, though this is not required.

      You are also not using the mysqli_error() statement correctly. It REQUIRES the connection link as a parameter, which also means you cannot use it to get error information about the connection. Again, this is covered in the php.net documentation, for both just the mysqli_error() statement and code examples that include the use of the mysqli_error() statement in them.

      In your recent threads, the PDO extension was mentioned as well. You should use it instead of the mysqli extension. It is more consistent and easier to use then the msyqli extension, especially if you are using prepared queries.

      Speaking of using prepared queries, you should use them when you are putting data values into an sql query statement. They eliminate the need to escape string data, which is (still) open to sql injection if not used correctly and with correct character encoding set for a database connection. Prepared queries prevent sql injection by separating the data from the sql query statement. Both the PDO and msyqli extensions support prepared queries.

      Next, both the PDO and mysqli extensions support using exceptions for errors. You should use exceptions for errors. This will eliminate the need for things like the or trigger_error() / or die(), and conditional logic around database statements that can fail, simplifying all your logic. You can just enable exceptions (done slightly different between the PDO and msyqli extensions) and any errors will throw an exception. You can just let php catch the exception and handle the error, in which case php's error_reporting and display_errors/log_errors settings determine what happens with the error information, or you can catch the exception in your code, with a try/catch block, and handle the error yourself. If you let php do this, you will get a fatal uncaught exception error that also contains the actual database error information.

      Lastly, the only big gotcha of using PDO, is that it emulates prepared queries by default and has a setting that you need to turn off so that real prepared queries get used.

        One other advantage of PDO: If for any reason you want/need to change to a different DBMS, you only have to change the DSN string in the PDO constructor. Well, only that, and tweak any queries where you use SQL functions or syntax that is unique to the DBMS you were using. :queasy:

          Thank you to all that replied.

          I can now move forward and start to learn a little more concerning MySQLi and PDO.

          Again, Thanks.

            Write a Reply...