Upgrading MYSQL to MYSQLI

Asked

Viewed 138 times

-2

What can I do to fix this? I did some research, but I couldn’t understand what I’m doing wrong, my knowledge is basic in the subject. I bought an application that came old MSQL that no longer worked on PHP7, I’m trying to update.

Warning: mysqli_query() expects at least 2 Parameters, 1 Given in /Storage/ssd1/664/11665664/public_html/news/api.php on line 23

Warning: mysqli_query() expects at least 2 Parameters, 1 Given in /Storage/ssd1/664/11665664/public_html/news/api.php on line 32

Warning: mysqli_num_rows() expects Parameter 1 to be mysqli_result, null Given in /Storage/ssd1/664/11665664/public_html/news/api.php on line 55 []

<?php
    $servername = "localhost";
    $database = "id11665664_admin";
    $username = "id11665664_lincoln";
    $password = "157abc";

    // Create connection

    $mysqli = mysqli_connect($servername, $username, $password, $database);

    // Check connection

    if (!$mysqli) {

        die("Connection failed: " . mysqli_connect_error());

    }
    echo "Connected successfully";
    mysqli_close($mysqli);
?>
<?php

    mysqli_query("SET NAMES 'utf8'"); 
    //mysql_query('SET CHARACTER SET utf8');

    if(isset($_GET['cat_id']))
    {
            //$query="SELECT * FROM tbl_news_category WHERE cid='".$_GET['cat_id']."' ORDER BY tbl_news_category.cid DESC";     
            //$resouter = mysql_query($query);

            $query="SELECT * FROM tbl_news_category c,tbl_news n WHERE c.cid=n.cat_id and c.cid='".$_GET['cat_id']."' ORDER BY n.nid DESC";         
            $resouter = mysqli_query($query);

    }
    else if(isset($_GET['latest_news']))
    {
            $limit=$_GET['latest_news'];        

            $query="SELECT * FROM tbl_news_category c,tbl_news n WHERE c.cid=n.cat_id ORDER BY n.nid DESC LIMIT $limit";            
            $resouter = mysqli_query($query);
    }
    else if(isset($_GET['apps_details']))
    { 
            $query="SELECT * FROM tbl_settings WHERE id='1'";       
            $resouter = mysqli_query($query);
    }
    else
    {   
            $query="SELECT * FROM tbl_news_category ORDER BY cid DESC";         
            $resouter = mysqli_query($query);
    }

    $set = array();

    $total_records = mysqli_num_rows($resouter);
    if($total_records >= 1){

      while ($link = mysqli_fetch_array($resouter, MYSQLI_ASSOC)){

        $set['NewsApp'][] = $link;
      }
    }

     echo $val= str_replace('\\/', '/', json_encode($set)); 
?>
  • if it is mysqli, correct the title of your question

  • sorry, I didn’t notice the typo

  • There are some errors with your code. You barely open the database connection and then close it. All following calls to mysqli_close($mysqli); will give error. As for the alerts, this is the signature of mysqli_query mysqli_query ( mysqli $link , string $query [, int $resultmode = MYSQLI_STORE_RESULT ] ) : mixed, in the first parameter pass the reference to the BD connection you obtained with mysqli_connect. As an example one of your queries would look like this: $resouter = mysqli_query($mysqli, $query);

1 answer

-1

Try to use the connection to perform the processes, IE, store it in a variable to then have execution management...

    mysqli_report(MYSQLI_REPORT_STRICT);
    try 
    {
        self::$connection = new mysqli($array[0]["server"],$array[0]["user"],$array[0]["pwd"],$array[0]["db"],$array[0]["port"]) or die('Erro ao conectar o banco');
        mysqli_set_charset(self::$connection, 'utf8');
        if (!self::$connection)
        {
            die('Conexao invalida');
        }
    }
    catch (Exception $e ) 
    {
        echo "Excecao ao conectar o banco";
        exit;
    }

Note that I am using a class (self) but nothing that Voce does not solve with a variable And there, whereas connected...

self::$connection->query('Select * from table')
  • 1

    can explain how your answer solves the migration problem mentioned in the question, which already has a connection in the code?

  • Sorry Mrcunha, I didn’t get your answer

  • Lincoln, in the example I passed Voce will see that utod in mysqli works with 2 parameters, the open connection and the parameter, ie: $resouter = mysqli_query($mysqli, $query); taking your code as an example (see that Voce must pass the variable of the open connection and then the query), different from the old version that did not expect more of a connection

Browser other questions tagged

You are not signed in. Login or sign up in order to post.