0

I turned the entire google upside down and I was unable to find solution for my problem. I have spent 2 months programming a website. I used a PDO connection, but recently switched to Mysqli because PDO started to cause me trouble. I am not so experienced, I think there is solution for the problems that PDO caused me, but I've already spend days trying to figure out how to solve the problems... nothing worked so I switched to Mysqli.

There is just one thing that I need to do in order to complete the conversion of my project from PDO to Mysqli.

$stmt = $conn->prepare("SELECT * FROM users WHERE name = ? AND id = ?");
$stmt->bind_param("si", $categoryname, $categoryid);
$categoryname = "admin";
$categoryid = "13";
$stmt->execute();
$result = $stmt->get_result();

$dsafsdfa = $result[0]['name'];
echo $dsafsdfa;

Of course this code shows me error "Cannot use object of type mysqli_result as array" The fetchall also doesn't work, it also shows me error. Lets say this code is put into a file that is included all over my project. I need to write the code in a way in which it will allow me to set a variable like this:

$dsafsdfa = $result[0]['name'];

I know about the mysqli_fetch_assoc($result); option but, as i said this file is included all over my project. After the file is included there are always variables are set in this way. If i don't find a way to make the code return me result as an array, i will have to re-code 40% of my project. And my project goes above 30 000 lines of code. Is there a command/option that i don't know that can help me out in this situation? Thank you for your time.

Phil
  • 157,677
  • 23
  • 242
  • 245
  • Show your `fetch_all` code. What error are you getting? – Barmar Mar 13 '19 at 22:15
  • Do you have the `mysqlnd` driver installed? You need that to be able to use these functions with prepared statements. – Barmar Mar 13 '19 at 22:16
  • 1
    What kind of problems are you having with PDO? In my experience, PDO is much easier to use than mysqli. – Barmar Mar 13 '19 at 22:16
  • Fatal error: Uncaught Error: Call to undefined method mysqli_stmt::fetchAll() – Black Sun Phoenix Entertainmen Mar 13 '19 at 22:23
  • The name is `fetch_all()`, not `fetchAll()`. And it's a method of `mysqli_result`, not `mysqli_stmt`. – Barmar Mar 13 '19 at 22:24
  • 1
    `fetchAll()` is the PDO method. mysqli doesn't use camelcase naming. – Barmar Mar 13 '19 at 22:24
  • So it should be `$rows = $result->fetch_all();` – Barmar Mar 13 '19 at 22:25
  • That's the error i get using fetchall. It's not worth the time and patience giving the big explanation why PDO sucks for my project. Btw, for first time i hear about a driver... gosh. In my experience i never had to deal with drivers? Can you give me really quick explanation of what this is. Is this like the javascript plugins or something? Is it something that my Host has to have installed also? – Black Sun Phoenix Entertainmen Mar 13 '19 at 22:27
  • 1
    It's part of the PHP installation on the server. See https://stackoverflow.com/questions/13159518/how-to-enable-mysqlnd-for-php – Barmar Mar 13 '19 at 22:28
  • 1
    Why is this the first you're hearing? It says it right on the [documentation page](http://php.net/manual/en/mysqli-result.fetch-all.php#refsect1-mysqli-result.fetch-all-mysqlnd): **MySQL Native Driver Only** – Barmar Mar 13 '19 at 22:29
  • Well... thanks for your time guys. I gues i will have to make my site in a way in which it will work with both PDO and Mysqli. Sounds so amateur LOL... but there is no other way. – Black Sun Phoenix Entertainmen Mar 13 '19 at 22:32

0 Answers0