Jump to content

Warning: mysqli_num_rows() expects exactly 1 parameter


dojo

Recommended Posts

Hello everyone,

 

I am new to php coding and i am currently trying to check for duplicate data when inserting new values (i.e. a question) in mysql database table. I keep getting this error 'Warning: mysqli_num_rows() expects exactly 1 parameter,2 given in'.  

Below is the code i am currently working with. Thanks in advance

 

<?php
session_start();
$link = mysqli_connect("elephant.ecs.westminster.ac.uk", "w1268094", "euMm27FZwZDK", "w1268094_0");
?>
<?php
//Error reporting due to long script
error_reporting(E_ALL);
ini_set('display_errors','1');
?>


<?php
ob_start();
//Add question to table
If (isset($_POST['question'])) {


$question = mysqli_real_escape_string($link, $_POST['question']);
$date = mysqli_real_escape_string($link, $_POST['questiondate']);
//See if question is identical to another question in the table
$sql = mysqli_query($link,"SELECT pQuestionNo FROM presidentQuestion WHERE pQuestion='$question'LIMIT 1");
$questionMatch = mysqli_num_rows($link, $sql); //count the output amount
if ($questionMatch>0){
echo 'Sorry you tried to place a duplicate "Question" into the table, <a href="inventory.php">Click here</a>';
exit();
}
//Add the question to the database 
$sql = mysqli_query($link,"INSERT INTO presidentQuestion (pQuestion,pDate)
VALUES ('". mysqli_real_escape_string($link, $question)  ."','". mysqli_real_escape_string($link, $date)  ."')") or die (mysqli_error($link));
header("location: inventory.php");
exit();
}
?>


<?php
//Grabs the whole queston list
$question_list="";
$sql = mysqli_query($link,"SELECT * FROM presidentQuestion LIMIT 0,10") or die(mysql_error());
$questionCount = mysqli_num_rows($sql);// count the output amount


if($questionCount>0){
while($row = mysqli_fetch_array($sql, MYSQLI_ASSOC)){
$question = $row["pQuestion"];
$date = $row["pDate"];
$question_list .= "$question  <br/>";
}
}else{
$question_list = "There are no questions in the inventory yet";
}
?>


<?php 
$prop_id = $_GET['prop_id'];
?>

 

You get that when you don't have a query that ran.  ALWAYS check the results of an operation to be sure it worked before moving on.

 

Also as a newbie - you should read the posts at the top of the forum that say to please read them.  This very problem is discussed there since everybody goes thru it once.

 

Also I notice that your insert query is doing a second escape on your args.  Not good.  You already did it once, why are you doing it again?

If the error message is:

'Warning: mysqli_num_rows() expects exactly 1 parameter,2 given in'.  

Try looking at that line:

 

$sql = mysqli_query($link,"SELECT pQuestionNo FROM presidentQuestion WHERE pQuestion='$question'LIMIT 1");
$questionMatch = mysqli_num_rows($link, $sql); //count the output amount

 

mysqli returns an object, so you don't need to pass the link, just the result (in your case $sql). So, it needs only 1 parameter (like the error message says).

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.