Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 17
Hello to all, I am just trying to established and TEST a simple to my MySQL test database using PHP via my firefox browser. I am able to connect using ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Mar 2014
    Posts
    29

    unable to connect o MySQL using PHP via web browser???


    Hello to all,

    I am just trying to established and TEST a simple to my MySQL test database using PHP via my firefox browser.


    I am able to connect using SSH and via terminal window on Fedora 20.

    I have created small php connection file so that I can test the connection.

    Code:
    <?php
    // Create connection
    $con=mysqli_connect("example.com","peter","abc123","my_db");
    
    // Check connection
    if (mysqli_connect_errno())
      {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }
    ?>

    when I type in my 127.0.0.1/test.php file in my FIREFOX browser.

    I get a error message " Failed to connect to MySQL: Permission denied "


    Just trying to understand where I went wrong?

    any help is greatly appreciated.

  2. #2
    Just Joined!
    Join Date
    Jan 2008
    Posts
    28
    It is not entirely clear...

    Are you running your web server on the same machine as your MySQL server? I'm going to guess 2 machines because your mysqli connect example does not use localhost. If you have a single machine, you should use localhost.

    If there are 2 machines, are they both running Fedora 20? I don't use Fedora much, and not for servers, but most of the concepts are the same.

    Is your web server running Apache or a different web server? I'm going to guess you are running Apache.

    If you are able to connect from the web server to the database server at a command line with the mysql client then you probably don't have a firewall issue on the database server. If you are running selinux, there is a flag that prevents outbound network connections from Apache.

    It's also possible your mysql user was created with localhost spec.

  3. #3
    Just Joined!
    Join Date
    Mar 2014
    Posts
    29
    Quote Originally Posted by Dustspeck View Post
    It is not entirely clear...

    Are you running your web server on the same machine as your MySQL server? I'm going to guess 2 machines because your mysqli connect example does not use localhost. If you have a single machine, you should use localhost.

    If there are 2 machines, are they both running Fedora 20? I don't use Fedora much, and not for servers, but most of the concepts are the same.

    Is your web server running Apache or a different web server? I'm going to guess you are running Apache.

    If you are able to connect from the web server to the database server at a command line with the mysql client then you probably don't have a firewall issue on the database server. If you are running selinux, there is a flag that prevents outbound network connections from Apache.

    It's also possible your mysql user was created with localhost spec.

    My end goal is to use PHP to query results from database / tables to show results on my WEB browser. So I run a small PHP script to at least connect to my MySQL database.



    Code:
    <?php
    $username = "your_name";
    $password = "your_password";
    $hostname = "localhost"; 
    
    //connection to the database
    $dbhandle = mysql_connect($hostname, $username, $password) 
     or die("Unable to connect to MySQL");
    echo "Connected to MySQL<br>";
    
    //select a database to work with
    $selected = mysql_select_db("examples",$dbhandle) 
      or die("Could not select examples");
    
    //execute the SQL query and return records
    $result = mysql_query("SELECT id, model,year FROM cars");
    
    //fetch tha data from the database 
    while ($row = mysql_fetch_array($result)) {
       echo "ID:".$row{'id'}." Name:".$row{'model'}."Year: ". //display the results
       $row{'year'}."<br>";
    }
    //close the connection
    mysql_close($dbhandle);
    ?>

    When I run this script and I have a database created in my MySQL. On my web browser all I get is a "connected to my MySQL message".

    Yes, I am running Fedora 20 , apache, php, mysql, all of it on the same machine. I am using localhost. sorry for the confusion. but still I am not able to connect to the tables using web browser.

    I am able to connect to MySQL via command line shell, but not via web browser.

    Not sure what I am doing wrong?
    Last edited by cyberdome; 03-15-2014 at 05:58 PM.

  4. #4
    Linux Engineer docbop's Avatar
    Join Date
    Nov 2009
    Location
    Woodshed, CA
    Posts
    902
    Logs aren't giving any clues?

    Have you tried this as a code snippet from the command line to check code outside apache to help narrow things down?

  5. #5
    Just Joined!
    Join Date
    Jan 2008
    Posts
    28
    Quote Originally Posted by cyberdome View Post
    ...
    Code:
    //execute the SQL query and return records
    $result = mysql_query("SELECT id, model,year FROM cars")
    ...
    You're not required to include the resource identifier but I do because sometimes it doesn't figure it out.

    Also, I always add a semi-colon at the end of my sql commands to make sure that isn't an issue.

    Maybe set a debug flag and display messages when each command succeeds.

  6. #6
    Just Joined!
    Join Date
    Mar 2014
    Posts
    29
    Quote Originally Posted by docbop View Post
    Logs aren't giving any clues?

    Have you tried this as a code snippet from the command line to check code outside apache to help narrow things down?

    Logs aren't giving any clues?

    I am completely new to Fedora 20, apache, php, and mysql. I thought I try to do a simple php mysql connection via web browser. I don't know from which logs to look at?


    Have you tried this as a code snippet from the command line to check code outside apache to help narrow things down?

    code snippet? sorry, I have no idea what is that suppose to mean?

  7. #7
    Just Joined!
    Join Date
    Mar 2014
    Posts
    29
    Quote Originally Posted by Dustspeck View Post
    You're not required to include the resource identifier but I do because sometimes it doesn't figure it out.

    Also, I always add a semi-colon at the end of my sql commands to make sure that isn't an issue.

    Maybe set a debug flag and display messages when each command succeeds.

    I am completely new to this php and mysql. I am using online tutorial to test out my php mysql connection. but it does not display anything on the browser page. I followed both tutorials and do not get any results on my browser.


    Just trying to see how php displays the info on the web page.

    not sure what I am doing wrong. I have set the SELinux policy to allow connections. I have opened mysql port on iptables.

    scratching my head right now

  8. #8
    Linux Engineer docbop's Avatar
    Join Date
    Nov 2009
    Location
    Woodshed, CA
    Posts
    902
    Here's how to turn on php logging.

    PHP Log All Errors to a Log File to Get Detailed Information - nixCraft

    Mysql logs are located in /var/log/mysql


    PHP can be run from the command line so writing a "snippet" is just writing enough code to test something out.

  9. #9
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,397
    In some of your examples you are using mysqli. In others mysql_* functions. They are NOT compatible. Use mysqli functions and/or (preferably) classes. The mysql_* functions have been deprecated in current versions of PHP. In any case, you cannot mix&match them. I use PHP with mysqli daily now in my development work (complex analytical web pages with lots of database I/O and graphics). This has not been a problem for me in accessing data from either a local mysql database, or a remote one (even when in the Amazon cloud). Make sure your database host, port (defaults to 3306), user, password, and database schema arguments are correct. These are the most common problems.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  10. #10
    Just Joined!
    Join Date
    Mar 2014
    Posts
    29
    Quote Originally Posted by Rubberman View Post
    In some of your examples you are using mysqli. In others mysql_* functions. They are NOT compatible. Use mysqli functions and/or (preferably) classes. The mysql_* functions have been deprecated in current versions of PHP. In any case, you cannot mix&match them. I use PHP with mysqli daily now in my development work (complex analytical web pages with lots of database I/O and graphics). This has not been a problem for me in accessing data from either a local mysql database, or a remote one (even when in the Amazon cloud). Make sure your database host, port (defaults to 3306), user, password, and database schema arguments are correct. These are the most common problems.
    I did know not about the currents version of the PHP. I am sorry. I am completely new to this. I am just trying to follow some online tutorials and just want to test a PHP MySQL connection via web browser. I can't even get that to work.


    I am following a online tutorial, in the tutorial, php will get the data from the mysql database and display it on the web browser.

    for example, name , last name , etc.

    here is the tutorial I followed:

    webcheatsheet.com/php/connect_mysql_database.php

    w3schools.com/php/php_mysql_select.asp


    I am sorry for the confusion. when I run another php file via browser, I just get the message " connected to MySQL"


    When I type my first file into the browser --> localhost/file.php

    I get the blank white page.


    when I type my second file int the browser --> localhost/test.php

    I get message "connected to MySQL"


    Code:
    file.php contents 
    
    <?php
    
    $con=mysqli_connect("localhost","root","password","examples",);
    
    // Check connection
    if (mysqli_connect_errno())
      {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }
    
    
    ?>
     on the browser --> localhost/file.php --> getting a blank white page.

    Code:
    test.php contents
    
    <?php
    
    
    $hostname = "localhost";
    $username = "root";
    $password = "password";
    
    
    // Create connection to the database
    $dbhandle = mysql_connect($hostname, $username, $password)
        or die ("unable to connect to MySQL");
    echo "connected to MySQL<br>";
    
    // select database to work with
    $selected = mysql_select_db("examples",$dbhandle)
    or die("could not select examples");
    
    //execute the SQL query and return records
    $result = mysql_query("SELECT id, model, year FROM cars");
    
    //fetch the data from database
    while ($row = mysql_fetch_array($result)) { 
    echo "ID:".$row{'id'}." Name:".$row{'model'}."Year: ". 
    $row{'year'}."<br>";}
    
    //close the connection
    mysql_close($dbhandle);
    ?>
    on the browser --> localhost/test.php  -->  getting message " connected to MySQL"

    What did I do wrong, Is there something I am suppose to turn on in the server to have php execute on the browser. when I right click on browser and view page source. on one of them I see nothing. on the other I see connected to mysql.


    scratching my head

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •