www.webdeveloper.com
Results 1 to 6 of 6

Thread: How to retreive sql database using PHP?

  1. #1
    Join Date
    Aug 2013
    Posts
    3

    How to retreive sql database using PHP?

    Hi,

    I would please like to know how I can retreive sql database table using php? I tried an exemple from a book and an exemple code from a website (http://webcheatsheet.com/php/connect...se.php?print=Y) . In both cases, I sucessfully connected to mysql server but haven't succeded connecting to the database to retreive information from a table. I spend 3 days on it but I didn't find the solution. My guess is that it has something to do with the user, host and password I enter. I think that I don't use a username and a password so I enter "localhost" for the host, "root" for the user and "" for the password. I tried the command "SELECT * FROM mysql.user;" to get that information and I saw many users, hosts and passwords and I don't know which on is the right one nor which one I am currently using. A few of those passwords were probably created by me a month ago and now, i don't even remember how.


    Additional information:
    Before connecting to mysql I went to the bash application and entered the following parameters:
    myuser='user'
    mypass='passe'
    mydb='namr'
    myq="Quers"
    alias mysql=/usr/local/mysql/bin/mysql
    mysql --user=root mysql
    So from the following, I would like to know if I should enter
    $username = "user";
    $password = "passe";
    $hostname = "localhost";
    in my case ? I also tried it but it still didn't solve the situation.

    In resumé, from the following code from (http://webcheatsheet.com/php/connect...se.php?print=Y), I need to know why I can't retrieve information(I always get Could not select examples message) and If I correctelly entered the user, password and host?

    PHP Code:
    <?php
    $username 
    "root";
    $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);
    ?>

    To create 'examples' database on your MySQL server I run the following script:

    Code:
    CREATE DATABASE `examples`;
    USE `examples`;
    CREATE TABLE `cars` (
       `id` int UNIQUE NOT NULL,
       `name` varchar(40),
       `year` varchar(50),
       PRIMARY KEY(id)
    );
    INSERT INTO cars VALUES(1,'Mercedes','2000');
    INSERT INTO cars VALUES(2,'BMW','2004');
    INSERT INTO cars VALUES(3,'Audi','2001');
    Thank you very much !!

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,933
    You may want to check some return values, then see if PHP and/or MySQL can tell you anything interesting, e.g.:
    PHP Code:
    //execute the SQL query and return records
    $result mysql_query("SELECT id, model,year FROM cars");
    if(
    $result == false) {
        throw new 
    Exception("Query failed: ".PHP_EOL.mysql_error());

    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Aug 2013
    Posts
    3
    Thx, I retried it with your advice but I still have the same problem . The problem is before that istruction, it's when I try to conect to the database:
    $selected = mysql_select_db("examples",$dbhandle)
    or die("Could not select examples");
    It doesn't connect and never recognize database examples. I always get Could not select examples. Why?

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,933
    Try using mysql_error() with that error message, too, to see what the DB is complaining about.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Aug 2013
    Posts
    3
    Quote Originally Posted by NogDog View Post
    Try using mysql_error() with that error message, too, to see what the DB is complaining about.
    It doesn't recognise database exemples when I do mysql error(). but in mysql when I type show tables, exemples is there. Why?

  6. #6
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,933
    "exemples" or "examples"?

    Does your script log in with a different database user than whatever you use to manually look at the database? If so, they might have different permissions with regard to what they have access to.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles