Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

@mysql_select_db($database) fails

1,021 views
Skip to first unread message

Paul

unread,
Jun 8, 2010, 8:48:57 PM6/8/10
to
Help.
Im trying to get MySQL,Apache,PHP working on my system to learn about
them.
I have succeded in getting Apache and PHP to read and process test
scripts.
However I'm having trouble trying to create and access a MySQL database.

My system:
iMac G5 2mhz 1gb running Mac OS X 10.4.11, xcode2.5.
MySQL 5.1.40 for osx10.4 powerpc in /usr/Local/mysql
Apache 2.0.63 in /Library/Apache2
PHP 5.2.13 in /usr/Local/php

Edit httpd.conf and define UserDir as Sites (for Mac OS)
Put my test.php in Sites and used http://localhost/~mac/test.php.

What I do:
1) I started Apache and enabled personal web sharing.

2) I started MySQL using SystemPreferences panel.

3) Ran mysql to set password and flush privileges:
shell:/usr/local/mysql mac$ bin/mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 21
Server version: 5.1.40 MySQL Community Server (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the current input
statement.

mysql> UPDATE mysql.user SET Password = PASSWORD('localtest') WHERE
User = 'root';
Query OK, 3 rows affected (0.03 sec)
Rows matched: 3 Changed: 3 Warnings: 0

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

mysql> exit;
Bye

4) Ran a test script to setup a database:

<?
$username="root";
$password="localtest";
$database="contacts";

mysql_connect("localhost",$username,$password);
echo "localhost".":".$username.":".$password." ";
echo mysql_error();
@mysql_select_db($database) or die( "Unable to select database");
$query="CREATE TABLE contacts (id int(6) NOT NULL auto_increment,first
varchar(15) NOT NULL,last varchar(15) NOT NULL,phone varchar(20) NOT
NULL,mobile varchar(20) NOT NULL,fax varchar(20) NOT NULL,email
varchar(30) NOT NULL,web varchar(30) NOT NULL,PRIMARY KEY (id),UNIQUE
id (id),KEY id_2 (id))";
mysql_query($query);
mysql_close();
echo "Database created";
?>

The output was:
localhost:root:localtest Unable to select database

5) When I looked at the variable MySQL had it shows:

mysql> SELECT User, Host, Password FROM mysql.user;
+------+--------------------+-------------------------------------------
+
| User | Host | Password
|
+------+--------------------+-------------------------------------------
+
| root | localhost | *A8C80825CADD2829AF643443276C24D462918054
|
| root | Paul-iMac-G5.local | *A8C80825CADD2829AF643443276C24D462918054
|
| root | 127.0.0.1 | *A8C80825CADD2829AF643443276C24D462918054
|
| | localhost |
|
| | Paul-iMac-G5.local |
|
+------+--------------------+-------------------------------------------
+
5 rows in set (0.06 sec)

Questions:
What the ^%$#^% is this passwword *A8C80825C...? Is it "localtest"
encrypted?
How does the database get created? By @mysql_select_db($database)?
How can you "select" a database before you "create" it?

Any suggestions as to what is wrong?

Thanks,
--Paul

Norman Peelman

unread,
Jun 8, 2010, 10:20:38 PM6/8/10
to

First of all you are suppressing the error messages by using '@'.
Try:

$dblink = mysql_connect('localhost',$user,$password);
if (!$dblink)
{
die('Not connected to database!');
}
$db = mysql_delect_db($database,$dblink);
if (!$db)
{
die("Cannot use database '$database':".mysql_error());
}

Second, those are the passwords as a hash. MySQL concerts the
password to a hash before comparison.

Third, you must create a database before you can 'select' it. You can
do it from the MySQL console or from within a script. Then select it.

--
Norman
Registered Linux user #461062
-Have you been to www.mysql.com yet?-

Gordon Burditt

unread,
Jun 8, 2010, 11:38:50 PM6/8/10
to
>Help.
>Im trying to get MySQL,Apache,PHP working on my system to learn about
>them.
>I have succeded in getting Apache and PHP to read and process test
>scripts.
>However I'm having trouble trying to create and access a MySQL database.

I recommend that while you are learning, you do not use @ in front
of a function (any function) to suppress error messages, and that
you print out the result of mysql_error() when anything MySQL-related
might fail.

For production work, you suppress the error messages or log them in
a file rather than as part of the output.

The more modern way of setting a password is
mysql> set password for root@localhost = password('localtest');
although this will not accomplish the wildcard to get all the
root accounts like your update did.

>4) Ran a test script to setup a database:

This does *not* try to create a database.

What did you set it to with your UPDATE query? password('localtest')
It's a hashed password.
Try:
select password('localtest');
in a command-line client.

>How does the database get created?

With CREATE DATABASE.
Fire up a command-line client and try the query:
show databases;
to see whether a database already exists.

>By @mysql_select_db($database)?
No!!

>How can you "select" a database before you "create" it?

You don't. You create the database first (once: creating it will fail
the second time unless you drop it first, and databases are usually fairly
permanent). Often using a command-line client. The standard
installation procedure creates the administrative 'mysql' database,
and maybe a database called 'test'. It's up to you to create others.

You do not need to select a database for operations that don't need
one. For example, you can run:
select 2+2;
or
select password('localtest');
without a current database because it doesn't use tables.
You can also run "create database foo;" without selecting a database,
*then* select it.


Michael John Ruff

unread,
Jun 9, 2010, 5:46:26 AM6/9/10
to
Hello

Yes he A8C80825C... are encrypted password, in your dump every user have
to have a password, in your case

To create a database your have to use for example "create database sysops;"

You cannot select a database before you create it.

Mike


