tags:

views:

39

answers:

2

I have been trying to convert my script from mysql to mysqli and during the process I got the following error listed below.

Warning: mysqli_query() expects parameter 1 to be mysqli, string given in on line 41

Warning: mysqli_error() expects exactly 1 parameter, 0 given in on line 43

Here is the full code below.

<?php

require_once ('./mysqli_connect.php'); // Connect to the db.

if (isset($_POST['submitted'])) {

// Query member data from the database and ready it for display
 $mysqli = new mysqli("localhost", "root", "", "sitename");
 $dbc = mysqli_query($mysqli,"SELECT * FROM tags");
if (!$dbc) {
 // There was an error...do something about it here...
 print mysqli_error($mysqli);
}

// grab the tag
$tag = mysqli_real_escape_string($mysqli, $_POST['tag']);

// see if the tag already exists and potentially what the current count is
 $mysqli = new mysqli("localhost", "root", "", "sitename");
 $dbc = mysqli_query($mysqli,"SELECT id, count, page FROM tags WHERE tag='$tag'");

//--if there is a row, that means the tag exists
if(mysqli_num_rows($dbc))
{
//--pull out the tag ID and the current count and increment by one.
  $tag_info = mysqli_fetch_array($result);
  $tag_info_id = $tag_info["id"];
  $tag_info_count = $tag_info["count"] + 1;

//--update the table with the new count
  $sql_update_cnt = "UPDATE tags SET count='$tag_info_count' 
       WHERE id='$tag_info_id'";
  mysqli_query($sql_update_cnt);

  echo "$tag now with $tag_info_count instances";
}
else
{
// tag is not there, so insert a new instance
  $query = "INSERT INTO tags (tag, count) VALUES ('$tag', 1)";
if (!mysqli_query($query, $dbc))
  {
  die('Error: ' . mysqli_error());
  }
echo "1 record added";
}
mysqli_close($dbc);
}
?>
A: 

you're calling it incorrectly.

Should be $mysqli->query("SELECT * FROM tags");

$mysqli is the object and query is the method on that object. It only requires you to pass in the query. Obviously all calls to mysqi_query need to be changed to match.

Steve Francia
He's using the procedural API, not the OO one.
Jacob Relkin
Therefore, he should use mysqli_connect
Jacob Relkin
A: 

May I suggest you take a step backwards and use a wrapper for mysqli? I've been using Rob Poyntz codesense_mysqli wrapper consistently in recent months and I've found it to be excellent for reducing the sheer amount of cruft that mysqli otherwise dumps into your code.

Of course it's good to know what's going on under the hood, but sometimes one just needs the cleanest method of getting the car to work.

Cruachan