Jump to content

mysql_connect


blackhawk08

Recommended Posts

Try this:

<?php

// Connect to the database
$host = "HOST"; // db host
$user = "USERNAME"; // db username
$pass = "PASSWORD"; // db password
$db = "DATABASE NAME"; // db name

$connection = mysql_connect ($host, $user, $pass) or die ("Unable to connect"); 
mysql_select_db ($db) or die ("Unable to select database"); 
?>

 

Remember to replace all the capitalized words with your own.

Link to comment
Share on other sites

this creates no output, not even in the source

 

<html>
    <head>
        <title>Example</title>
    </head>
<?php

include 'dbconnect.php';

$query = "SELECT * FROM Person";
$result = mysql_query($sql);
$people = array();
while($row = mysql_fetch_array($result)){
  $people[] = $row;
}
?>

    <body>
       <?php die("<pre>" . print_r($people,true) . "</pre>"); ?>
    </body>
</html> 

 

the included file is:

<?php

// Connect to the database
$host = "localhost"; // db host
$user = "root"; // db username
$pass = "********"; // db password
$db = "testing"; // db name

$connection = mysql_connect ($host, $user, $pass) or die ("Unable to connect"); 
mysql_select_db ($db) or die ("Unable to select database"); 

?>

Link to comment
Share on other sites

Hi,

    Make sure you have the host address and the port written correctly. If it still aint working... my only guess comes that the connection config aint correct. You could also check if there is an error in the username and password, else if the user is assigned the rights to access the database.

 

Thanks

 

Jyot Vakharia

Link to comment
Share on other sites

Well, maybe you want to extend your error trapping while you're at it.

 

$query = "SELECT * FROM Person";
$result = mysql_query($query) or die("Error: ". mysql_error(). " with query ". $query); // what's happening?

Link to comment
Share on other sites

im at a complete loss here...reinstalled everything...now im using a WAMP bundle...cant seem to figure out whats going on...here's my latest code

 

<html>
<head>
<title>add info into sql</title>
</head>
<body>
<?php
include 'dbconnect.php';
{
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$age = $_POST['age'];

$query = "INSERT INTO Person('FirstName', 'LastName', 'Age');
mysql_query($query) or die('Error, Insert query failed');


mysql_Close($conn)
echo 'new data added';
}
else
{
?>
<form method="post">
<table width="400" border="0" cellspacing="1" cellpadding="2">
<tr>
<td width="100">Firstname</td>
<td><input name="Firstname" type="text" id="firstname"></td>
</tr>
<tr>
<td width="100">lastname</td>
<td><input name="lastname" type="text" id="lastname"></td>
</tr>
<tr>
<td width="100">age</td>
<td><input name="age" type="text" id="age"></td>
</tr>
<tr>
<td width="100"> </td>
<td> </td>
</tr>
<tr>
<td width="100"> </td>
<td><input name="add" type="submit" id="add" value="Add New Info"></td>
</tr>
</table>
</form>
<?php
}
?>
</body>
</html>

 

and the dbconnect.php is:

<?php

// Connect to the database
$host = "127.0.0.1:3307"; // db host
$user = "root"; // db username
$pass = "ind34dol"; // db password
$db = "testing"; // db name

$connection = mysql_connect ($host, $user, $pass) or die ("Unable to connect"); 
mysql_select_db ($db) or die ("Unable to select database"); 

?>

 

what am i missing here? where am i screwing up? im not getting ANY output, not even the form that i created..NOTHING

 

 

Link to comment
Share on other sites

Looks like you have forgotten to close a string somewhere in you code, if you look at the first code block above you will see about half way through the syntax colouring goes wrong.

 

This is the offending line:

$query = "INSERT INTO Person('FirstName', 'LastName', 'Age');

Change it to:

$query = "INSERT INTO Person('FirstName', 'LastName', 'Age')";

 

Also it is a good idea to run display_errors on in your php.ini. If you make any changes to the php.ini make sure you restart Apache.

 

 

EDIT: Ooh MadTechie beat me to it.

Link to comment
Share on other sites

yes, everything works until i try adding the mysql_connect() command

Check that the mysql extension is loaded by running phpinfo() and scrolling down the page until you find a mysql section.

 

Click the link in my sig below and read the FAQ thread titled called to undefined function mysql_connect

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.