Please Help Me My SQL

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

dudeking

Thread Starter
Joined
Feb 7, 2007
Messages
483
Right I need to use mysql.

If some one would please post a PHP script that connected to a sql database, stored a value in the database then echo the value back out of the database.

If I saw a script that did that I would be able to start using my sql with my site. I just have no clue where to start at the moment.


Thank you soooo much any one who does this for me :D :D (y) (y)
 

MMJ

Guest
Joined
Oct 15, 2006
Messages
3,625
Put together from http://www.w3schools.com/php/php_mysql_intro.asp
You can learn almost all there is to know there. :)
PHP:
<?php
$con = mysql_connect("localhost","dbuser","dbpass");
if (!$con)
	die('Could not connect: ' . mysql_error());

mysql_query("CREATE DATABASE my_db",$con);
mysql_select_db("my_db", $con);
$sql = "CREATE TABLE person 
(
FirstName varchar(15),
LastName varchar(15),
Age int
)";
mysql_query($sql,$con);
mysql_query("INSERT INTO person (FirstName, LastName, Age) VALUES ('Peter', 'Griffin', '35')");
mysql_query("INSERT INTO person (FirstName, LastName, Age) VALUES ('Glenn', 'Quagmire', '33')");
$result = mysql_query("SELECT * FROM person");
echo
"<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";

while($row = mysql_fetch_array($result))
{
	echo "<tr>";
	echo "<td>" . $row['FirstName'] . "</td>";
	echo "<td>" . $row['LastName'] . "</td>";
	echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
In this code I create a db, a table, and two rows.

You should only have to do that once.
 

dudeking

Thread Starter
Joined
Feb 7, 2007
Messages
483
so do i have to have some code to check if the table and rows are already there?
 

MMJ

Guest
Joined
Oct 15, 2006
Messages
3,625
Add IF NOT EXISTS

Example:

PHP:
$sql = "CREATE TABLE IF NOT EXISTS person 
(
FirstName varchar(15),
LastName varchar(15),
Age int
)";
 

dudeking

Thread Starter
Joined
Feb 7, 2007
Messages
483
Okay, so right, I want to create user account for my site.

Do I have a separate row for each member then. So a registration form people enter info and its exported to a row on the members table. Some one comes to the site, it sees there is no cookie for the site, so it says login instead of saying welcome 'Your Name'. Then people click login enter there username and password, now this is the bit im stuck on. Do i need to seach the database for that username then see if the password matches up? Or is there some other more secure way to do this?
 

MMJ

Guest
Joined
Oct 15, 2006
Messages
3,625
You will do something like mysql_query("SELECT * FROM users WHERE username = $_POST['username'] AND password = $_POST['password']")

Don't use that code though, it might be incorrect and will definitely be a security risk.

There are hundreds of tutorials on the net about this issue. Google is your friend! :)
 
Joined
May 21, 2007
Messages
43
Yes, you will need a row for each member. In the database you will have a username and password field. The password field should be varchar, 32 characters long. To be more secure, you will want to encrypt what the user enters into the password text box using md5 encryption. This encrypted password will be stored as a 32 character long hash. When you check to see if the username and password match up, you compare the md5 hash from the password field with the md5 hash in the database, that way no plain text password will be stored. It might also be wise to use session variables that are persistent across pages, start your pages with <?php $session_start; ?>. You can store variables in the session with the following:
<?php $username = $_SESSION['username']; ?> With this method you can access the username from different pages.
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Staff online

Top