Page 1 of 1

[SOLVED] New at MySQL - connecting to DB not working

Posted: Wed Jul 21, 2004 11:18 am
by jslick
I am very new at MySQL. I've been using PHP for a while but finally found the need to learn MySQL. I made a DB and this script:

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head><title>MySQL stuff</title>
<link rel="stylesheet" href="/my_style.css" />
</head>
<body>
<table>
<tr style="font-weight:bold;"><td>ID</td><td>First Name</td><td>Last Name</td><td>Password</td></tr>
<table
<?php
$link=mysql_connect("69.72.142.82","[b]*****[/b]_newdb","[b]*****[/b]");
$db=mysql_select_db("[b]*****[/b]_newdb",$link);
$query="SELECT * FROM people";
$result=mysql_query($query,$link);
$numrows=mysql_num_rows($result);
for ($i=0; $i<$numrows; $i++){
$the_arrays=mysql_fetch_array($result);
print("<tr><td>$the_arrays[ID][$i]</td><td>$the_arrays[FirstName][$i]</td><td>$the_arrays[LastName][$i]</td><td>$the_arrays[Password][$i]</td></tr>");
}
?>
</table></body></html>
I get this:
ID First Name Last Name Password
Warning: mysql_connect(): Access denied for user: 'taken out by author' (Using password: YES) in /home/PATH/people.php on line 12

Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link resource in /home/PATH/people.php on line 13

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/PATH/people.php on line 15

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/PATH/people.php on line 16
I'm pretty sure the connection isn't working.

I edited some stuff, too - the stuff in bold.

Edit: changed "code" tag to "php" tag.

Posted: Wed Jul 21, 2004 11:25 am
by pickle
Hi, Welcome to Devnetwork

First things first: Put your code in [syntax=php][/syntax] tags - it colours the code as well which makes it much easier to read :).

Your problem is that when you call mysql_connect, you are sending (I think) the database you want to connect to, and a password. You need to send a username instead of the database.

Posted: Wed Jul 21, 2004 11:30 am
by jslick
I think that my username and database name are the same, though.

Posted: Wed Jul 21, 2004 11:39 am
by pickle
Can you login through the command line using that username and password? Also, if you're trying to access a remote database, make sure the username you are using is allowed to login from wherever you're coming from.

Posted: Wed Jul 21, 2004 11:44 am
by jslick
I don't have Shell access. I have phpMyAdmin. I had tried local_host but it didn't work.

Posted: Wed Jul 21, 2004 11:46 am
by jslick
I got it. I am using localhost. I originally used local_host. I feel like an idiot. Thanks pickle :) :!:

Posted: Wed Jul 21, 2004 12:39 pm
by jslick
Well I got that working but now I am trying to add to that database:

Code: Select all

<?php
$FirstName=$_POST['FirstName'];
$LastName=$_POST['LastName'];
$Password=$_POST['Password'];
$link=mysql_connect("localhost","[b]*****[/b]_newdb","[b]*****[/b]");
$db=mysql_select_db("[b]*****[/b]_newdb",$link);
$query="INSERT INTO people (FirstName, LastName, Password)"
."VALUES($FirstName,$LastName,$Password)";
mysql_query($query);
mysql_close($link);
header("Location:people.php");
?>
It doesn't give me an error but it doesn't add it to the database.

Posted: Wed Jul 21, 2004 12:53 pm
by feyd
it doesn't give you an error because you didn't ask it to..

Code: Select all

mysql_query($query) or die(mysql_error())

Posted: Wed Jul 21, 2004 1:03 pm
by jslick
Okay, I put that in and got "Unknown column 'Guest' (that is what I put for $FirstName) in 'field list'.

EDIT: I don't want it to look for a column named Guest, I want to make a row with the $FirstName to be Guest.

Posted: Wed Jul 21, 2004 1:12 pm
by markl999
You need to quote values, eg
VALUES('$FirstName','$LastName','$Password')";

without the quotes it treats them as column names.

Posted: Wed Jul 21, 2004 1:25 pm
by jslick
markl999 wrote:You need to quote values, eg
VALUES('$FirstName','$LastName','$Password')";

without the quotes it treats them as column names.
It worked! Thanks markl999 :!: