Closed

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

Moderator: General Moderators

Ultimadark
Forum Newbie
Posts: 9
Joined: Sun Sep 21, 2003 5:57 am

Closed

Post by Ultimadark »

Closed
Last edited by Ultimadark on Mon Sep 22, 2003 3:37 pm, edited 3 times in total.
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

You connect to the MySQL database. Then first check for the username:

Code: Select all

SELECT * FROM "users" WHERE username = '$_POSTї'username'] AND email = '$_POSTї'email']
Then, count how many rows are returned with mysql_num_rows. If it is 0, which it should be, then continue. If there is 1, then redirect to the page that says username is already taken.

-Nay
Ultimadark
Forum Newbie
Posts: 9
Joined: Sun Sep 21, 2003 5:57 am

Post by Ultimadark »

Since im a newb, could u help me out with that code too?
Ive just started with php lol
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

Have you even started on the code?

You should do some research. We're here to help, not do-for-you.

Anyhow, the script. Give it a try first. Then post the not-working script here. We'll help you get it to a running stage. Then you can learn from it.

I'll give you some guidelines to go about doing it. The rest is up to you.

- Connect to your MySQL database (mysql_connect(), mysql_select_db)
- Set a variable for the query
- Execute the query (mysql_query())
- Count the number of rows (mysql_num_rows())
- If row is 0, redirect to success page (header(Location: success.php))
- Else if row is 1, then redirect to error page.

-Nay
Ultimadark
Forum Newbie
Posts: 9
Joined: Sun Sep 21, 2003 5:57 am

Post by Ultimadark »

<?php $con = mysql_connect("localhost", "ultimadark", "************")or die("Connect Error: ".mysql_error());
$db="ultimadark";
mysql_select_db($db, $con);
SELECT * FROM "users" WHERE username = '$_POST['username'] AND email = '$_POST['email']

if (mysql_num_rows > '0' )
{
(header(Location: failedsign.html))
}
else
{
(header(Location: signup.php))
}
mysql_close($con);
}
?>
'0' ) { (header(Location: failedsign.html)) } else { (header(Location: signup.php)) } mysql_close($con); } ?>
This shows on the top of the page, and the script isnt working.

Yes ive started on the code, but i did it last week im still very very much a newbie in php.
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

Okay, good :). Let's see what's wrong. The code below is your code, with certain corrections. Read through it - I left some notes for you.

Code: Select all

<?php

// set sql variables
$host = "localhost";
$user = "ultimadark";
$pass = "************";
$db = "ultimadark";

$con = mysql_connect($host, $user, $pass)or die("Connect Error: ".mysql_error());
mysql_select_db($db, $con);

// set the variable for the query
$q = "SELECT * FROM 'users' WHERE username = $_POST['username'] AND email = $_POST['email']";

// execute the query
$result = mysql_query($q, $con);

// count rows
$rows = mysql_num_rows($result);

if ($rows=="0") {
header(Location: failedsign.html) }
else {
header(Location: signup.php) } 
mysql_close($con);
} 
?>
I used variables to connect to MySQL. It's not necessary but I'd recommend it, if you're doing more than one script. Then you can have a seperate file with the SQL variables and include it when you need to use it. So if there's any change, you can change it quickly and effectively.

-Nay
Ultimadark
Forum Newbie
Posts: 9
Joined: Sun Sep 21, 2003 5:57 am

Post by Ultimadark »

Thanks really for the help man :D Just one more thing. Is this some fault in the script or is it just that my host for mysql is bad (sometimes when i try to log in on phpadmin it says too many connections):

Warning: mysql_connect() [function.mysql-connect]: Too many connections in /web/www/frac/users/ultimadark/signup.php on line 2
Connect Error: Too many connections
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

http://www.mysql.com/doc/en/Too_many_connections.html

What's your website? Run a whois.sc/yourdomain.com and check how many sites are hosted on your server. This is probably since too many people are connecting to the SQL server at the same time.

Well, that's my thing. I think maybe someone has a better solution. mMm

-Nay
Ultimadark
Forum Newbie
Posts: 9
Joined: Sun Sep 21, 2003 5:57 am

Post by Ultimadark »

Yeh i think your right. Now i just gotta get a better mysql host :P
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

If you don't mind me asking, who's your host anyway?

-Nay
Ultimadark
Forum Newbie
Posts: 9
Joined: Sun Sep 21, 2003 5:57 am

Post by Ultimadark »

http://www.frac.dk

i tried mysql hosting at http://www.freesql.org some days ago but i think theyre very slow
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

Oh, free hosting I see. Well, try: http://www.webhosts4free.com/. I found some hosts with PHP and MySQL with no banners there. Anyhow, I'd say get better and better at PHP and advertise yourself. I got my two domains and two reseller hosting accounts by designing a few sites and PHP back-ends for some people.

;-)

-Nay
Ultimadark
Forum Newbie
Posts: 9
Joined: Sun Sep 21, 2003 5:57 am

Post by Ultimadark »

Cool :wink: I wish i were that good
Ultimadark
Forum Newbie
Posts: 9
Joined: Sun Sep 21, 2003 5:57 am

Post by Ultimadark »

Oh and anyway i might get hosting next week at http://www.namek.co.uk , i have applied for a competition for an intro to his site. The best intro wins free hosting there with no demands on already having a good website :D

I think my flash intro is pretty good u can wiew it here
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

Code: Select all

<?php

$host = "localhost";
$user = "ultimadark";
$pass = "********";
$db = "ultimadark";

$con = mysql_connect($host, $user, $pass)or die("Connect Error: ".mysql_error());
mysql_select_db($db, $con);

$q = "SELECT * FROM 'users' WHERE username = $_POST['username'] AND email = $_POST['email']";

$result = mysql_query($q, $con);

$rows = mysql_num_rows($result);

if ($rows=="0") {
header("Location: signup.php.html"); }

else {
header("Location: failedsign.html"); }

mysql_close($con);

?>
You forgot the ; after the header()'s. I would not put <html> tags IMHO, since this is a kind of process page, you're not displaying anything. And from what I know, put a space after location in headers. header("Location: signup.php") not header("Location:signup.php"). Yeah, one more thing. Why was it signup.php.html anyway? It's either .html, which I doubt, .php or .phtml :).

-Nay
Post Reply