Hi, trying to get connected to a database I have set up and see the contents.
I got some code setup and it seems to be connecting, but the output just gives the array # and the word array like this:
User: Conjurer - Password: 3x1yzn1
Attempting to connect to database
Connected successfully! <<<--- this line prints even when I don't connect!
Number of rows: 231
0: Array
1: Array
2: Array
3: Array
4: Array
5: Array
6: Array
7: Array
8: Array
9: Array
The code I am using is here:
<?php
session_start();
//create short variable names
$username ="Conjurer";
$passwd = "3x1yzn1";
// include function files for this application
//require_once('includes/member_fns.php5');
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Test for OMGMA Connection</title>
</head>
<body>
<?php
echo "<p>User: $username - Password: $passwd </p>";
echo "<p>Attempting to connect to database</p>";
$db= new mysqli("localhost", "omgma_omgmauser", "3x1yzn1", "omgma_members") or die ('I cannot connect to the database because: ' . mysql_error());
if ($db)
{
echo "<p>Connected successfully!</p>"; //this seems to print whether I connect or not!
}
else
{ echo 'Error: could not connect to database @ line 29';
}
$query = "select user_id, last_name, first_name from directory ";
$result = mysqli_query($db, $query);
$num_results = mysqli_num_rows($result);
echo "<p>Number of rows: $num_results</p>";
for ($i=0; $i <10; $i++)
{
$row = mysqli_fetch_assoc($result);
echo "<p>$i: $row</p>";
}
?>
</body>
</html>
My select query was to pull three fields: user_id, last_name, first_name
What do I need to change so I can see the first few rows of these three fields?
Also, why is this test for the connection not working? It executes the echo "<p>Connected successfully!</p>"; whether I did or didn't connect successfully.
if ($db)
{
echo "<p>Connected successfully!</p>"; //this seems to print whether I connect or not!
}
else
{ echo 'Error: could not connect to database @ line 29';
}
Thanks, getting close I think. 🙂