0

I am trying to convert this PDO to Mysqli. I am a PHP noob and have been learning mysqli this whole time. However this snippet of code is in PDO and i need to make it mysqli.

Here is the original PDO code:

$connect = new PDO("mysql:host=localhost; dbname=testing;", "root", "");
function fill_select_box($connect, $category_id)
{
 $query = "
  SELECT * FROM team";
 $statement = $connect->prepare($query);
 $statement->execute();
 $result = $statement->fetchAll();
 $output = '';
 foreach($result as $row)
 {
  $output .= '<option value="'.$row["id"].'">'.$row["teamname"].'</option>';
 }
 return $output;
}

Here is my attempt at changing it to mysqli:

$connect= new mysqli('localhhost', 'root', '', 'testing');
if(!$connect){
    die(mysqli_error($connect));
}
function fill_select_box($connect, $category_id){
$sql="SELECT * FROM team";
$result=mysqli_query($connect,$sql);
while ($row = mysqli_fetch_assoc($result)){
    echo '<option value="'.$row["id"].'">'.$row["teamname"].'</option>';
}
}

Am i missing anything or am I correct with my conversion?

JahIsGucci
  • 17
  • 5
  • 4
    Why? PDO is generally much easier and more flexible than mysqli. – Barmar Apr 10 '23 at 23:33
  • 1
    `!$connect` will never be true.`new mysqli` always returns an object. If you want to check if it succeeded, use `if ($connect->connect_errno)` – Barmar Apr 10 '23 at 23:34
  • But your query and fetch loop loop are fine. – Barmar Apr 10 '23 at 23:35
  • Because I'm still a noob and my entire project is done in mysqli. So id like to finish project first and then think about switching to PDO. awesome! So besides the !$connect part, it all looks good? Much appreciated Barmar – JahIsGucci Apr 10 '23 at 23:45
  • As a noob you really should avoid learning such difficult things as mysqli. Just start with easy PDO. – Dharman Apr 11 '23 at 00:59
  • "*Am i missing anything or am I correct with my conversion?*" What makes you think something is wrong? – Dharman Apr 11 '23 at 01:00
  • Interesting you say that. I've gotten somewhat used to mysqli so changing the syntax this early in my learning phase has been difficult haha. just want to make sure all is good – JahIsGucci Apr 11 '23 at 01:29

0 Answers0