1

I haven't used PHP or SQL in a while, and can't seem to figure out why this query is failing. Probsbly going to be something silly :).

<php?
    $dbconn = mysql_connect("localhost","xxx","xxx");
    if (!$dbconn)
    {
    die('Error connecting to DB!');
    }
    if (! @mysql_select_db('rdrkictj_rsvp') ) 
    { 
        die(mysql_error()); 
    } 
    if(isset($_GET['id'])){
        $ID = $_GET['id'];
        $stockcount = $_GET['stockcount'] - 1;
    }
    else
        die(mysql_error()); 
    
    mysqli_query($dbconn,'UPDATE products SET stockcount = "5" WHERE id = "1"');
    
    mysqli_close($dbconn);
?>

I receive the following errors:

Warning: mysqli_query() expects parameter 1 to be mysqli, resource given in /home/rdrkictj/public_html/test/buyit.php on line 19

Warning: mysqli_close() expects parameter 1 to be mysqli, resource given in /home/rdrkictj/public_html/test/buyit.php on line 21

Any advice would be greatly appreciated.

Community
  • 1
  • 1
phunder
  • 1,607
  • 3
  • 17
  • 33
  • possible duplicate of [mysql\_fetch\_array() expects parameter 1 to be resource, boolean given in select](http://stackoverflow.com/questions/2973202/mysql-fetch-array-expects-parameter-1-to-be-resource-boolean-given-in-select) – John Conde Feb 02 '14 at 14:09

3 Answers3

1

<php? should be <?php, also you're mixing mysql functions with mysqli functions. Choose one of them (mysqli). So, change: he

mysql_connect("localhost","xxx","xxx");

to the mysqli equivalent:

mysqli_connect("localhost","xxx","xxx");

Also change mysql_error() to mysqli_error(),

and finally change:

@mysql_select_db

to:

@mysqli_select_db

display-name-is-missing
  • 4,424
  • 5
  • 28
  • 41
0

Use either mysqli or mysql (mysqli is recommended)

For example:

$dbconn = mysql_connect("localhost","xxx","xxx");

should be

$dbconn = mysqli_connect("localhost","xxx","xxx");

Same for others as well

Full Code:

<?php
    $dbconn = mysqli_connect("localhost","xxx","xxx") or die('Error connecting to server');

    if (! @mysqli_select_db($dbconn, 'rdrkictj_rsvp') ) 
    { 
        die(mysqli_error($dbconn)); 
    } 
    if(isset($_GET['id'])){
        $ID = $_GET['id'];
        $stockcount = $_GET['stockcount'] - 1;
    }
    else
        die(mysqli_error($dbconn)); 

    mysqli_query($dbconn,'UPDATE products SET stockcount = "5" WHERE id = "1"');

    mysqli_close($dbconn);
?>
gurudeb
  • 1,856
  • 22
  • 29
0

OK so that is what happens when you mix two tutorial together -_-

Thanks to both respondents. The following code works:

$con=mysqli_connect("localhost","xxx","xxx","xxx");

    if (mysqli_connect_errno())
      {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }

    mysqli_query($con,'UPDATE products SET stockcount = "5" WHERE id = "1"');

    mysqli_close($con);
phunder
  • 1,607
  • 3
  • 17
  • 33