--
Regards Mike
MSCE, CCNA, Network+, A+, RHCE.
Yes Posting from Windows, with lots of Linux Experience.

Paul

unread,
Jun 10, 2010, 6:20:27 PM6/10/10
to
In article <080620102048578182%pa...@yahoo.com>, Paul <pa...@yahoo.com>
wrote:

> Help.
> Im trying to get MySQL,Apache,PHP working on my system to learn about
> them.
> I have succeded in getting Apache and PHP to read and process test
> scripts.
> However I'm having trouble trying to create and access a MySQL database.
>
> My system:
> iMac G5 2mhz 1gb running Mac OS X 10.4.11, xcode2.5.
> MySQL 5.1.40 for osx10.4 powerpc in /usr/Local/mysql
> Apache 2.0.63 in /Library/Apache2
> PHP 5.2.13 in /usr/Local/php
>

Thanks Norman, Gordon and Michael for your helpful comments.
I was able to create table "contacts" in existing database "test".
Now I'm trying to add rows to the table and display the results.
I've beeen following an example that I think is rife wthh errors
and omissions so I could use some additional help.

The insert script looks like this:
<form method="post">
First Name: <input type="text" name="$first"><br />
Last Name: <input type="text" name="$last"><br />
Phone: <input type="text" name="$phone"><br />
Mobile: <input type="text" name="$mobile"><br />
Fax: <input type="text" name="$fax"><br />
E-mail: <input type="text" name="$email"><br />
Web: <input type="text" name="$web"><br />
<input type="Submit">
</form>

<?
include("dbinfo.inc.php");

$dblink = mysql_connect('localhost',$username,$password);


if (!$dblink)
{
die('Not connected to database!');
}

$db = mysql_select_db($database,$dblink);
if (!$db)
{
die("Cannot select database '$database':".mysql_error());
}

$query = "INSERT INTO contacts VALUES
('','$first','$last','$phone','$mobile','$fax','$email','$web')";
$db = mysql_query($query);
if (!$db)
{
die("Cannot query database '$database':".mysql_error());
}

mysql_close();

?>

The display script looks like this:
<?
include("dbinfo.inc.php");
$dblink = mysql_connect('localhost',$username,$password);


if (!$dblink)
{
die('Not connected to database!');
}

$db = mysql_select_db($database,$dblink);


if (!$db)
{
die("Cannot use database '$database':".mysql_error());
}

$query="SELECT * FROM contacts";
$result=mysql_query($query);
if (!$result)
{
die("Cannot query database '$database':".mysql_error());
}

$num=mysql_numrows($result);
mysql_close();

echo "<b><center>Database Output</center></b><br><br>";

?>
<table border="0" cellspacing="2" cellpadding="2">
<tr>
<th><font face="Arial, Helvetica, sans-serif">Name</font></th>
<th><font face="Arial, Helvetica, sans-serif">Phone</font></th>
<th><font face="Arial, Helvetica, sans-serif">Mobile</font></th>
<th><font face="Arial, Helvetica, sans-serif">Fax</font></th>
<th><font face="Arial, Helvetica, sans-serif">E-mail</font></th>
<th><font face="Arial, Helvetica, sans-serif">Website</font></th>
</tr>

<?
$i=0;
while ($i < $num) {
$first=mysql_result($result,$i,"first");
$last=mysql_result($result,$i,"last");
$phone=mysql_result($result,$i,"phone");
$mobile=mysql_result($result,$i,"mobile");
$fax=mysql_result($result,$i,"fax");
$email=mysql_result($result,$i,"email");
$web=mysql_result($result,$i,"web");
echo "Test="."$first"." "."$last";
?>

<tr>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$first $last";
?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$phone";
?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$mobile";
?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$fax";
?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><a href="mailto:<? echo
'$email'; ?>">E-mail</a></font></td>
<td><font face="Arial, Helvetica, sans-serif"><a href="<? echo "$web";
?>">Website</a></font></td>
</tr>
<?
++$i;
}
echo "</table>";


?>


When I run the insert script it presents the form and accepts the input.
When I run the display script I just get the headings and blank lines
for each row. It seems to increment the number of rows for each insert
I enter. So it's doing something but not inserting the data.

Again any suggestions will be appreciated.
Thanks
--Paul

Kees Nuyt

unread,
Jun 12, 2010, 3:19:36 AM6/12/10
to
On Thu, 10 Jun 2010 18:20:27 -0400, Paul <pa...@yahoo.com> wrote:

>In article <080620102048578182%pa...@yahoo.com>, Paul <pa...@yahoo.com>
>wrote:
>
>> Help.
>> Im trying to get MySQL,Apache,PHP working on my system to learn about
>> them.
>> I have succeded in getting Apache and PHP to read and process test
>> scripts.
>> However I'm having trouble trying to create and access a MySQL database.
>>
>> My system:
>> iMac G5 2mhz 1gb running Mac OS X 10.4.11, xcode2.5.
>> MySQL 5.1.40 for osx10.4 powerpc in /usr/Local/mysql
>> Apache 2.0.63 in /Library/Apache2
>> PHP 5.2.13 in /usr/Local/php
>>
>
>Thanks Norman, Gordon and Michael for your helpful comments.
>I was able to create table "contacts" in existing database "test".
>Now I'm trying to add rows to the table and display the results.
>I've beeen following an example that I think is rife wthh errors
>and omissions so I could use some additional help.

[snip]

You have a programming problem here, not a MySQL / database
problem. You close the connection before retrieving the data.
Closing the connection frees up all buffers.
Move the mysql_close(); statement to just before the php end
tag:

mysql_close();
?>

Best regards,
--
( Kees Nuyt
)
c[_]

0 new messages