Question:
Whats wrong with this SQL statement? (with some PHP)?
SpaZz
2011-03-04 19:26:14 UTC
ok so I am reading in JSON data and want to insert it into a database and this is what I have. It comes back and says

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result

meaning the SQL statement is wrong, but I don't know whats wrong with it.


foreach($json_o->roster as $data)
{
//echo "{$data[0]} {$data[1]} {$data[2]}";
//echo "
";
require_once ('mysqli_connect.php');
$q = "INSERT INTO roster (character, effortP, gearP) VALUES ('$data[0]', '$data[1]', '$data[2]')";
$r = mysqli_query ($dbc, $q);
if (mysqli_num_rows($r) == 1) {
echo "All entry additions added successfully!";
}else{
mysqli_rollback($dbc);
}

}
}
Six answers:
anonymous
2011-03-04 20:06:35 UTC
I strongly recommend you to ALWAYS add this after any kind of query:



"or die(mysqli_error())"



so it will be like this:



$r = mysqli_query ($dbc, $q) or die(mysqli_error());



it will stop and show you the error (if exists) related to the query.



good luck and if possible copy the error here.
Rick B
2011-03-04 23:08:01 UTC
Just curious: after including mysqli_connect did you happen to call it so that $dbc is an active connection?
anonymous
2011-03-04 20:30:43 UTC
I agree with Jose. If

$r = mysqli_query ($dbc, $q);

returns an error, $r isn't a valid mysqli_result, so mysqli_num_rows($r) blows up.
anonymous
2016-10-05 04:50:31 UTC
the previous answer isn't precisely precise. If there are precisely 4 columns in normalrequests and the styles of each and every are such that the 4 provided values have compatibility, then your insert could artwork. although, it is many times a sturdy prepare to specify a minimum of a few column names to make confident that the values are mapped wisely (any columns no longer special could ought to have default values defined or be nullable.) yet another threat is that the 4th column is something different than Boolean, iwc fake could want to be enclosed in single fees.
Zero
2011-03-04 19:53:50 UTC
i don't see any thing wrong with the statement can you post the table structure

?
anonymous
2011-03-04 20:22:46 UTC
One thing I think may be wrong is the IF statement. Try doing something like this:



$check = mysqli_num_rows ($r)

if ($check==1){



This may get you on your way but if not I see one more thing to try, do back to your $q variable and fix the mysql query like so:



$q = "INSERT INTO `roster` (character, effortP, gearP) VALUES ('$data[0]', '$data[1]', '$data[2]')";



Notice the addition of ( ` ) around your table name, you should always have that (I forgot its name) in your mysql statements. You use ( ` ) around table/ database names and ( ' ) around data your putting in.



AND if all else fails try changing your $r variable to this:



$r = mysqli_query ("$dbc","$q");





Hope one of these things helps you out. I'm not to sure about the last one but I never query like that so I wouldn't know. I always open my database with mysql_connect followed by mysql_select_db in one PHP connect file, then run all my querys with mysql_query, then close it with mysql_close.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...