1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Database Depreciation Errors

Discussion in 'Databases' started by captainron19, Jul 20, 2020.

  1. #1
    First of all I have no knowledge on MYSQL really but a friend created a database for me. I did do some updates to my server recently and ever since then an admin form I use on my website to select a category for a trivia question shows the following error on the form itself reading...
    SEMrush

    Deprecated: mysql(): This function is deprecated; use mysql_query() instead in /home/PATH-TO-PHP-FILE on line 116

    The admin area still works but how do i remove this error?
    The Database still works properly and pulls the data like it should for each day's new trivia question
     
    captainron19, Jul 20, 2020 IP
    SEMrush
  2. SpacePhoenix

    SpacePhoenix Well-Known Member

    Messages:
    153
    Likes Received:
    21
    Best Answers:
    2
    Trophy Points:
    120
    #2
    It's one of the functions of the mysql_* extension which was removed from version 7 of PHP. It's replacement is either the myslqi_* extension or PDO. Without seeing any of the code, my gut feeling is that you might potentially have SQL injection vulnerability issues
     
    SpacePhoenix, Jul 20, 2020 IP
  3. captainron19

    captainron19 Active Member

    Messages:
    49
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    93
    #3
    If it helps the code on line 116 reads...
    $catResult = mysql("$db", "select * from category ORDER BY name");

    And my PHP Version in use is 5.4.45
     
    captainron19, Jul 20, 2020 IP
  4. wmtips

    wmtips Well-Known Member

    Messages:
    590
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    140
    #4
    mysql_* functions are deprecated since PHP 5.5. I can't find the mysql function reference, it seems that it was deprecated even earlier. I think you need to use mysqli_* or PDO functions instead.
     
    Last edited: Jul 21, 2020
    wmtips, Jul 21, 2020 IP
  5. captainron19

    captainron19 Active Member

    Messages:
    49
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    93
    #5
    Yeah unfortunately I have no idea on any of that - like I said I have NO database knowledge and someone built this DB a while back for me and I can no longer get in touch with him
     
    captainron19, Jul 22, 2020 IP
  6. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    6,407
    Likes Received:
    1,368
    Best Answers:
    24
    Trophy Points:
    400
    #6
    Post the entire code and ask someone to rewrite it for mysqli_ It may work without changing anything else.
     
    qwikad.com, Jul 22, 2020 IP
  7. captainron19

    captainron19 Active Member

    Messages:
    49
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    93
    #7
    Guess it is worth a shot.... here is the code for that control page to add a new trivia question every day.... FYI the Depreciated Error comes up in the category section (see screen shot)
    
    <?php
    include("globals.php");
    include("functions.php");
    ?>
    <html>
    <head>
       <title>Add Trivia Question</title>
    </head>
    <link rel="stylesheet" href="<?php echo $siteURL; ?>styles.css">
    <body>
    <span class="titlefont">Add Trivia Question</span>
    <p>
    <?php
    $question=$_GET["question"];
    $answer=$_GET["answer"];
    $category=$_GET["category"];
    $sendDate=$_GET["sendDate"];
    $submit=$_GET["submit"];
    
    echo($category);
    
    if($submit){ // if they submit the form, run this
    stripslashes($question);
    // error checking
       sendDateCheck($sendDate);
       if(!$question){
         $error = "true";
         $questionERROR = "<span class=errorfont>This field is required</span><br>";
         echo "question";
       } else {
         $questionERROR = "";
       }
       if(!$answer){
         $error = "true";
         $answerERROR = "<span class=errorfont>This field is required</span><br>";
         echo "answer";
       } else {
         $answerERROR = "";
       }
       if(!$category){
         $error = "true";
         $categoryERROR = "<span class=errorfont>This field is required</span><br>";
         echo "cat";
       } else {
         $categoryERROR = "";
       }
    // end error checking
    
       if(!$error){ // if there are no errors add the trivia to the database
         $sendDate = strtotime($sendDate);
         
         // check to see if date is already in DB
         $dateResult = mysql("$db", "select * from trivia WHERE sendDate = '$sendDate'");
         echo mysql_error();
         $num_rows_date = mysql_numrows($dateResult);
         
         if($num_rows_date > "0"){ // if the current date matches one in DB run this
           echo "Date already exists in database<br>";
           $trivID = mysql_result($dateResult,0,'trivID');
           echo "<a href=\"",$siteURL,"search.php?trivID=$trivID&gotResults=true\" target=\"new\">Click here</a> to see the current question for this date.";
           $sendDate = date('m/d/Y',$sendDate);
         } else { // if the current date does not match one in DB then add it
           $sql = "INSERT INTO trivia (question,answer,sendDate) VALUES ('$question','$answer','$sendDate')";
           $result = mysql_query($sql);
           if(mysql_error()){ // if there is a mysql error show it
             echo mysql_error();
           }else{
             echo "Trivia added to database...<br>";
           } // end if mysql error
           // get trivID of the one we just put in of triv_cat relational DB
           $result = mysql("$db", "select * from trivia WHERE question='$question' AND answer='$answer'");
           echo mysql_error();
           $trivia = mysql_fetch_array($result);
           
           // associate the trivID to the selected catID's in the triv_cat relational DB
           $num_rows=count($category);
           for($i = 0; $i < $num_rows; ++$i){
             $catID = $category[$i];
             $sql = "INSERT INTO triv_cat (trivID,catID) VALUES ('$trivia[trivID]','$catID')";
             $result = mysql_query($sql);
             if(mysql_error()){ // if there is a mysql error show it
               echo mysql_error();
             }else{
               echo "Category / Trivia association added to database...<br>";
             } // end if mysql error
           }
           echo "Fill out the form below to add another<p>";
           // clear variables
           $question = "";
           $answer = "";
           $category = "";
           $sendDate = "";
         } // end if no date match
       } // end if no errors
    } // end if submit
    
    ?>
    
    <form action="<?php echo $PHP_SELF; ?>" method="get" name="memberAdd">
    <?php if($error) echo "<span class=errorfont>There are errors.  Please correct the marked fields, below.</span><br>"; ?>
    * = Indicated required fields
    <table width="600" border="1">
       <tr>
         <td width="250" class="mainfont" valign="top"><b>Trivia Question</b>*<br><?php echo $questionERROR; ?></td>
         <td width="350"><textarea cols="50" rows="3" name="question" class="mainfont"><?php echo htmlspecialchars($question); ?></textarea></td>
       </tr>
       <tr>
         <td class="mainfont" valign="top"><b>Trivia Answer</b>*<br><?php echo $answerERROR; ?></td>
         <td width="350"><textarea cols="50" rows="3" name="answer" class="mainfont"><?php echo htmlspecialchars($answer); ?></textarea></td>
       </tr>
       <tr>
         <td class="mainfont"><b>Category(s)</b>*<br><?php echo $categoryERROR; ?></td>
         <td width="350">
    <?php
       // get available category list
       $catResult = mysql("$db", "select * from category ORDER BY name");
       echo mysql_error();
       $num_rows_cat = mysql_numrows($catResult);
       
       $row = 0;
       while ($row < $num_rows_cat): /* used to loop through all records */
         
    ?>
         <input type="checkbox" name="category[]" value="<?php echo mysql_result($catResult,$row,'catID'); ?>"<?php catChecked($category,mysql_result($catResult,$row,'catID'),$num_rows_cat);?>> <?php echo mysql_result($catResult,$row,'name'); ?><br>
    <?php
         $row++;
       endwhile;
    ?>
         </td>
       </tr>
       <tr>
         <td class="mainfont"><b>Date</b>*<br><?php echo $answerERROR; ?>
         <span class="minifont">MUST be formatted MM/DD/YYYY</span></td>
         <td width="350"><input type="text" name="sendDate" value="<?php echo $sendDate; ?>" maxlength="10" size="10"></td>
       </tr>
    </table>
    <input type="submit" name="submit" value="Submit">
    </form>
    
    </body>
    </html>
    
    Code (markup):
     

    Attached Files:

    • 1.jpg
      1.jpg
      File size:
      64.4 KB
      Views:
      11
    captainron19, Jul 22, 2020 IP
  8. JEET

    JEET Notable Member

    Messages:
    3,352
    Likes Received:
    371
    Best Answers:
    16
    Trophy Points:
    235
    #8
    Change this line:

    $dateResult = mysql("$db", "select * from trivia WHERE sendDate = '$sendDate'");

    to this:

    $dateResult = mysql_query("select * from trivia WHERE sendDate = '$sendDate'");

    This will work if the script has already created a connection to the database in one of those "included" files (globals.php and functions.php, mentioned on top).
    Otherwise this will give you an error, like connection failed, resource missing, something like that.

    by the way, this is not a good script for many reasons.
    Also, will completely fail on newer PHP versions like on PHP7, because "mysql_query" also got removed from PHP.

    You should get someone to change the script to use "mysqli" functions instead of this.
    But before doing this, make sure that mysqli functions are there in your PHP version, otherwise your current server will not be able to run the script at all.
    You are using an old version of PHP.
    Most hosts are now running PHP 7.3
     
    JEET, Jul 22, 2020 IP
  9. captainron19

    captainron19 Active Member

    Messages:
    49
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    93
    #9
    Thanks...... changed that line but the error still shows up as shown above
     
    captainron19, Jul 23, 2020 IP
    JEET likes this.
  10. JEET

    JEET Notable Member

    Messages:
    3,352
    Likes Received:
    371
    Best Answers:
    16
    Trophy Points:
    235
    #10
    Hi,
    Change this line also:
    $catResult = mysql("$db","select * from category ORDER BY name");

    to:
    $catResult = mysql_query("select * from category ORDER BY name", $db);

    and this one also:
    $dateResult = mysql("$db", "select * from trivia WHERE sendDate = '$sendDate'");

    to this:
    $dateResult = mysql_query("select * from trivia WHERE sendDate = '$sendDate'", $db);


    If you get an error like, cannot connect to database, or permission denied, then you will need to connect to database first. That is another 2 lines of code.
     
    JEET, Jul 23, 2020 IP
  11. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    6,407
    Likes Received:
    1,368
    Best Answers:
    24
    Trophy Points:
    400
    #11
    @JEET Shouldn't it be mysqli_query instead of mysql_query? mysql_query will still throw the same error.

    The $num_rows_date = mysql_numrows($dateResult); should also be adapted to mysqli_ as well as error reporting: mysql_error()

     
    qwikad.com, Jul 24, 2020 IP
    JEET likes this.
  12. JEET

    JEET Notable Member

    Messages:
    3,352
    Likes Received:
    371
    Best Answers:
    16
    Trophy Points:
    235
    #12
    @qwikad.com
    He is still running php 5.4
    I am not sure if he has the mysqli extension installed.
    MySQL extension is available in that version of php.
     
    JEET, Jul 25, 2020 IP
  13. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    6,407
    Likes Received:
    1,368
    Best Answers:
    24
    Trophy Points:
    400
    #13
    mysqli_ is supported by php 5.4. As long as it's 5 and above.
     
    qwikad.com, Jul 25, 2020 IP
    JEET likes this.
  14. JEET

    JEET Notable Member

    Messages:
    3,352
    Likes Received:
    371
    Best Answers:
    16
    Trophy Points:
    235
    #14
    I know, but am not sure if his server has it.
    Plus, he will need to change entire thing to make it compatible with mysqli, the method to connect to database. It won't be about just this query then.
     
    JEET, Jul 25, 2020 IP