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... 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
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
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
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.
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
Post the entire code and ask someone to rewrite it for mysqli_ It may work without changing anything else.
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):
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
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 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 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.
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.