Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This question already has an answer here:

I am trying to work with databases on PHP, however I am getting an error when I try to make a table of data. The table seems to be created correctly (I think) but when I want to print the confirmation of the table is created, it throws me an error. This is the code:

   if (mysql_query($connection,$sql)) //error
   {
      echo "1 record added";
   }
   else
   {
      die('Error: ' . mysql_error($con)); //error
   }

They both give me the error mysql_query() expects parameter 1 to be string and mysql_error() expects parameter 1 to be resource

Where $sql - the info being put in the table

share|improve this question
    
the error says that the function mysql_query() expects parameter 1 to be string. Are you sure that variables $connection and $sql are string? –  Vainglory07 Feb 17 at 1:39
    
thats backwards, parameters should be: ($sql,$connection) –  Dagon Feb 17 at 1:39
1  
Why is it called $connection first` and $con second? And why didn't you just try switching parameter order for the first? Isn't the error message clear? –  mario Feb 17 at 1:39
1  
You are calling mysql_query() like mysqli_query(), which takes a resource as its first param. –  Michael Berkowski Feb 17 at 1:39
1  
Are people still using mysql functions? Please, get with the times and use mysqli or pdo. –  Christian Varga Feb 17 at 1:40
show 2 more comments

marked as duplicate by John Conde, andrewsi, Chris, Shankar Damodaran, Hüseyin BABAL Feb 24 at 6:44

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

3 Answers

up vote 1 down vote accepted

That won't work because mysql_query only returns a boolean based on if the query failed to execute or not (i.e. SQL syntax error).

You'll need to use mysql_affected_rows instead.

mysql_query($sql);
if(mysql_affected_rows()) {
    echo "1 record added";
}

Please take note of the scary red box on that page. mysql_query is deprecated. You need to switch to mysqli

share|improve this answer
    
+1 for the comment about mysql functions, I can't believe are still trying to use mysql in new projects. –  Christian Varga Feb 17 at 1:42
    
how do you know its new ? –  Dagon Feb 17 at 1:46
    
I took your advice to switch to mysqli but now it is expecting a mysqli parameter. What exactly is a mysqli parameter? –  user1404664 Feb 17 at 1:50
    
You will have to switch all your functions to mysqli, not just one –  Machavity Feb 17 at 3:11
add comment

Please read the manual for those functions, you are giving the wrong parameters (in order):

Here is another stack post that deals with the same issue with solutions:

http://stackoverflow.com/a/5554326/3199478

share|improve this answer
add comment
$server = 'localhost';  
$username   = 'root';  
$password   = 'YOUR PASSWORD';  
$database   = 'DB_name';  
$con = mysql_connect($server, $username,$password);
$db = mysql_select_db($database);
if(!$con)  
{  
    exit('Error: could not establish database connection');  
}  
if(!$db)  
{  
    exit('Error: could not select the database');  
} 
$sql ="CREATE TABLE Persons(FirstName CHAR(30),LastName CHAR(30),Age INT)";
$result = mysql_query($sql);
if (!$result) //error
   {
     echo mysql_error(); //error
   }
   else
   {
      echo "1 record added"; 
   }
share|improve this answer
add comment

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