0

I started to learn PHP and MySql from Head First and now i'm in chapter 2 and i'm doing some exercises from book.

    $connection = mysql_connect("127.0.0.1", "root", "", "aliendatabase")
or die ("Oops! Couldn't connect to server because ". mysql_error());



    $sql="INSERT INTO aliens_abduction (first_name, last_name, when_it_happened, how_long, how_many, alien_description, what_they_did, fang_spotted, other, email)
    VALUES ('$first_name', '$last_name', '$when_it_happened', '$how_long', 'how_many', '$alien_description', '$what_they_did', '$fang_spotted', '$other', '$email')";


    mysqli_query($connection, $sql)
    or die('Error querying database.' . mysql_error());

  mysqli_close($connection);

While i was writing that part, i keep getting "Error querying database" error. Can anyone show me what's wrong in this code?

7
  • "Error querying database" should be followed by the mysql_error(). What is the error? Jul 17, 2014 at 20:13
  • It doesn't say anything just "Error querying database.".
    – Mert Serin
    Jul 17, 2014 at 20:15
  • @user3559801 You're mixing mysqli and mysql. You can't youse these both at te same time ;) I suggest you to use MYSQLI :)
    – GCallie
    Jul 17, 2014 at 20:18
  • @Callie are there any important difference between mysql and mysqli? :)
    – Mert Serin
    Jul 17, 2014 at 20:23
  • @user3559801 Well, msqli is just a newer library/version with more functionallity especially for Object Oriented Programming. The 'i' stands for improved.Here you can find a list of the improvements. here
    – GCallie
    Jul 17, 2014 at 20:37

4 Answers 4

6

You cant use mysqli_query after starting with mysql_query.

<?php

$connection = mysql_connect("127.0.0.1", "root", "", "test") or die ("Oops! Couldn't connect to server because ". mysql_error());
mysql_select_db("test");


$sql="INSERT INTO users (id) VALUES (100000)";


mysql_query($sql,$connection) or die('Error querying database.' . mysql_error());

mysql_close($connection);

Here is a tutorial for Mysqli: Link

For this purpose here is the mysqli code:

$db = new mysqli('localhost', 'root', '', 'aliendatabase');

if($db->connect_errno > 0){
    die('Unable to connect to database [' . $db->connect_error . ']');
}

$sql = "INSERT INTO aliens_abduction (first_name, last_name, when_it_happened, how_long, how_many, alien_description, what_they_did, fang_spotted, other, email)
VALUES ('$first_name', '$last_name', '$when_it_happened', '$how_long', 'how_many', '$alien_description', '$what_they_did', '$fang_spotted', '$other', '$email')";

if(!$result = $db->query($sql)){
    die('There was an error running the query [' . $db->error . ']');
}

$db->close();
3
  • So should i change mysql_connect() to mysqli_connect()?
    – Mert Serin
    Jul 17, 2014 at 20:16
  • If you know how to use Myslqi yes... But it seems like you don't. I added a tutorial for it ;)
    – Ozan Kurt
    Jul 17, 2014 at 20:17
  • I'll try to figure it out somehow. Thanks
    – Mert Serin
    Jul 17, 2014 at 20:19
1

You're mixing mysqli (note the (i)) and mysql (note the LACK of an (i)). The two libraries are NOT interchangeable/mixable like that. Connections established in one are NOT usable by the other.

Note the differences in your calls:

mysql_connect()
mysqli_query()
mysql_error()
mysqli_close()
2
  • 1
    I already told that...
    – Ozan Kurt
    Jul 17, 2014 at 20:17
  • @OzanKurt: Yes, and? So I should obsessively reload the site just in case someone answered a few seconds before I hit the submit button?
    – Marc B
    Jul 17, 2014 at 20:20
0

For this purpose here is the mysqli code:

$db = new mysqli('localhost', 'root', '', 'aliendatabase');

if($db->connect_errno > 0){
    die('Unable to connect to database [' . $db->connect_error . ']');
}

$sql = "INSERT INTO aliens_abduction (first_name, last_name, when_it_happened, how_long, how_many, alien_description, what_they_did, fang_spotted, other, email)
VALUES ('$first_name', '$last_name', '$when_it_happened', '$how_long', 'how_many', '$alien_description', '$what_they_did', '$fang_spotted', '$other', '$email')";

if(!$result = $db->query($sql)){
    die('There was an error running the query [' . $db->error . ']');
}

$db->close();
2
0

You shouldn't be using mysql. The reasons are explained in this article. http://php.net/manual/en/migration55.deprecated.php Also, you can't mix mysqli with mysql. Here is a brief solution unsing mysqli

$connection = new mysqli("127.0.0.1", "root", "", "test") 


$sql="INSERT INTO users (id) VALUES (100000)";


if(mysqli_query($sql,$connection))
{
   //message  if everything goes right
}
else
{
   printf("Error: %s\n", mysqli_error($connection)); //will print the errors
}
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.