2uzhan.com
Advertisement
Now Place:2uzhan.com » Unable to connect to mysql or mysqli from PHP via browser

Unable to connect to mysql or mysqli from PHP via browser

MySQL @ October 9, 2006   Views:0

Have a little problem here. I've read a lot of similar issues with mysql/php connection problems with Windows platform, but none dealing Linux. I'm running Fedora Core 5/linux with Apache, PHP, and MYSQL. Here's what I have so far:

What I can't do
1. When I try to connect to mysqli via the browser it just states "Connect failed:2002"
2. When I try to connect to mysql via the browser there is just a blank page.

What I can do
1. I can see the phpinfo page great. All configurations seem okay.
2. I can connect to mysql via PHP script using a command line.
3. I can connect to mysqli via PHP script using a command line.
4. I can fetch, query, and display datbase results via a PHP script in a terminal window.

But can't figure out why I can't connect via the same PHP script in the browser.

Can someone clue me in.

Thanks

--------------Solutions-------------

Can you post the relevant snippet of that script? Didnt' know which script you wanted so here a are both.

mysqli:
<?php
$mysqli = new mysqli("localhost", "root", "", "repeatdb");

if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_errno());
exit();
} else {
printf("Host information: %s\n", mysqli_get_host_info($mysqli));
mysqli_close($mysqli);
}
?>

mysql:
<?php
$username_oConn="root";
$password_oConn="";
$hostname_oConn="localhost";
$database_oConn="repeatdb";
$oConn = mysql_pconnect($hostname_oConn, $username_oConn, $password_oConn) or trigger_error(mysql_error(),E_USER_ERROR);
$dbh = mysql_select_db($database_oConn) or die("Cannot find database");
print "Connected to MYSQL<br>";
?>

Thanks Here is the script I am using to fetch the records and works from the command line.

<?php
$mysqli = new mysqli("localhost", "root", "", "repeatdb");

if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_errno());
exit();
} else {
$sql = "SELECT * FROM platformTrks";
$res = mysqli_query($mysqli, $sql);

if ($res) {
while ($newArray = mysqli_fetch_array($res, MYSQLI_ASSOC)) {
$trkId = $newArray['trkId'];
$name = $newArray['name'];
echo "The trkId is ".$trkId." and the name is ".$name."<br/>";
}
} else {
printf("Could not retrieve records: %s\n", mysqli_get_host_info($mysqli));
}

mysqli_free_result($res);
mysqli_close($mysqli);
}
?>

Thanks PROBLEM SOLVED.
Something really easy. I had been starting mysqld in safe_mode. Decided to stop and start with the "service mysqld stop" & "start" comands. Also, did httpd for the heck of it.

Tried out the browser and PHP connected straight through and all data was retrieved and displayed correctly on the page. For both mysql and mysqli connections.

Thanks

Tags:
© 2018 2uzhan.com Contact