Jump to content
Larry Ullman's Book Forums

Recommended Posts

Hi!

I have a question. Script 12.3 (Creating and Selecting a Database) does not work for me with mysqli_. I replace all mysql function with mysqli and it doesn't work. Why is that? (I'm using PHP 5.3.0, MySQL 5.1.36)

Thanks!

Link to post
Share on other sites

Here is the code, what i'm using:

 

<!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" xml:lang="en" lang="en">

<head>

<meta http-equiv="content-type" content="text/html; charset=utf-8" />

<title>Create the Database</title>

</head>

<body>

<?php // Script 12.3 - create_db.php

/* This script connects to the MySQL server. It also creates and selects the database. */

 

// Attempt to connect to MySQL and print out messages:

if ($dbc = @mysqli_connect('localhost', 'root', 'mypassword')) {

 

print '<p>Successfully connected to MySQL!</p>';

 

// Try to create the database:

if (@mysqli_query('CREATE DATABASE myblog', $dbc)) {

print '<p>The database has been created!</p>';

} else { // Could not create it.

print '<p style="color: red;">Could not create the database because:<br />' . mysqli_error($dbc) . '.</p>';

}

 

// Try to select the database:

if (@mysqli_select_db('myblog', $dbc)) {

print '<p>The database has been selected!</p>';

} else {

print '<p style="color: red;">Could not select the database because:<br />' . mysqli_error($dbc) . '.</p>';

}

 

mysqli_close($dbc); // Close the connection.

 

} else {

 

print '<p style="color: red;">Could not connect to MySQL:<br />' . mysqli_error() . '.</p>';

 

}

 

?>

</body>

</html>

 

And here is the result, what I get in the browser:

 

Successfully connected to MySQL!

 

Could not create the database because:

.

 

Could not select the database because:

.

 

The code is exatcly the same, as in the downloadable script 12.3, but I've replaced the "mysql" to "mysqli". With mysqli the database isn't created, but with the old mysql it is. (the password and the username for the connection is correct). ...and i don't get any error report.

 

Thanks for helping!

Link to post
Share on other sites

Ok, here is the error report:

 

Successfully connected to MySQL!

 

Warning: mysqli_query() expects parameter 1 to be mysqli, string given in D:\wamp\www\PHP_for_the_web_EBOOK\ch12_4th_edition\create_database.php on line 18

 

Could not create the database because:

.

 

Warning: mysqli_select_db() expects parameter 1 to be mysqli, string given in D:\wamp\www\PHP_for_the_web_EBOOK\ch12_4th_edition\create_database.php on line 25

 

Could not select the database because:

.

 

The code is the same, as above... (without the @ sign)

Link to post
Share on other sites

I've tried to remove $dbc from "mysqli_query" and "mysqli_select" (i thought that's the problem), then I get this:

 

Successfully connected to MySQL!

 

Warning: mysqli_query() expects at least 2 parameters, 1 given in D:\wamp\www\PHP_for_the_web_EBOOK\ch12_4th_edition\create_database.php on line 18

Could not create the database because:

.

 

Warning: mysqli_select_db() expects exactly 2 parameters, 1 given in D:\wamp\www\PHP_for_the_web_EBOOK\ch12_4th_edition\create_database.php on line 25

 

Could not select the database because:

.

Link to post
Share on other sites

Looking at the PHP manual for the mysqli functions:

http://us.php.net/manual/en/mysqli.query.php

and

http://us.php.net/manual/en/mysqli.select-db.php

 

 

Try reversing the position of the function arguments: $dbc and 'string'

 

e.g.

mysqli_select_db($dbc, 'myblog')
mysqli_query($dbc, 'CREATE DATABASE myblog')

 

 

See if that works.

 

 

 

Zane

 

Yes, that was the problem! With reverse order now it works. But it is strange... In the "create table" script I must use reverse order too and I guess (almost) everywhere with "mysqli".

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...