Convert PHP5 mysql_query() code to PHP7 mysqli_query()
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

I have a form that I'm using to enter students grades that I created before mysql_query() was depreciated. I would like to have the following code work with mysqli_query($mysqli,) where $mysqli is my db info which is supported by PHP7.

foreach ($_POST['profiles'] as $profile) { 
    $query = sprintf("INSERT INTO grades (course, campus_id, $type_of_grade, title, url, points_earned, point_total, comments) 
                  VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s' )", 
                  $profile['course'], $profile['campus_id'], '1', $_POST['title'], $_POST['url'], $profile['points_earned'], $_POST['point_total'], $profile['comments']
                );
$result = mysql_query($query); 
}
awarded to feroldi
Tags
php
php7

Crowdsource coding tasks.

2 Solutions

Winning solution

You'll need to use PDO (PHP Data Object), which is a better way, suited for accessing database from php nowadays.

To create a new object for your database, you'll use the PDO class, thus creating a new instance of it:

$db = new PDO(
    'mysql:host=HOST;dbname=DBNAME', // host, database
    'USER', // user goes here
    'PASSWORD', // password goes here
    array(
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_PERSISTENT => false
    )
);

You may access your database to read, modify etc, from this new PDO instance (which was stored in $db).

Doing a query command is as easy as $db->query(command). So just create your new PDO object with your parameters, and change $result = mysql_query($query); to $result = $db->query($query);

EDIT: If it's not worth it to make such change, you may also use mysqli with this method.

I want it converted to mysqli_query() not PDO since I don't understand PDO and don't plan on learning it. I just want to update this one last snippet to something familiar. Also, you didn't supply enough PDO code for me to simply copy and paste to make it work.
imokyourok almost 3 years ago
Just change from mysql_query to mysqli_query then :)
feroldi almost 3 years ago
The first thing I tried was $result = mysqli_query($query); and that didn't work.
imokyourok almost 3 years ago
Have you changed all functions from mysql to mysqli?
feroldi almost 3 years ago
I got it to work when I changed $result = mysql_query($query); to $mysqli->query($query);
imokyourok almost 3 years ago
Great! Just a tip: you can actually modify every mysql_* function to mysqli_*. However, some functions have slightly different parameters. So check out this link to guarantee non-breaking changes. The case with mysqli_query is that it expects a link to your mysql database as a parameter, followed by a query command. Sorry for any confusion.
feroldi almost 3 years ago

if (!$result = $mysqli->query($sql)) {
echo "Sorry, the website is experiencing problems.";
exit;
}

Check out the example here: Mysqli examples
Note: $mysqli here is defined with the db connection.
e.g:
$mysqli = new mysqli('127.0.0.1', 'your_user', 'your_pass', 'sakila');

So modification that your code needs is:
foreach ($_POST['profiles'] as $profile) {
$query = sprintf("INSERT INTO grades (course, campus_id, $type_of_grade, title, url, points_earned, point_total, comments)
VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s' )",
$profile['course'], $profile['campus_id'], '1', $_POST['title'], $_POST['url'], $profile['points_earned'], $_POST['point_total'], $profile['comments']
);
$result = $mysqli->query($query);
}

Define $mysqli

View Timeline