Mysqli to pdo

I have corrected most of it but then have come across the few lines below, which I’m getting errors for.

$mResult = mysqli_query($con,$mQry) or ("Wrong Select Query");
$mRecordCount = mysqli_num_rows($mResult);

I think I have the first one corrected but cant work out the second line, the first one being

$mResult = $pdo->prepare($mQry) or ("Wrong Select Query");

But not 100%, and these are the errors

Notice: Undefined variable: con in \\CSFFILES11\WEBSITES\dev\category-Result.php on line 701

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in \\CSFFILES11\WEBSITES\dev\category-Result.php on line 701

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in \\CSFFILES11\WEBSITES\dev\category-Result.php on line 703

Is this correct guys

$mResult = $pdo->prepare($mQry) or ("Wrong Select Query");

$mRecordCount = $mResult->fetchColumn();

For counting rows you can use $mResult->rowCount() if you want to count rows, fetchColumn is use if you want to fetch specified column from table.

And you don’t need to use or ("Wrong Select Query") in PDO you have error reporting for PDO errors

No. the expression after or does nothing and $mQry looks like it contains data that belong into parameters. fetchColumn() only retrieves a single value (not sure if you want that here).

tl;dr it depends on the code not shown.

1 Like

Ah thank you guys, Im glad I asked if its right as changing it to rowCount fixed the issue that came from me using fetchColumn

It was in the pagination.

Thanks both

Sorry guys, have just come across another -

$q=mysqli_fetch_assoc($result) or $morevalue=false;

Used inside

for($j=1; $j<=$cnt; $j++) {
$q=mysqli_fetch_assoc($result) or $morevalue=false;

I notice you are not putting any data in your execute. If it’s a fixed statement with no variables, there is no need to prepare and execute.
Or are you binding some parameters elsewhere, that we can’t see?

1 Like
while ($q=mysqli_fetch_assoc($result))

makes much more sense.

Right I see, so I can leave prepare out and execute, cheers, didnt know that

But I don’t think I can use that when I’m moving over to use PDO instead,

Do use prepare if the query contains variable data, particularly user input (and of course bind parameters for that data) or if you want to re-use the query multiple times.
Otherwise, there is no need.

1 Like

Still cant quite work out that last one I posted, I’m getting this error

Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, object given in \\CSFFILES11\WEBSITES\dev\en\category-Result.php on line 771

I tried

while($q=$pdo->fetch($result) or $morevalue=false){

But broke it, as dont think there any need for the db reference in using $pdo

You can’t mix mysqli with PDO.

No I know, its a process of elimination lol

This seems to have done the trick in that those errors have gone,

for($j=1; $j<=$cnt; $j++) {

But what has happened now is the values such as echo $q[‘Nom_Hot’] are not echo’ing out


Reminds me of this post…

LIve Update from MySQL to PDO went smoothly - #2 by solidcodes

…where I updated from MySQL to PDO on a live site thanks to CodeIgniter’s Active Record Class.

Umm, I’m lost…

Just to say it was working using mysqli and the changes have stopped me echoing out the values, so I used var_dump as below

for($j=1; $j<=$cnt; $j++) {

And got bool(false), so its failing to pass the values over is that right?

no .

I think I have it to my surprise…

$stmt = $result;
while($q = $stmt->fetch()){

while($q = $stmt->fetchAll()){ will give you an array with multiple results.