Not understanding MySqli

I am trying to learn mysqli and so far I think it is way to much. MySql was much simpler to get what you were after.

For example with MySql all you needed was

<?php echo $row['itemname'];?>

now with MySql you need all this jumbo

<?php 
	while($row = $result->fetch_assoc()) {
	echo stripslashes($row['itemname']);	
} ?>

Am I missing something or do you really need all that code just to display data from your table now? I am wanting to learn MySqli but the more I look at it the more I steer clear of it. I know that soon mysql will be outdated, but to me it just makes things much more easy to get what your after.

That’s all you need to do the equivalent in MYSQLi too. In both cases you need a second statement to load the value into $row.

mysql example:

$sql    = 'SELECT itemname FROM bar WHERE id = 42';
$result = mysql_query($sql, $link);
while ($row = mysql_fetch_assoc($result)) {
    echo $row['itemname];
}

mysqli equivalent example:

$sql    = 'SELECT itemname FROM bar WHERE id = 42';
$result = $link->query($sql);
while ($row = $result->fetch_assoc()) {
    echo $row['itemname'];
}

Almost no difference at all. It is only once you implement prepare statements in order to eliminate any possibility at all of injection that the mysqli becomes slightly more complicated and you get rid of all the escaping at the same time so that’s not much different in the amount of code either but a lot more secure.

I added this to my page

<?php 
            $sql    = 'SELECT * FROM new_equip WHERE featured = 1';
			$result = $link->query($sql);
			while ($row = $result->fetch_assoc()) {
			    echo $row['itemname'];
			}
            ?>

But I am getting this error
Fatal error: Call to a member function query() on a non-object in

I’m a bit surprised the message isn’t a “non-resource” error, but I’m guessing there is a problem with the line
$link = new mysqli("localhost", "user", "password", "database");
* or whatever variant you’re using

http://php.net/manual/en/mysqli.quickstart.connections.php

I was about to say, he doesn’t have that line

$link = new mysqli("localhost", "user", "password", "database");

at all.

Scott

I have that at the very top

$db = new mysqli('localhost, "user", "password", "database");

so you need to use $db instead of $link in all the subsequent database references.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.