-2

Following a tutorial, how would I convert this to mysqli? I found the error that it was mysql_query that wasn't supported anymore, so I already switched it to mysqli_query, but now I'm stumped on where to move on from there. How would I return that function?

function user_exists($username) {
$username = sanitize($username);
$query = mysqli_query("SELECT COUNT('user_id') FROM 'users' WHERE 'username' = '$username'");
return (mysql_result($query, 0) == 1) ? true : false; }

function user_id_from_username($username) {
$username = sanitize($username);
return mysql_result(mysql_query("SELECT 'user_id' FROM 'users' WHERE 'username' = '$username'"), 0, 'user_id'); }

Future thanks.

  • Please read the fantastic manual at http://www.php.net//manual/en/book.mysqli.php as it will tell you what you need to do. – Jay Blanchard Jun 11 '14 at 19:19
  • Bad coding: **NEVER** chain DB queries. `mysql_result(mysql_query())` will fail at some point, and since you blindly assume nothing can ever go wrong, you'll never find out until some OTHER code that calls this function fails. – Marc B Jun 11 '14 at 19:29

1 Answers1

0

Sky is wrong with his assupmtions

You need to do a few things

  1. The basic syntax for a mysqli_query is mysqli_query($link,$query). This means that you need to change your query statements to mysqli_query($link,"YOUR QUERY TEXT HERE").
  2. In order for the above to work you need to change your database connection variable. You used to have $link=mysql_connect("host","username","password"). You need to change that to $link=mysqli_connect("host","username","password","database")
  3. Then you need to change all mysql_result to mysqli_result

All of what I have written above is publicly availible on the PHP.net resource http://www.php.net//manual/en/book.mysqli.php

If you need to know anything about mysqli check there first

MarshallOfSound
  • 2,629
  • 1
  • 20
  • 26