James James - 2 months ago 7
MySQL Question

Little stuck with converting some old MySQL code to MySQLi

Noticed an old petition site i done year back has not been working for a while since PHP server upgrades but its a just for fun kind of page so checked and fixing it up but PHP is not the best so fixed most of it but stuck with confirming email now.


Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result,
array given in /home/merlin/public_html/index.php on line 64

Warning: mysqli_query() expects at least 2 parameters, 1 given in
/home/merlin/public_html/index.php on line 75

Warning: mysqli_error() expects exactly 1 parameter, 0 given in
/home/merlin/public_html/index.php on line 75


CODE: (ive attempted to fix up)

<?php

if(isset($_GET['key']) ? $_GET['key'] : '') {

include('database.php');

$confirmId = (isset($_GET['key']) ? $_GET['key'] : null);
$errorMessage = "";
$validCount = 0;

$sql = "SELECT confirmed FROM email_list WHERE confirmationcode='$confirmId'";
$sqll = mysqli_query($mysqli,$sql);
$result = mysqli_fetch_array($sqll);
$validCount = mysqli_num_rows($result);


if($result['confirmed'] == 1) {

echo "<div class=\"notice error\">You have already confirmed this petition signup.</div>";

} else if($result['confirmed'] == 0){

$query = "UPDATE email_list SET confirmed = 1 WHERE confirmationcode='$confirmId'";

mysqli_query($query) OR die(mysqli_error());

echo '<div class=\"notice error\">Your petition has been accepted, Thank you and with your help we might just get Merlin back on our screens. Why not spread the word on Twitter and Facebook? Just quick the icons to the right.</div>';

}

}

?>


PREVIOUS CODE: (converting from)

<?php

if(isset($_GET['key']) ? $_GET['key'] : '') {

include('database.php');

$confirmId = (isset($_GET['key']) ? $_GET['key'] : null);

$errorMessage = "";
$validCount = 0;

$query = "SELECT confirmed FROM email_list WHERE confirmationcode='$confirmId'";
$result = mysql_query($query);
$validCount = mysql_num_rows($result);

if($result['confirmed'] == 1) {

echo "<div class=\"notice error\">You have already confirmed this petition signup.</div>";

} else if($result['confirmed'] == 0){

$query = "UPDATE email_list SET confirmed = 1 WHERE confirmationcode='$confirmId'";

mysql_query($query) OR die(mysql_error());

echo '<div class=\"notice error\">Your petition has been accepted, Thank you and with your help we might just get Merlin back on our screens. Why not spread the word on Twitter and Facebook? Just quick the icons to the right.</div>';

}

}

?>


NEW database.php:

<?php

ini_set('display_errors', 1);
error_reporting(E_ALL);

$mysqli = new mysqli("localhost", "merlin_camp", "PASSWORD", "merlin_camp");

/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}

?>

Answer

You should use $sqll (and not $result .. because in your case is not the result of mysqli_query but the array form mysqli_fetch_array)

$sql = "SELECT confirmed FROM email_list WHERE confirmationcode='$confirmId'";
$sqll = mysqli_query($mysqli,$sql);
$result = mysqli_fetch_array($sqll);
$validCount = mysqli_num_rows($sqll );