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

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
jslick
Forum Commoner
Posts: 35
Joined: Wed Jul 21, 2004 11:18 am

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

Post 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.
Last edited by jslick on Wed Jul 21, 2004 11:28 am, edited 1 time in total.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
jslick
Forum Commoner
Posts: 35
Joined: Wed Jul 21, 2004 11:18 am

Post by jslick »

I think that my username and database name are the same, though.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
jslick
Forum Commoner
Posts: 35
Joined: Wed Jul 21, 2004 11:18 am

Post by jslick »

I don't have Shell access. I have phpMyAdmin. I had tried local_host but it didn't work.
jslick
Forum Commoner
Posts: 35
Joined: Wed Jul 21, 2004 11:18 am

Post by jslick »

I got it. I am using localhost. I originally used local_host. I feel like an idiot. Thanks pickle :) :!:
jslick
Forum Commoner
Posts: 35
Joined: Wed Jul 21, 2004 11:18 am

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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())
jslick
Forum Commoner
Posts: 35
Joined: Wed Jul 21, 2004 11:18 am

Post 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.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

You need to quote values, eg
VALUES('$FirstName','$LastName','$Password')";

without the quotes it treats them as column names.
jslick
Forum Commoner
Posts: 35
Joined: Wed Jul 21, 2004 11:18 am

Post 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 :!:
Post Reply