So I have been succesful at establishing a database using mysql, but now I want to try to update my code and begin using mysqli...

Here is the code I was given on here to work with...


<?php

// MySQL Server settings
$server = 'samplename';
$user = 'sampleuser';
$pass = 'samplepassword';
$dbname = 'sampledatabase';

/**
* Procedural MySQLi
*/
// First we connect
$conn = mysqli_connect($server,$user,$pass,$dbname);

// Check connect failed
if( !$conn )
{
   // if it did stop here and tell us why
   die('There was a problem connecting to MySQL:<br>('.mysqli_errno($conn).') '.mysqli_error($conn));
}

// Define a query to run
$query = "SELECT * FROM user WHERE username='$username'";

// Query the database
$result = mysqli_query($conn,$query);

// Check if the query failed
if( !$result )
{
   die('There was a problem executing the query ('.$query.'):<br>('.mysqli_errno($conn).') '.mysqli_error($conn));
}

// Check if there are results
if( mysqli_num_rows($result) > 0 )
{

   // Loop through the resulting rows
   while( $row = mysqli_fetch_assoc($result) )
   {
      // output a column
      echo $row['col'];
   }
}
else
{
   // No results
   echo 'No results found';
}

// Close the connection
mysqli_close($conn);


/**
* Object-Oriented MySQLi
*/
// First we connect
$conn = new mysqli($server,$user,$pass,$dbname);

// Check if the connection failed
if( $conn->connect_error )
{
   die('There was a problem connecting to MySQL:<br>('.$conn->errno.') '.$conn->error);
}

// Define a query to run
$query = "SELECT * FROM `table` WHERE `col` = 'value'";

// Query the database
$result = $conn->query($query);

// Check if the query failed
if( !$result )
{
   die('There was a problem executing the query ('.$query.'):<br>('.$conn->errno.') '.$conn->error);
}

// Check if there are results
if($result->num_rows > 0 )
{

   // Loop through the resulting rows
   while( $row = $result->fetch_assoc() )
   {
      // output a column
      echo $row['col'];
   }
}
else
{
   // No results
   echo 'No results found';
}

// Close the connection
$conn->close(); 

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Gear Recycling Site</title>

<META NAME="keywords" CONTENT="" />
<META NAME="description" CONTENT="" />
<link href="gear.css" rel="stylesheet" type="text/css" />

</head>

  <div id="rightcol">
<center><form action="mysql_conn.php" method="POST">
Username:<br />
<input type="text" name="username"><p />
Password:<br />
<input type="password" name="password"><p />

<input type="checkbox" name="rememberme"> Remember me<br />
<input type="submit" name="login" value="Log in" />
</form></center>
  </div>

</div>
</body>
</html>

So I tried to run the script and get started and I get ...

Warning: mysqli::mysqli() [mysqli.mysqli]: (28000/1045): Access denied for user 'sampleusername'@'cgi1701.int.bizland.net' (using password: YES) in /hermes/waloraweb060/b2133/moo.codeadministrator/mysql_conn.php on line 13

Warning: main() [function.main]: Couldn't fetch mysqli in /hermes/waloraweb060/b2133/moo.codeadministrator/mysql_conn.php on line 18

Warning: main() [function.main]: Couldn't fetch mysqli in /hermes/waloraweb060/b2133/moo.codeadministrator/mysql_conn.php on line 18
There was a problem connecting to MySQL:
()

Do I have to do something to tell my server to activate mysqli or should it be able to connect just as the mysql does? It says the error is in line 13 which would be...

$conn = mysqli_connect($server,$user,$pass,$dbname);

Should I have the actual server paths, names passwords here maybe? Thanks for the help guys! Trying to get up to speed so I am not writing old code to annoy people! lol...

    Did you update the 'MySQL Server settings' section at the top of the example code to reflect the correct values?

      // MySQL Server settings
      $server = 'samplename.fatcowmysql.com';
      $user = 'code-a';
      $pass = 'Tatertots';
      $dbname = 'databass';

      Yes, they actually look something like this in the code.

        I only asked since your error message still contained the username 'sampleusername' as if you hadn't done so.

        If you have, then I would say the error is just as the error message says; PHP couldn't connect to the given MySQL server based on the credentials you supplied.

          Yeah, I went and changed that too just to be pc on the board, so originally the error message did have the actual username there. See the thing is though, that my other sql code runs fine.

            code-a;10998388 wrote:

            See the thing is though, that my other sql code runs fine.

            Are you positive that your "other sql code" uses the exact same username and password, connects to the same host, and uses the same database as the example code you're trying to execute above?

              Ok, I thought I had copied and pasted everything exactly. I did find an error in the password. Now it looks like I have cleared the errors, and the login screen will load! Looks like I have a new world to explore! Thanks Brad! I commit to only using mysqli from here forward and thank you guys for keeping me up to speed and doing things right from the start! Stoked to get deeper into the nitty gritty!

                code-a;10998391 wrote:

                I commit to only using mysqli from here forward

                What is you need to connect to a different DBMS? 😛 The next logical step after mysqli procedurally is to learn to use it in OO fashion. Then after that you can begin learning to use PDO.

                  Write a Reply...