/    Sign up×
Community /Pin to ProfileBookmark

can’t get my php to connect to mysql

I’m using PHP 5 for mysql on a Windows XP Pro box to learn some basic connectivity for an upcoming project….i’m new to this game so i’m still trying to learn the basics

I got an error doing some basic connectivity using mysql_connect() :

<?
$username=”root”;
$password=”xxxx”;
$database=”Cars”;

$manu=$_POST[‘manu’];
$model=$_
POST[‘model’];
$year=$_POST[‘year’];

$connection = mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( “Unable to select database”);

$query = “INSERT INTO cartype VALUES (”,’$manu’,’$model’,’$year’)”;
mysql_query($query);

mysql_close();
?>

my index.html file calls insert.php (which is what i have above) and when it runs it comes back with an error;

Fatal error: Class ‘mysql’ not found in C:Program FilesApache GroupApache2htdocsinsert.php on line 10

there is one thing i read on search that i haven’t tried yet, simply because i don’t know how….a possibility that mysql extension not installed for php, but that was applying to somebody with php 4.x.

What other possibilities might there be?

my php.ini path on httpd.conf is correct….although i am using the “ini recommended” file whatever that means.

does it matter that my .php file is seperate and not imbedded in my index.html file? should I embed it and make it index.php?

to post a comment
PHP

15 Comments(s)

Copy linkTweet thisAlerts:
@SheldonOct 21.2005 — well index.html wont pull the insert.php because html just cant do that, rename the page to index.php and it will work,

If thats not the case try printing the results aswell.

[code=php]
<?
$username="root";
$password="xxxx";
$database="Cars";

$manu=$_POST['manu'];
$model=$_POST['model'];
$year=$_POST['year'];

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

$query = "INSERT INTO cartype VALUES ('','$manu','$model','$year')";
mysql_query($query);

mysql_close();

print 'Welcome';
print $username;
print ', How is the new';
print $model;
?>
[/code]
Copy linkTweet thisAlerts:
@chazzyOct 21.2005 — 
my index.html file calls insert.php
[/quote]

I think you mean index.html posts to insert.php.

also, i think you might want to check, i'm not sure that but i think you need to start with <?php...not <? but i might be wrong.

are you sure that php is running while you are running this page? that's an important step.
Copy linkTweet thisAlerts:
@SheldonOct 21.2005 — <? is just short terms for <?php i prefer <?php but either will work.

If Chazzy is right about index.html posting to insert.php, post the code for your form as maybe thats the problem
Copy linkTweet thisAlerts:
@chazzyOct 21.2005 — <? is just short terms for <?php i prefer <?php but either will work.

If Chazzy is right about index.html posting to insert.php, post the code for your form as maybe thats the problem[/QUOTE]


if that's the case, then it's either mysql wasn't installed or php is not running. most likely php is not running or apache can't find it.

just to check, if you install php then apache, apache will pick up php's directory if it is running. if it's not running or not installed, you'll have to manually configure it.
Copy linkTweet thisAlerts:
@ridicimousmanauthorOct 21.2005 — ok i finally got it to work, i did some research and found that 387410398471203 people seem to have this problem, as php 5 has changed some very essential things that was once taken for granted with php 4.x, so here goes:

in order to correct my undefined mysql_connect() problem (and anything else that would have shown up for any other function) I had to....

