SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Jun 2001
    Location
    uk
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    problems connecting to mysql

    Mysql is totally new to me. I installed it yesterday along with php and I thought it was all working. the problem is connecting to a database from php. I can actually connect to the database and insert new tables into an existing database, but i cant view any info from within the table.

    i think it is a problem with the username and password. if i set the username to root and no password i get no errors but cannot see anything although if i remove the username root i get the message

    Access denied for user: 'hostname' to database 'jokes'

    I am running nt workstation with apache 1.3 and php4

    any ideas anyone. this is driving me insane!

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    If you were having a problem with the username and password, then why can you insert data into the table? Is it your belief that internet security allows you to freely trash people's databases, but only requires you to provide a password and username if you want to read data from a database? Here is some sample code on how to connect to a database:

    $connection=mysql_connect("localhost", "", "") or die("Couldn't connect to server.");
    $db=mysql_select_db("db_name", $connection) or die("Couldn't select database.");

    Since you are new, you could be doing many things wrong, yet you didn't provide any code for a more experienced person to examine in order to spot your error. Since you failed to provide any relevant information, I can only guess as to what your problem is, but it may be that you tried to echo the "result identifier" from this statement:

    $sql="SELECT * FROM table_name";
    $sql_result=mysql_query($sql, $connection);
    echo $sql_result;


    $sql_result is what is called a "result indentifier" and cannot be echoed out. You have to do a little more work to get the data from that. The following statement:

    $row = mysql_fetch_array($sql_result);

    puts a record(or row) of data in an array and assigns it to $row. To echo the field you want, you can do it two ways:

    echo $row[0];

    will echo the first field.

    echo $row['field_name'];

    will echo the field with the name 'field_name'. The second method is preferred to make your code easier to follow. Finally, at the end of your script, it is good practice to free up resources and close your connection with these statements:

    mysql_free_result($sql_result);
    mysql_close($connection);
    Last edited by 7stud; Jun 1, 2001 at 10:36.

  3. #3
    SitePoint Member
    Join Date
    Jun 2001
    Location
    uk
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for the reply, although unfortunately no joy. Sorry i wasn't implying any security issues at all. I thought i may have made an error with my set-up.

    I have tried creating a new table using the following script

    $sql = "CREATE TABLE tester ( " .
    "ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY, " .
    "testerText TEXT, " .
    "testerDate DATE NOT NULL " .
    ")";if ( mysql_query($sql) ) {
    echo("<P>tester table successfully created!</P>");
    } else {
    echo("<P>Error creating tester table: " .
    mysql_error() . "</P>");
    }

    and then checked the server thats running mysql and it's definately there. I have also added some info onto the table but when i use the following script to try and access the information, i still end up with a blank page.

    $dbcnx = @mysql_connect("localhost", "", "");
    if (!$dbcnx) {
    echo( "<P>Unable to connect to the " .
    "database server at this time.</P>" );
    exit();
    }
    if (! @mysql_select_db("jokes") ) {
    echo( "<P>Unable to locate the joke " .
    "database at this time.</P>" );
    exit();
    }

    $result = mysql_query("SELECT testerText FROM tester");
    if (!result) {
    echo( "<P>Unable to locate the tester " .
    "info at this time.</P>" );
    exit();
    }

    can you find anything in there that i am doing totally wrong?

    even if i right click and select view source i still see nothing so as far as i can tell the server is recognising the php side of things. I tried using the code you sent me, but still nothing.

  4. #4
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    One problem is this

    PHP Code:
    if (!result) { 
    should read

    PHP Code:
    if (!$result) { 
    Next where is the code you are using to try and print the data from the database?
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  5. #5
    SitePoint Member
    Join Date
    Jun 2001
    Location
    uk
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that's it. i'd made the same silly mistake on the code that i was using to print. how silly!!

    thanks again

  6. #6
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Just for future posts, that error you made would have given you a parse error, and php should have given you a line number where it thought the error occurred (first check that line and then work your way upwards until you find the error). When you are posting about a problem with a script, always include any parse errors and their line numbers.
    Last edited by 7stud; Jun 1, 2001 at 23:37.


Bookmarks

Posting Permissions

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