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...