+ Reply to Thread
Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By gomarc

Thread: Help with php and mysql

  1. #1
    garrette is offline x10Hosting Member garrette is an unknown quantity at this point
    Join Date
    Jan 2012
    Posts
    27

    Help with php and mysql

    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 Code:
    $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?

  2. #2
    gomarc's Avatar
    gomarc is offline x10 Elder gomarc is an unknown quantity at this point
    Join Date
    Oct 2007
    Location
    USA
    Posts
    511

    Re: Help with php and mysql

    Hi garrette,

    Try using 'localhost' (notice the single quotes surrounding 'localhost'?)
    garrette likes this.

  3. #3
    garrette is offline x10Hosting Member garrette is an unknown quantity at this point
    Join Date
    Jan 2012
    Posts
    27

    Re: Help with php and mysql

    Hmm, thanks, I'll try that.

    Thankyou, I'm fairly new so I was unaware that it also needed " ' ".
    Last edited by garrette; 01-04-2012 at 05:52 PM.

  4. #4
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Help with php and mysql

    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 by misson; 01-05-2012 at 12:10 AM.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  5. #5
    garrette is offline x10Hosting Member garrette is an unknown quantity at this point
    Join Date
    Jan 2012
    Posts
    27

    Re: Help with php and mysql

    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.

  6. #6
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Help with php and mysql

    Was it W3Schools, by any chance?
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  7. #7
    garrette is offline x10Hosting Member garrette is an unknown quantity at this point
    Join Date
    Jan 2012
    Posts
    27

    Re: Help with php and mysql

    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.

  8. #8
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Help with php and mysql

    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.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

+ Reply to Thread

Similar Threads

  1. Replies: 0
    Last Post: 10-18-2011, 03:16 PM
  2. Replies: 2
    Last Post: 08-03-2011, 06:46 PM
  3. Replies: 2
    Last Post: 10-14-2010, 10:49 AM
  4. Replies: 2
    Last Post: 12-17-2007, 03:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers