Jump to content

Problem with MySQL_Query


Corbokhan

Recommended Posts

I'm pretty new to PHP and I'm having trouble inserting data into my database from a webpage. Here is the code.

 

The error is on the lines with $query and $result. The error comes back:

 

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/content/76/5697876/html/corbinreynolds/insert.php on line 39

Lost connection to MySQL server at 'reading initial communication packet', system error: 111

 

I've been working at this for hours and can't seem to figure this out. Any ideas?

 

<?php

$first=$_POST['first'];
$last=$_POST['last'];

if (!$first || !$last) {
echo 'Error: Enter the required data.';
exit;
}

if (!get_magic_quotes_gpc()) {
    $first = addslashes($first);
    $last = addslashes($last);
}

$hostname = '127.0.0.1';
$username = 'root';
$password = 'root';
$dbname = 'phonebook';

@ $db = mysql_connect($hostname,$username.$password,$dbname);

if (mysqli_connect_errno()) {
echo 'Errer: Could not connect to database';
echo '<br>';
echo mysqli_connect_error();
exit;
}

$query = "INSERT INTO contact (first,last) VALUES ('$first','$last')";
$result = mysql_query($query,$db) or die(mysql_error());

if ($result) {
echo $db->affected_rows." contact inserted into the database";
echo 'Contact Entered:  '.$first.' '.$last.'';
}
else {
echo 'Error: Contact not added.';
exit;
}

echo 'Closing Database';
$db->close();

?>

 

Link to comment
Share on other sites

First off, you are trying to use both mysql and mysqlI functions.  You can't do that, pick one.  Second, you have an error in your connect string.  There is a dot instead of a comma.  Thirdly, $db is not an object.  I think you have just copied code from different places and tried to hack it together. Also, don't use the @ error suppression, ever.

Link to comment
Share on other sites

Line 39 starts with $query.

111 is the error code.

 

There is no reason for that error message when you just assign a string to a variable. So probably the line under?

 

 

@ $db = mysql_connect($hostname,$username.$password,$dbname);

 

correct me if I'm wrong, but in the php documentation, it never says you can provide the database name when you connect, you gotta do that with it's own function.

http://no.php.net/manual/en/function.mysql-connect.php

 

How to do it:

mysql_connect($mysqlhost,$mysqlusername,$mysqlpassword);

@mysql_select_db($mysqldatabase) or die($mysqlerror);

Link to comment
Share on other sites

moreover once you do the query(supposing u corrected all of the errors above) u need to store it in an array or as an object or fetch it as an asociative array before you get your results....do a mysql_fetch_assoc($result) and storee this in a variable say $result_row and then access your column names or field names of your table just like a normal key,value pair

Link to comment
Share on other sites

nah, it's good when the function doesn't return TRUE when it succeeds, but it returns FALSE when it doesn't.

 

if(@fopen($file,'r'))

 

^ to check if I actually could load something from another page, because the webpage might be down.

 

would be super happy if you had another short code for that. (it was a picture btw, for then to get different properties from it)

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.