How to change code below from mysql() to PDO
I have been researching and found out that I should be using PDO and not mysql() in my php to connect to the database. To help with my research and be able to change all of the mmysql() code in multiple pages to PDO, I want to know if anybody can convert the code below into PDO so then I can see and be able to use the answer you provided in order to help me change my code in other pages as well.

Below is the code:

    <?php
    
    session_start();
    
    
    $username="xxx";
    $password="xxx";
    $database="xxx";
    
    mysql_connect('localhost',$username,$password);
    
    mysql_select_db($database) or die( "Unable to select database");
    
    $i = 0;
    $c = count($_POST['gridValues']);
    
    $insertquestion = array();
    
    for($i = 0;  $i < $c; $i++ ){
    
    
        $optionquery = "SELECT OptionId FROM Option_Table WHERE (OptionType = '". mysql_real_escape_string($selected_option)."')";
        $optionrs = mysql_query($optionquery);
        $optionrecord = mysql_fetch_array($optionrs);
        $optionid = $optionrecord['OptionId']; 
    
        $insertquestion[] = "'". mysql_real_escape_string($_SESSION['id'] ) . 
                        ($_SESSION['initial_count'] > 1 ? $_SESSION['sessionCount'] : '') ."' ,'". 
                        mysql_real_escape_string( $_POST['numQuestion'][$i] ) ."','".  
                        mysql_real_escape_string( $optionid ) ."'";
    
    }
    
    
     $questionsql = "INSERT INTO Question (SessionId, QuestionId, QuestionContent, NoofAnswers, ReplyId, QuestionMarks, OptionId) 
        VALUES (" . implode('), (', $insertquestion) . ")";
    
    mysql_query($questionsql);
    
    mysql_close();
    
    
    ?>

Also what is PDO version of below:

    $num = mysql_num_rows($result = mysql_query($query));
    
    
    while($row = mysql_fetch_array($result)){
    }
How to change code below from mysql() to PDO
===

I have been researching and found out that I should be using PDO and not mysql() in my php to connect to the database. To help with my research and be able to change all of the mmysql() code in multiple pages to PDO, I want to know if anybody can convert the code below into PDO so then I can see and be able to use the answer you provided in order to help me change my code in other pages as well.

Below is the code:

    <?php
    
    session_start();
    
    
    $username="xxx";
    $password="xxx";
    $database="xxx";
    
    mysql_connect('localhost',$username,$password);
    
    mysql_select_db($database) or die( "Unable to select database");
    
    $i = 0;
    $c = count($_POST['gridValues']);
    
    $insertquestion = array();
    
    for($i = 0;  $i < $c; $i++ ){
    
    
        $optionquery = "SELECT OptionId FROM Option_Table WHERE (OptionType = '". mysql_real_escape_string($selected_option)."')";
        $optionrs = mysql_query($optionquery);
        $optionrecord = mysql_fetch_array($optionrs);
        $optionid = $optionrecord['OptionId']; 
    
        $insertquestion[] = "'". mysql_real_escape_string($_SESSION['id'] ) . 
                        ($_SESSION['initial_count'] > 1 ? $_SESSION['sessionCount'] : '') ."' ,'". 
                        mysql_real_escape_string( $_POST['numQuestion'][$i] ) ."','".  
                        mysql_real_escape_string( $optionid ) ."'";
    
    }
    
    
     $questionsql = "INSERT INTO Question (SessionId, QuestionId, QuestionContent, NoofAnswers, ReplyId, QuestionMarks, OptionId) 
        VALUES (" . implode('), (', $insertquestion) . ")";
    
    mysql_query($questionsql);
    
    mysql_close();
    
    
    ?>

Also what is PDO version of below:

    $num = mysql_num_rows($result = mysql_query($query));
    
    
    while($row = mysql_fetch_array($result)){
    }