Using PHP cannot find the database but it's there

Hello,

Really new to using PHP. Used Java in the past, and looking to getting into becoming a web developer.

Okay, so I have already hit several road blocks, but I managed to get past them all besides this. Using mysql, I type the command “show databases” I clearly see the database I created called “Test2”. I have already established a successful connection to my server using the generic code:

“$link = mysql_connect($hostname,$username,$password);”

I get a successful connection. The problem is selecting the database I created. I keep getting this message " unable to find my database".

The code I am using is pretty straightforward.
if (!mysqli_select_db($link,‘test2’))

{

$output = ‘Unable to locate the test2 database.’;

include ‘output.html’;

exit();

Like I said, I can see it using this command in mysql “show databases”., but I am unable to figure why I am not able to connect to it, and pull data from it. It really is frustrating.

Please can anyone show me how to do this correctly.

I again attempted to adjust my code to this:
<?php
$username = “Jamal”;
$password = “red!2345”;
$hostname = “Jamal-pc”;
$name = mysql_connect($hostname,$username,$password)
or die(“Unable to connect to MySQL”);
echo “Connected to MySQL<br>”;

mysqli_select_db($name,“test”) or die(“can’t find it”);

?>

Straightforward and simple, but I still getting an error message " mysqli_select_db() expects parameter 1 to be mysqli, resource given in C:\wamp\www\index.php on line 13.

At this point, I am just shooting in the dark today. Really unproductive day spent all morning with “PHP” and haven’t learned anything. It’s really frustrating spending your day off trying to accomplish something and just completely blew the day away. Sorry to rant, but I feel better and would feel a great deal better if I understand what I am doing wrong here.

Thanks.

Hi,

The problem is that you’re using functions from two different database extensions. mysql_connect is part of the mysql extension, while mysqli_select_db is from the mysqli extension. The mysql stuff is depreciated anyway and will be removed from PHP at some point, so you’d be better off sticking with mysqli:


$link = mysqli_connect($host, $username, $password, 'test2');

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\
", mysqli_connect_error());
    exit();
}

Thank you for your prompt response.

Alright, so I have a successful connection to my database. How would I go about accessing it from the code you given me. As I am truly lost, at this point.

What functions should I be using to just select the database? If you could kindly answer that, I would be so happy and salvage the rest of my day.

Thanks

The code example in my previous post connects to the DB server and selects the database you want to use. If you wanted to change to another DB after making the initial connection, then you would use the mysqli_select_db function as in your first post.

Hope you’re still around, I am still stuck.

I used your original code to connect to my database and I have a suspicion that something is wrong. When I run a query, it yields me nothing but just errors.

Here is the small code I am running
<?php
$username = “Jamal”;
$password = “red!2345”;
$hostname = “Jamal-pc”;
$mysqli= mysql_connect($hostname,$username,$password,‘japanesewords’)
or die(“Unable to connect to MySQL”);
echo “Connected to MySQL<br>”;
$result = mysqli_query($mysqli, ‘SELECT * FROM japanesedefinition’);

?>
I made use that everything is correct but I am still getting error messages: : mysqli_query() expects parameter 1 to be mysqli, resource given.

I have tried to use google and not be lazy, but I have been at this code all day. Please let me know, if I am doing something wrong.

Take a good look at fretburn’s example again.


$username = "Jamal";
$password = "red!2345";
$hostname = "Jamal-pc"; 
$mysqli= mysql[COLOR="#FF0000"][b]i[/b][/COLOR]_connect($hostname,$username,$password,'japanesewords')
  or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
$result = mysqli_query($mysqli, 'SELECT * FROM japanesedefinition');  

You have the highlighted character missing.

Thank you for your help.

I understand that “typo” now.

