Jump to content

mysql_connect problems?


excel11

Recommended Posts

Very new to this.  I am just trying to get the basics of php now.  I have programmed with ASP so not new to concepts of programming.

 

Any way I can't seem to get any errors or anything displayed on the screen when trying to connect to the mysql database

I can connect to the mysql db by using the command line with no problems.  But when issuing the below code I get nothing on the screen.  No errors, nothing.  In asp if your code can't do something IE, will prompt you with some time of error.  Can't php do this as well.  Do I have to configure my apache server somehow.

 

<html>

<body>

<?php

 

$username="excel11";

$password="asdf";

$hostname="localhost";

$database="my_db";

 

mysql_connect($hostname,$username,$password);

mysql_select_db($database);

   

$query="Select * from users";

 

echo $query;

 

?>

</body>

</html>

 

Link to comment
Share on other sites

try something like

<?php
$con = mysql_connect("$hostname", "$username", "$password") or die(mysql_error());
mysql_select_db('$database', $con) or die(mysql_error());

$query="Select * from users";

echo $query;
?>

 

the

or die(mysql_error());

will display an error message if it can't connect

Link to comment
Share on other sites

Still nothing.  The page is completly blank.

 

This is the output from the command line to indicate something is within the db.

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 1

Server version: 5.0.37-community-nt MySQL Community Edition (GPL)

 

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

 

mysql> use my_db

Database changed

mysql> select * from users;

+---------+-------+------+

| fname  | lname | age  |

+---------+-------+------+

| Michael | Duff  |  28 |

+---------+-------+------+

1 row in set (0.16 sec)

Link to comment
Share on other sites

Are you trying to echo the actual query string, or the results of the query?

 

If you want the query results to be displayed, you will need to actually run the query with mysql_query():

 

<?php
$con = mysql_connect("$hostname", "$username", "$password") or die(mysql_error());
mysql_select_db('$database', $con) or die(mysql_error());

$query="Select * from users";
$result = mysql_query($query);
if($result) {
   while($row = mysql_fetch_array($result)){
       $fname = $row["fname"];
        $lname = $row['lname'];
        $age = $row['age'];
       echo "Name: ".$fname." ".$lname.", ".$age."<br />";
   }
}

?>

Link to comment
Share on other sites

I don't know what could be the possible cause for nothing to be displayed on the screen but I still get nothing.

 

If the connect statement can't connect you are supposed to be issued the mysql_error() message, but nothing.  Just a blank page.

 

It doesn't seem that this is a normal problem.  Was there something I forgot to install with PHP?

 

 

Link to comment
Share on other sites

just to check.. if you've used what heyray2 said, have you switched the variables with ones from your table?

        $fname = $row["fname"];
        $lname = $row['lname'];
        $age = $row['age'];
        echo "Name: ".$fname." ".$lname.", ".$age."<br />";

fname, lname and age may not be columns in your table...

and because you have no error message for the echo statement, if it can't find any information for the set variables it will just be blank.

 

are you sure that 'users' is your table name? it's case sensative.. so might be Users, or USERS etc..

Link to comment
Share on other sites

Solved!!!!!! ;D

 

 

I checked Apache error logs and found the following:

Call to undefined function mysql_connect()

 

Found out that PHP5+ doesn't come with MySQL installed.  You have to manually configure this.

 

Create Directory within your php directory such as: c:\Program Files\PHP\ext

Within your php.ini file edit the line: extension_dir with extension_dir= "c:\Program Files\PHP\ext"

 

Make sure that you have the following files php_mysql.dll and libmysql.dll on your computer else download them here: http://dev.mysql.com/downloads/connector/php/

 

If you have them on your computer make sure that you copy them into the above directory that you created.

 

Everything should work now!

 

 

I appreciate all the help that you guys gave me and hopefully now I can proceed in my learning php.

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.