My first php code

Taz

Seasoned Veteran
Joined
Mar 1, 2009
Messages
3,652
Reaction score
2
FP$
1,032
my first php code is the connect

config.php

Code:
<?php
$con = mysql_connect("localhost" , "name" ,"password">;
if  ( $con )
   {
   die ( ' Could not connect:  '  . myql_error ( )) ;
   {

// some code


mysql_close ( $con) ;
?>

If you see some errors please tell me.
 
Not much you or I can do about it. Just have to wait for a moderator.
 
Code:
<?php
$con = mysql_connect("localhost" , "name" ,"password"[b]>[/b];
if  ( $con )
   {
   die ( ' Could not connect:  '  . [b]myql_error ( ))[/b] ;
   {

// some code


mysql_close ( $con) ;
?>

I have bolded the errors, and added in some extra stuff, this is what it should be like::
Code:
<?php
$con = mysql_connect("localhost" , "name" ,"password") or die(mysql_error());
mysql_select_db("database name", $con)
?>

You have made it so complicated, this is the only the necessary information.
 
Taz said:
my first php code is the connect

config.php

Code:
<?php
$con = mysql_connect("localhost" , "name" ,"password">;
if  ( $con )
   {
   die ( ' Could not connect:  '  . myql_error ( )) ;
   {

// some code


mysql_close ( $con) ;
?>

If you see some errors please tell me.

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

Just saying I looked at your PHP code. 😛 Look just alike to the one at that url! I put above they are the same. Just different text in the database user, pass, host.

But anyway! This site will help you with mysql: http://www.w3schools.com/php/
 
Yes, put the different between echoing it and using the die function is that when the die function is called, all code after it will not be executed.
 
Well, nice first try. But if you'll notice, you said
Code:
if($con)
{
die( "Could not connect" . mysql_error());
}
However, that will make it say it cannot connect if it does connect. It should read:
Code:
if(!$con)
{
die( "Could not connect" . mysql_error());
}

Also, like thepimpedmistletoe said, you did make it sort of more complicated than it needed to be. The "or" works like an "else" so mysql_connect(...) or die(...); will do the same as what you want. 🙂
 
yeah the tutorials showed me that happens but I thought it is a typo.
 
Just two things; I would look into MySQLi, as it has become the standard with PHP and offers more flexibility. Second, you should probably try to start off with Object-Oriented PHP rather than Procedural, that way it isn't so hard to learn later. 🙂
 
way I still don't know php so can you guys be=ring it down to a beginner level.
 
Taz said:
way I still don't know php so can you guys be=ring it down to a beginner level.
If that's true, I would take a couple of days to go through the PHP tutorial on W3Schools and the SQL Tutorial. Make sure you try every example, and double check from the tutorials. If you think it matches and you still do not know what's wrong, ask on here or a PHP support forum. 😉
 
yeah I"m looking for some one to teach me php one-on-one. I tried W3 thoguh. But too slow if I have questions.
 
Back
Top Bottom