Help with php and mysql

garrette

New Member
Messages
27
Reaction score
0
Points
0
Hello, I am currently having problems with databases in mysql. As of now, I have a basic script in which the user will input data into 7 text field, and hit the submit button.

My real problem is that for some reason I am unable to access my database through php.

I have it set up like so:
PHP:
$username="*******";
$password="********";
$database="*********";

$first=$_POST['first'];
$last=$_POST['last'];
$phone=$_POST['phone'];
$mobile=$_POST['mobile'];
$fax=$_POST['fax'];
$email=$_POST['email'];
$web=$_POST['web'];

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

I have the <? and the ?>, so this isn't the problem, this is a small chunk.

When I run the script on my page though, it returns "Unable to select database". Why? Am I doing something wrong? What can I do to fix this problem? I am very new to scripting, so the problem is probably something basic. Anyone know what is wrong?
 

gomarc

Member
Messages
516
Reaction score
18
Points
18
Hi garrette,

Try using 'localhost' (notice the single quotes surrounding 'localhost'?)
 

garrette

New Member
Messages
27
Reaction score
0
Points
0
Hmm, thanks, I'll try that.

Thankyou, I'm fairly new so I was unaware that it also needed " ' ".
 
Last edited:

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
PHP converts undefined constants to strings, so localhost will become "localhost". However, never rely on this as it can lead to subtle bugs and will likely be dropped in the future. If you're not familiar with this feature, you should read through the PHP language reference or find a good book on PHP; there's quite a bit more you're missing.

The production code may be vulnerable to SQL injection via the various variables set from the $_POST values. Moreover, copying from the array to separate variables is unnecessary; just use the $_POST array.

The mysql extension is outdated and on its way to deprecation. Instead, use PDO, which has many useful improvements, such as prepared statements (prepared statement parameters aren't vulnerable to injection) and support for the Traversable interface, so you can loop over results with foreach. If you need a PDO tutorial, try "Writing MySQL Scripts with PHP and PDO".

See the X10 Wiki's various MySQL articles for more information on setting up and accessing MySQL databases on X10 servers.
 
Last edited:

garrette

New Member
Messages
27
Reaction score
0
Points
0
Thank you, very informative. I am going through that tutorial right now. Seems the resource I was using to learn PHP left out a few things, and is outdated haha.
 

garrette

New Member
Messages
27
Reaction score
0
Points
0
Haha I was using it for a bit, but in someones signature they claimed it was bad, so I went elsewhere. I'm used a page called "freewebmasterhelp" just to get me started, I already know some c++, and I'm finding it very similar to php in many ways.
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
A few similarities, I suppose. More similar than either to a Lisp or even Smalltalk, but between C++'s static typing, multiple inheritance, overloading for functions & operators, templates, separation of interfaces (in headers) and implementations, STL and PHP's type hints, single inheritance, "overloading", separation of interfaces & abstract classes, dynamic properties, traits (upcoming in PHP 5.4.0), built-in garbage collection and SPL. Not to mention C++ is much more well-defined in that it has standards.
 
Top