1. add the c:php; to my system path, which should have been done already i know

  • 2. renamed php.ini-dist to php.ini


  • 3. went into php.ini and inserted the path of the apache document root path into the doc_root field

    in addition inserted the extensions dir path to the extension_dir field in php.ini, which was c:phpext


  • 4. scrolled down to almost the bottom of the php.ini file and deleted the 'comment' character to activate the php_mysql.dll extension.


  • after all that, it finally works great...ugh


    thanks for all you guys suggestions!! good lookin out ? ? ?
    Copy linkTweet thisAlerts:
    @chazzyOct 21.2005 — 
    4. scrolled down to almost the bottom of the php.ini file and deleted the 'comment' character to activate the php_mysql.dll extension.
    [/QUOTE]


    Based on the fact that apache picked up on the compilation for php, that means that it did find c:php (most likely picked up automatically). i think that if you did this step only and tried it, it should have worked as well.

    but either way, good job, congrats on it working.
    Copy linkTweet thisAlerts:
    @NogDogOct 21.2005 — The string literal "localhost" should have been quoted in this line:
    <i>
    </i>$connection = mysql_connect([color=red]"[/color]localhost[color=red]"[/color],$username,$password);
    Copy linkTweet thisAlerts:
    @conputerguy99Oct 21.2005 — Everybody seems to be overlooking a very important part of the query. The above is also correct, but there is still a problem

    [code=php]$query = "INSERT INTO cartype ('fieldname', 'fieldname', 'fieldname', 'fieldname') VALUES ('', '$manu', '$model', '$year')";[/code]

    Everybody was missing the fieldnames. Don't you need to have your field names defined before you give the values?
    Copy linkTweet thisAlerts:
    @NogDogOct 21.2005 — ...

    Everybody was missing the fieldnames. Don't you need to have your field names defined before you give the values?[/QUOTE]

    You can omit the field list as long as you have a value for every field in the correct sequence. As per http://dev.mysql.com/doc/refman/5.0/en/insert.html :
    If you do not specify the column list for INSERT ... VALUES or INSERT ... SELECT, values for every column in the table must be provided in the VALUES list or by the SELECT. If you do not know the order of the columns in the table, use DESCRIBE tbl_name to find out.[/quote]
    Copy linkTweet thisAlerts:
    @conputerguy99Oct 22.2005 — Didn't know that! Thanks!
    Copy linkTweet thisAlerts:
    @chazzyOct 22.2005 — The string literal "localhost" should have been quoted in this line:
    <i>
    </i>$connection = mysql_connect([color=red]"[/color]localhost[color=red]"[/color],$username,$password);
    [/QUOTE]


    completely missed that, good eye.

    however.

    this must be a php only thing, but it seems to work fine for me when i just use localhost, no quotes. i got the exact results i expected.


    unable to connect: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
    [/quote]


    which makes sense because i don't have a mysql server running on localhost. so it seems to respond correctly. but otherwise yes, it should have been quoted. an odd result actually.
    Copy linkTweet thisAlerts:
    @NogDogOct 22.2005 — Yeah, it will "work", but it's generally not advisable:
    If you use an undefined constant, PHP assumes that you mean the name of the constant itself, just as if you called it as a string (CONSTANT vs "CONSTANT"). An error of level E_NOTICE will be issued when this happens. See also the manual entry on why $foo[bar] is wrong (unless you first define() bar as a constant). If you simply want to check if a constant is set, use the defined() function.[/quote]
    (from http://www.php.net/manual/en/language.constants.php#language.constants.syntax )
    Copy linkTweet thisAlerts:
    @elAdiNov 17.2005 — ...i am not completely stupid.

    i had the same problem as ridicimousman, so i followed his solution. now, as soon as i take the comment marks away from the extension_dir, i get an error when restarting apache (or reboot the computer). it says it can't find the dll in the specified path...but the path is alright and the dll is there. see image.

    any clue on this?

    [upl-file uuid=47af45f9-45a2-44b3-b236-7c305868c237 size=61kB]errormsg.gif[/upl-file]
    Copy linkTweet thisAlerts:
    @ShrineDesignsNov 17.2005 — you must use forward slashes not back slashes, like; Directory in which the loadable extensions (modules) reside.
    extension_dir = "c:/php/ext/"
    Copy linkTweet thisAlerts:
    @elAdiNov 18.2005 — Nope, that's not it, even if I use forward slashes, it doesn't work. Same ol' error message when I restart the Apache server.

    Any other ideas or how I could work around this. I need the development server immediately, as the server (hosted in-house with a provider) is not set up yet, and I need to start programming.

    Adrian
    ×

    Success!

    Help @ridicimousman spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.6,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,

    tipper: @Samric24,
    tipped: article
    amount: 1000 SATS,
    )...