Jump to content

Cannot connect to MySQL


Recommended Posts

hi,

 

i have been trying to make the following php script work on fedora 5 os. but it does not show any data from the database. can any one please help. the code has been taken without any change from an online tutorial here.

 

http://www.freewebmasterhelp.com/tutorials/phpmysql/4

 

so i think it should be ok and the database has also been designed just as mentioned in the tutorial.

 

 

<?
$username="username";
$password="password";
$database="your_database";

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM contacts";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

echo "<b><center>Database Output</center></b><br><br>";

$i=0;
while ($i < $num) {

$first=mysql_result($result,$i,"first");
$last=mysql_result($result,$i,"last");
$phone=mysql_result($result,$i,"phone");
$mobile=mysql_result($result,$i,"mobile");
$fax=mysql_result($result,$i,"fax");
$email=mysql_result($result,$i,"email");
$web=mysql_result($result,$i,"web");

echo "<b>$first $last</b><br>Phone: $phone<br>Mobile: $mobile<br>Fax: $fax<br>E-mail: $email<br>Web: $web<br><hr><br>";

$i++;
}

?>

 

i have checked mysql database and the data is there but i m getting confused here.help please. thanks.

 

 

[moved from Terminal Shell commands, etc by spinynorman]

Link to comment
Share on other sites

$username="username";

$password="password";

$database="your_database";

 

Did you replace username with the mysql userid? And password? And your_database? These need to be set to access the database.

Link to comment
Share on other sites

yes,

 

i did so. but still nothing is displayed in the browser. i have been able to run a simple php page with no problems and this is the first time that i have tried to connect to a mysql database and problems are occuring. i created a database from mysql prompt first and then i set the user privilege using the following command

 

grant all privileges on *.* to root@"localhost" identified by 'thepassword';

 

and i m logging as root and trying to run the page but nothing is displayed. so i need help.

Link to comment
Share on other sites

tech: is there any data in the database? if all you did was create the database structure and give privilidges, then there is no data in the database and so you don't receive any results.

 

if there was an error, chances are you would get an error message back. if there was no data, $num would be 0, and your while loop would die before it got started. try changing the loop do a do-while and see what happens:

do {

$first=mysql_result($result,$i,"first");
$last=mysql_result($result,$i,"last");
$phone=mysql_result($result,$i,"phone");
$mobile=mysql_result($result,$i,"mobile");
$fax=mysql_result($result,$i,"fax");
$email=mysql_result($result,$i,"email");
$web=mysql_result($result,$i,"web");

echo "<b>$first $last</b><br>Phone: $phone<br>Mobile: $mobile<br>Fax: $fax<br>E-mail: $email<br>Web: $web<br><hr><br>";

$i++;
} while ($i < $num)

Link to comment
Share on other sites

remove the @ from the begining of

mysql_select_db($database) or die( "Unable to select database");

 

the @ surpresses errors

 

you could also change the connection line to :

mysql_connect(localhost,$username,$password) or die ("DAG NAM IT Jethro .. theres no database server");

Link to comment
Share on other sites

If you have selinux enabled, you can try the following:

 

# setsebool -P mysqld_disable_trans=1

This disables SELinux from protecting mysqld.

 

 

If that doesn't work, you can temporarily turn selinux off just for a test.

# setenforce 0

 

0 = permissive

1 = enforcing

 

check the status with

# sestatus 
SELinux status:				 enabled
SELinuxfs mount:				/selinux
Current mode:				   enforcing
Mode from config file:		  enforcing
Policy version:				 20
Policy from config file:		targeted

 

This might also have some relevant info on MySQl:

http://stanton-finley.net/fedora_core_5_in...otes.html#MySQL

 

Some SELinux info

http://fedora.redhat.com/docs/selinux-faq-fc5/en_US/

 

If turning off SELinux does the trick for you, please file a bug against the policy package.

Edited by jlc
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...