PhillToronto
PhillToronto

Reputation: 93

Error connecting to MYSQL using PHP

I am working on an assignment that requires me to create a form that sends info to a database. I am having issues connecting to the server.

I have tried using mysql_connect and mysqli_connect and neither work.

mysql_connect error: Warning: mysql_connect(): Connection timed out

mysqli_connect error: Fatal error: Call to undefined function mysqli_connect()

                    $connect=mysql_connect("server.com","username","password","database")
                    or die("Failed to connect to server");
                    mysql_query("INSERT INTO client (title,fname,lname,organization,email,phone,tshirt) VALUES ($title,$fname,$lname,$org,$email,$phone,$tshirt)");

                    $tabledata = mysql_query("SELECT * FROM client");
                    echo "<table>
                    <tr>
                    <th>Title</th>
                    <th>First Name</th>
                    <th>Last Name</th>
                    <th>Organization</th>
                    <th>Email</th>
                    <th>Phone</th>
                    <th>TShirt</th>
                    </tr>";
                    while($row = mysql_fetch_array($tabledata)){
                            echo "<tr>";
                            echo "<td>" . $row['title']  . "</td>";
                            echo "<td>" . $row['fname']  . "</td>";
                            echo "<td>" . $row['lname']  . "</td>";
                            echo "<td>" . $row['org']    . "</td>";
                            echo "<td>" . $row['email']  . "</td>";
                            echo "<td>" . $row['phone']  . "</td>";
                            echo "<td>" . $row['tshirt'] . "</td>";
                            echo "</tr>";
                    }
                    echo "</table>";
                    mysql_close($connect);

Info from PHP INFO

mysql

MySQL Support enabled Active Persistent Links 0 Active Links 0 Client API version mysqlnd 5.0.10 - 20111026 - $Id: e707c415db32080b3752b232487a435ee0372157 $

PDO

PDO support enabled PDO drivers sqlite

pdo_sqlite

PDO Driver for SQLite 3.x enabled SQLite Library 3.7.7.1

sqlite3

SQLite3 support enabled SQLite3 module version 0.7 SQLite Library 3.7.7.1

Directive Local Value Master Value sqlite3.extension_dir no value no value

Upvotes: 0

Views: 2275

Answers (5)

jasinth premkumar
jasinth premkumar

Reputation: 1413

check whether you have enabled mysqli.dll in php ini file

with

<?php phpinfo(); ?>

if it isn't enable it in php.ini file //remove ';' from dll name

for more info to check

Upvotes: 0

dwjv
dwjv

Reputation: 1227

Is "server.com" this the real thing you're trying to connect to? Are you connecting to mysql on the local machine or is it external? Try a telnet from the php server:

telnet <remote db name/IP> 3306

If that fails, you have a network/firewall problem. Or the DB is is not listening on that port or on the network entirely. Do a netstat on the remote DB to confirm: Linux

netstat -an | grep LISTENING

You should see :3306 if it's listening on the default port.

Upvotes: 0

mehdi
mehdi

Reputation: 1755

please use PDO::__construct() or mysqli_connect but in your question

try :

$connect=mysql_connect("server.com","username","password");
mysql_select_db("database");

Upvotes: 0

Zy0n
Zy0n

Reputation: 785

First, are you connecting locally or externally to your DB? If you're connecting locally your parameters for mysql_connect(); would be as follows mysql_connect('localhost', 'username', 'password');

Then:

mysql_select_db('your_db') 

Upvotes: 0

Rogue
Rogue

Reputation: 11483

First, make sure mysql is running, and if it already is, then I would try installing the mysql extension for your php environment anyhow, just to make sure:

sudo apt-get install php5-mysql libapache2-mod-auth-mysql

Also, you would want to use mysqli_*, not mysql_*

After you attempt installing, just restart apache from there.

Upvotes: 1

Related Questions