It appears that I am still having trouble with this database. Can you look at this code:
$test=$mysqli= mysqli_connect($hostname,$username)
or die(“Unable to connect to MySQL”);
echo ‘it worked’;
$result = mysqli_select_db($test,‘japanesedefinition’);
$result = mysqli_query($test,“SELECT * FROM Japanesewords”);
while($row = mysqli_fetch_array($result))
{
echo $row[‘FirstName’] . " " . $row[‘LastName’];
echo “<br>”;
}

?>
Alright, the error message I am getting is this:“mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\wamp\www\index.php on line 11”

It’s the story of my day today.

Thanks in advance. I just want to make sure it works.

You have not defined all the connection fields.

mysqli_connect($hostname,$username,$password,'japanesewords')

So I did some error handling
the problem appears to be is that I am able to connect to the server using my username etc, but I cannot find the database. In mysql, I am positive the table exists, but am unable to access it using my code. It’s really frustrating getting this to work. It seems so straight forward where am I failing at this.

<?php
$username = "monty";
$password = "";
$hostname = "Jamal-pc"; 
$test=$mysqli= mysqli_connect($hostname,$username)
  or die("Unable to connect to MySQL");
echo 'it worked';

if (!$test) 

{ 

  $output = 'Unable to connect to the database server.'; 

  include 'output.html'; 

  exit(); 

}
mysqli_select_db($test,'japanesedefinition');
if(!mysqli_select_db($test,'japanesewords'));{
 $output = 'Unable to locate the  database.'; 

  include 'output.html'; 

  exit(); 
}
?>

It fires the error, but I am to new at this to really have a deep insight on what I am doing here. It also sucks that I don’t have a teacher to ask. So I give you a million thanks for helping me. It really does mean a lot.

You are still failing to include ALL the parameters needed by the connect statement. The first THREE are mandatory and you are only supplying the second and third.

I have since correct that mistake
$test=$mysqli= mysqli_connect($hostname,$username,$password)
or die(“Unable to connect to MySQL”);
echo ‘it worked’;

if (!$test)

{

$output = ‘Unable to connect to the database server.’;

include ‘output.html’;

exit();

}
mysqli_select_db($test,‘japanesewords’);
if(!mysqli_select_db($test,“test”));{
$output = ‘Unable to locate the database.’;

include ‘output.html’;

exit();
}
?>

And how do you do with this simple test

<?php	
$mysqli = mysqli_connect($hostname,$username,$password,'japanesewords');
if (mysqli_connect_errno()){
	$output = 'Unable to connect to the database server.';
	include 'output.html';
	exit();
}

$query = "SELECT * FROM japanesedefinition";
$result = $mysqli->query($query);
while ($row = $result->fetch_array(MYSQLI_ASSOC)){
	echo "<pre>";
	print_r($row);
	echo "</pre>";
}
?>

it worked, but how?

I am so happy, but I need to understand how and why it worked? I think this presents me with a tremendous opportunity to learn.

Thanks

The hostname parameter tells the script which computer to look at to find the database.

localhost identifies the computer as the one the script is running on but provided that the appropriate port is open on the computer running the database you can remote connect to it using a script running on a different computer by specifying an appropriate value in hostname to identify that remote computer.

I understand that. My problem is I don’t understand Drummin code at all. Granted, I been looking at PHP for 3 days now, but all examples I see online don’t use the functions he is using. I have no idea what this mean" $result = $mysqli->query($query);" and this “while ($row = $result->fetch_array(MYSQLI_ASSOC))”. Specifically, I don’t understand the what is placed in the constructor of the array. For the examples, I have seen online, $result should be placed on the constructor. If you could kindly point me to the direction on where I can familiarize myself with this code or if you could explain to me like I was a 2 year old. I really want to know this. It’s 3 am in the morning my time and I won’t be able to sleep.

Did some digging is he using php like an object oriented language PDO to be precise. Using Java and looking at some notes online it is making a lot more sense.

If there is anything you could add, I would greatly appreciate it.

I do need to get to sleep.

THANK YOU ALL. If I was a millionaire, I would def. buy everyone here a new computer. I really do appreciate this. Not sure on how else to show my gratitude online, but to say thanks a million times.