Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

using mysql database using php


ghost's Avatar
0 0

I need little help on configuring apache,mysql and php. First will tell whatever i had did till now. 1)i installed the apache 2, php5, and mysql 5. 2)then i edited the httpd.conf file for php and now i can run php files in apache.

But i m not able to connect to mysql from php. I searched about it but got confused so need some help on whether i need to do some extra setting or anything else.

please help:)


ghost's Avatar
0 0

maybe u can start posting what error u got when u try to connect to db…


ghost's Avatar
0 0

i got this error: mysql_connect() is not defined

i used the connection string properly that i m sure but may be there are some setting that i need to change.


ghost's Avatar
0 0

Are u using win?


ghost's Avatar
0 0

yes windows xp


clone4's Avatar
Perl-6 Wisdom Seeker
0 0

harry_potter28 wrote: i got this error: mysql_connect() is not defined

i used the connection string properly that i m sure but may be there are some setting that i need to change.

Post what code you use to connect to the database:

here is little sample that I use for localhost connection:

<?php

function sql(){
$database = 'nameofdatabase';
$host = '127.0.0.1';
$db_user = 'root';
$db_pass = 'secretpassword';


$con = mysql_connect($host,$db_user,$db_pass) or die(mysql_error());
mysql_select_db($database,$con);

return $con;
}

function endsql(){
mysql_close();
}
?>

which you then include before the sql connection and use it as : $connect = sql(); etc

And no you shouldn't need any extra settings, except knowing your databse password and create database with new name, or use default database "mysql"


ghost's Avatar
0 0

Ok, try to do that:

You should have Apache installed in "c:\apache\apachegroup" or something similar; PHP installed in "c:\php" Mysql installed in "c:\mysql"

open the httpd.conf (c:\apache\apachegroup\conf\httpd.conf) and add: markup ScriptAlias /php/ "c:/php/" AddType application/x-httpd-php .php Action application/x-httpd-php "/php/php-cgi.exe" Go to php directory c:\php then rename php.ini-dist to php.ini (or make a new php.ini based on old php.ini file), and make sure u set the right extension: extension_dir = "c:\php\ext" extension=php_mysql.dll


ghost's Avatar
0 0

This is the code that i m using

<?php $con = mysql_connect("localhost","root","password"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("dbname", $con); mysql_query("insert into username ($name,$address,$age,$cno,$username,$password)"); mysql_close($con); ?>


ghost's Avatar
0 0

Cyph3rHell wrote: Ok, try to do that:

You should have Apache installed in "c:\apache\apachegroup" or something similar; PHP installed in "c:\php" Mysql installed in "c:\mysql"

open the httpd.conf (c:\apache\apachegroup\conf\httpd.conf) and add: markup ScriptAlias /php/ &quot;c:/php/&quot; AddType application/x-httpd-php .php Action application/x-httpd-php &quot;/php/php-cgi.exe&quot; Go to php directory c:\php then rename php.ini-dist to php.ini (or make a new php.ini based on old php.ini file), and make sure u set the right extension: extension_dir = "c:\php\ext" extension=php_mysql.dll

ok thanks done rest of the part except extension will do it and then see whether it works


ghost's Avatar
0 0

Hey guys what about using mysqli (MySQL Improved Extension)?

&lt;?php

$dbinfo = parse_ini_file(&#39;db.ini&#39;);

$conn = new mysqli($dbinfo[&#39;SERVER&#39;], $dbinfo[&#39;USER&#39;], $dbinfo[&#39;PASSWORD&#39;], $dbinfo[&#39;DB&#39;]);

//Check for errors
if ($mysqli_connect_error)
{
    die(&quot;Connection failed --&gt; &quot; . mysqli_connect_error());
}
echo &quot;Connected!&quot;;

$query = &quot;..&quot;;

if ($conn-&gt;query($query))
	echo &quot;Done :)&#92;n&quot;;
else
	echo &quot;Failed :(&#92;n&quot;;

$conn-&gt;close();

?&gt;

it seems to be nicer with OOP


clone4's Avatar
Perl-6 Wisdom Seeker
0 0

wow object for mysql, just like in perl i LOVE it, thx Cyph3rHell(does it come with default php install)? and sorry h_p the script seems alright, dunno what else could it be:)


ghost's Avatar
0 0

checked the extension part and its already there so problem not solved yet.


ghost's Avatar
0 0

clone4 wrote: wow object for mysql, just like in perl i LOVE it, thx Cyph3rHell(does it come with default php install)?

nope, u should install it… read this: http://it.php.net/mysqli


ghost's Avatar
0 0

harry_potter28 wrote: checked the extension part and its already there so problem not solved yet.

are you sure your daemon is started?


ghost's Avatar
0 0

yes i had checked these things 3 to 4 times. stopped all the servers then started again but nothing working.


ghost's Avatar
0 0

Since PHP5 you have to enable the mysql extention, are u sure u have it enabled in php.ini file?


ghost's Avatar
0 0

[PHP_MYSQL] extension=php_mysql.dll

this is the part of the php.ini file if that what u r asking.


ghost's Avatar
0 0

To enable mySQL for use in PHP5, you have to do the following:

  1. Make sure you have valid php.ini file in <Apache_dir>.
  2. Copy libmysql.dll to the system32 directory (in Windows installation directory). (php_mysql.dll won’t load without doing this!)
  3. Set the extension_dir in php.ini to the ext directory of your PHP installation directory - typically "extension_dir = <PHP_installation_dir>\ ext".
  4. Enable the mySQL extension in php.ini by un-commenting the line "extension = php_mysql.dll".
  5. Restart Apache Server.

ghost's Avatar
0 0

u said about having php.ini in apache group folder?

because its not there so if u say i will copy that file there. reply.


ghost's Avatar
0 0

guys finally done the connection thanks for all ur help.:)


ghost's Avatar
0 0

Hey, sorry for not replying to your post, i have got problems with my computer… However i'm glad that you finally solved your problem ;)


ghost's Avatar
0 0

hey no need to say sorry u helped a lot by replying previously thanks.


ghost's Avatar
0 0

thanks for the link spyware will try that one


ghost's Avatar
0 0

Is the domainname supposed to be for localhost development. In that case you can spoof a DNS name for local machines.

Are you running Windows or Linux?

There's a file in both of them called hosts, in Windows it's located in C:\Windows\system32\drivers\etc, and in linux it's located in /etc.

Just add the line: markup127.0.0.1 www.mydomain.com

and of course replace mydomain with whatever domain you want.