Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to set mysql to work with php?

Status
Not open for further replies.

newtomysql

Technical User
Apr 11, 2001
96
MY
Dear All,
I have set my php 5.2.4 version to work well with apache server. The problem is I cant set mysql to work with php? I have infact set the extension too but mysql is still not working. Can any one help me on this? Thanks.
 
What does your php.ini file look like? Do you have the extension enabled in there?

What is the error you're getting to show that mysql support isn't enabled?

Is mysql not working as you don't have the server running, or is the mysql extension not working in PHP?
 
Dear Vcherubini,
I dont get you what do you mean by php.ini looks like? I have checked my extension php.ini and this exist.
[PHP_MYSQL]
extension=php_mysqli.dll. For example once I run this sample code below is shows the page is not found without any error. But when I comment the connection coding and echo it comes out. So what else is my error. Thanks.

<?
/*--------- DATABASE CONNECTION INFO---------*/
$hostname="localhost";
$mysql_login="root"
$mysql_password="12345";
$database="test1";

// connect to the database server
if (!($db = mysql_pconnect($hostname, $mysql_login , $mysql_password))){
die("Can't connect to database server.");
}
else
{
// select a database
if (!(mysql_select_db("$database",$db)))
{
die("Can't connect to database.");
}
else
{
echo "OK";
}
}
?>


 
Ok, there are several things to check.

1. Is apache running with mod_php enabled? If not, going to will say page not found, or return a 500 status error.
2. Is mysqld running? You're on a Windows machine, so check your services and see if mysqld is running.
3. A better way to do the code above is to use the mysqli extension:

[tt]
$db = new mysqli('localhost', 'user', 'password', 'database');
$db->connect() or die('Failed to connect');

$db->query('SELECT * FROM whatever...');

$db->close();
[/tt]

It's best not to use pconnect() as well.

Artisan System Framework
 
Dear Vcherubini,

1. I have tried the link you gave it doesnt work and it says The page cannot be found. So how to enabled it ?

2. Mysql is running as I can login and checked under the services too. Currenly mysql is version 5.0.67.

3. This code also doesnt work. So what else do you want me to check or could be I am missing or is my error? Thanks.
 
please run a script with just the following in it

Code:
phpinfo();

note that including something in php.ini does not mean that it exists as an extension. the above will tell us what is loaded and what is not.

i note aswell that you are using the so called mysql improved extension. is that deliberate? i find the old extension much more stable.
 
Yes, using mysqli is deliberate. Using mysqli is recommended by the PHP manual and is much, much more robust. Its also object oriented, which is good for new programmers to see.

newtomysql, you can't click on the localhost link because localhost points back to your computer, it points to the IP address 127.0.0.1, which is your own computer. Use that for testing your programs.

Do what jpadie suggested and see what phpinfo(); returns. It will tell you in there if the mysql(i) extensions are installed.

Artisan System Framework
 
Dear Jpadie,
Sorry I was totally lost I dont know which one is new and which is old. So which one is the old method to be used ? I have run the phpinfo() and for the mysqli part is what I can see? I have enabled the mysql extension too but it did not appear as part of teh phpinfo()?

Client API library version 5.0.45
Client API header version 5.0.45
MYSQLI_SOCKET /tmp/mysql.sock

mysqli.default_host no value no value
mysqli.default_port 3306 3306
mysqli.default_pw no value no value
mysqli.default_socket no value no value
mysqli.default_user no value no value
mysqli.max_links Unlimited Unlimited
mysqli.reconnect Off Off
 
Dear vcherubini,
I have tried your code like below with slight editing. Unfortunately it just print out my code but not running at all. Is it that I must use double or single quote? Thanks.
<?
$db = new mysqli('localhost', 'root', '12345', 'test1');
$db->connect() or die('Failed to connect');

$db->query('SELECT * FROM tblproduct');

$db->close();
?>
 
Nope, if it's just printing out your code, you've got a while to go.

You need to tell apache how to interpret .php files. How did you go about installing each of these? Might I suggest using XAMPP ( to install PHP/Mysql/Apache on your machine so its configured properly.

Artisan System Framework
 
Dear vcherubini,
Actually I followed a website step by step on how to install manually. I installed first apache then php then mysql. The article is at this link I have tried xampp but the problem is that my apache was not running but when I did manualy it works. So only left now is the communication with mysql. Thanks.
 
Using mysqli is recommended by the PHP manual and is much, much more robust

I disagree. in practice i have found the mysql extension to be more robust and quicker.

Object orientation can be achieved through PDO without using the native mysql* library.

@OP - please post your phpinfo output here or post a link to where we can see it on the internet. it is not just the mysql* information that is relevant.
 
Dear Jpadie,
Thanks for all your help I have at last managed to get some results based on my code below. Can you please verify if this code is ok or not? My another problem is that now when there is syntax error is not shown on my page and it just say page not found. How to solve this problem? Thanks.

<?php
include("connectionInfo.php");

if ($result = mysqli_query($link, "SELECT * FROM tblProduct")) {

/* determine number of rows result set */
$row_cnt = mysqli_num_rows($result);

printf("Result set has %d rows.\n", $row_cnt);

/* close result set */
mysqli_free_result($result);
}

/* close connection */
mysqli_close($link);


?>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top