...

View Full Version : starting PHP - basic help required please



jarv
05-13-2008, 09:02 AM
Here is my php page insert.php


<?php

$con = mysql_connect("localhost","john", "*****");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("my_db", $con);

$sql="INSERT INTO person (FirstName, LastName, Age)
VALUES
('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";

if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";

mysql_close($con)
?>

can someone please help me, my error says: Error: No database selected

do i need to add databasename into this line:?
$con = mysql_connect("localhost","john", "*****");

abduraooft
05-13-2008, 09:09 AM
Check the output after changing your code like

mysql_select_db("my_db", $con) or die(mysql_error());

jarv
05-13-2008, 10:37 AM
the code is all ok, I used W3C

http://www.w3schools.com/PHP/php_mysql_connect.asp

jarv
05-13-2008, 11:36 AM
please help!

funnymoney
05-13-2008, 12:02 PM
Try using this code, it's same except it works for me :)


$username = "root";
$password = "";
$host = "localhost";
$database = "igra";

$link = mysql_connect($host, $username, $password)
or die('Cant connect, wrong username or password: ' . mysql_error());

mysql_select_db($database) or die('Error connecting to database'. mysql_error());
?>

peteyb383
05-13-2008, 12:13 PM
Have you created a database on your server?

funnymoney
05-13-2008, 01:17 PM
if you didn't create table then after you connect to database use this code


<?php

$sql = ("CREATE yourdatabase") ;

$link = mysql_query($sql)or die (mysql_error());

mysql_use_db("yourdatabase");

echo "Create table<br />
";
mysql_query('CREATE TABLE korisnici
(id INT,
name CHAR(20),
password CHAR(40))')
or die
("Error creating table
<blockquote style="background: #cccccc; padding: 5px">".
mysql_error()
."</blockquote>");
?>

there might be some parsing errors in that code :dunno:

firepages
05-13-2008, 01:53 PM
please help!

abduraooft tried but you ignored ;)

w3c code is valid but does not offer the same debugging as noted by abduraooft



<?php
mysql_connect("localhost","john", "*****") or die(mysql_error());
mysql_select_db("my_db")or die(mysql_error());
$q=mysql_query("SELECT * FROM $table")or die(mysql_error());
print_r(mysql_fetch_assoc($q));

?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum