Mysql to myqli errors

hi all im attempting to change my site from mysql to mysqli with the help of google but im stuck on this section of what to change
if(! function_exists(getMemberType) )
{
function getMemberType($userid)
{
$result = mysqli_query(" select memtype from user where user_id = ‘$userid’ ");
if ($line = mysql_fetch_array($result)) {
$response = $line[0];
}
return $response;
}
}

can anyone tell me what ive got to change as at the moment im getting the following error:
Warning: mysqli_query() expects at least 2 parameters, 1 given in /home/matureco/public_html/config/db_connect.php on line 211

Warning: mysql_fetch_array() expects parameter 1 to be resource, null given in /home/matureco/public_html/config/db_connect.php on line 212

and also on this section below
// find total message per user
if(! function_exists(unReadMsgCount) )
{
function unReadMsgCount($id)
{
return mysqli_query(“select * from messages where rece_id='”.$id.“’ and readflage = 0 and status=1”)->num_rows;
}
}
and for that section im currently getting
Warning: mysqli_query() expects at least 2 parameters, 1 given in /home/matureco/public_html/config/db_connect.php on line 68

any help would be much appreciated ty jan x

Hi @janice_robinson and welcome to the forum.

The function is missing the connection resource.

Check the procedural examples in these links:

http://www.nusphere.com/kb/phpmanual/function.mysqli-query.htm

http://php.net/manual/en/mysqli.query.php

Instead of changing to mysqli, you have to change to PDO. Just mechanical switching will do no good for you, leaving all the dangers the same. The goal of changing have to be the use of prepared statements. But mysqli prepared statements are too complex to use and you left to choice but PDO. Here gous your function written in PDO:

function getMemberType($userid, $pdo)
{
    $stmt = $pdo->prepare("select memtype from user where user_id = ?");
    $stmt->execute([$userid]);
    return $stmt->fetchColumn();
}

Note that you are using wrong approach with your second function. Instead of selecting all the records, you have to ask the database to count them. Here goes the code

function unReadMsgCount($id. $pdo)
{
    $sql = "select count(*) from messages where rece_id=? and readflage = 0 and status=1";
    $stmt = $pdo->prepare($sql);
    $stmt->execute([$id]);
    return $stmt->fetchColumn();
}

You may learn PDO from this article, and you will see how much safer and cleaner your code will be.

ok for PDO ive just been looking at the following code and trying to change it to PDO
$dbh = new PDO(DBHOST,DBUSER,DBPASS) or die(mysql_error());
$dn = mysql_select_db(DBNAME,$conn) or die(mysql_error());

$row = mysql_fetch_array(mysql_query("select * from settings where id = ‘1’ "));
$site_name = trim(stripslashes($row[‘site_name’]));
$email = trim($row[‘email’]);
$keyword = trim(stripslashes($row[‘keyword’]));
$description= trim(stripslashes($row[‘description’]));
$logo = trim($row[‘logo’]);
$copyright = trim(stripslashes($row[‘copyright’]));
and had a look through the link and tried a few bit out and got Fatal error: Uncaught exception ‘PDOException’ with message ‘invalid data source name’ in /home/matureco/public_html/config/db_connect.php:5 Stack trace: #0 /home/matureco/public_html/config/db_connect.php(5): PDO->__construct(‘localhost’, ‘dbusername’, password’) #1 /home/matureco/public_html/index.php(2): include(‘/home/matureco/…’) #2 {main} thrown in /home/matureco/public_html/config/db_connect.php on line 5

but ive since changed it back to mysql can you show to put in code added

ty jan

Well, from the link you should have learned how to connect to PDO. It is not like just changing the function names. You have to follow the example and adapt it for your credentials. Here goes the corrected code

$opt  = array(
    PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
);
$dsn = 'mysql:host='.DBHOST.';dbname='.DBNAME.';charset=utf8';
$pdo = new PDO($dsn, DBUSER, DBPASS, $opt);

$row = $pdo->query("select * from settings where id = '1' ")->fetch();
extract($row);

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.