mysql - PHP MYSQLI fetch Errors -
at school teacher told me lot of thing coding in php using postgres database. when started use mysqli server couldn't lot of thing.
first of functions. teacher told me type of function
$rows = getbuildings(); if (count($elenco) == 0) { print "<hr> no buildings here </hr>"; } else { ?> <thead> <tr> <th>address</th> <th>town</th> <th>manager</th> </tr> </thead> <tbody> <?php foreach ($elenco $riga) { ?> <tr> <td> <?= $row->address ?></td> <td> <?= $row->town ?></td> <td> <?= $row->name ?> <?= $riga->surname ?> </td> </tr> <?php } ?> </tbody> </table> </tbody> </table> <?php } ?> </body> </html> <?php function getbuldings() { global $db; $sql = "select * buildings, boss boss.codbo=buildings.codbu"; $rows = array(); if (!db::iserror($tab = $db->query($sql))) { while ($row = $tab->fetchrow(db_fetchmode_object)) { $rows[] = $row; } } else { die("errorsql:" . $tab->getmessage()); } return $rows; } ?> but working in mysql had lot of changes. in fact there error in !bd::iserror. had change connection.php in one
<?php session_start(); require_once("db.php"); $db = db::connect("pgsql:etc...."); if (db::iserror($db)) { die("error: " . $db->getmessage()); } ?> to one
<?php $servername = "localhost"; $username = "test"; $password = ""; $dbname = "test"; // create connection $db = new mysqli($servername, $username, $password, $dbname); // check connection if ($db->connect_error) { die("connection failed: " . $db->connect_error); } $a = mysqli_report(mysqli_report_error | mysqli_report_strict); echo "connected " . $a . " "; ?> in order show result of query code had become this:
<body> <?php $rows = getboss(); if (count($rows) == 0) { print "<h2>no boss here</h2>"; } else { foreach ($rows $row) { print "<p>" . $row['name'] . "</p>"; print "<p>" . $row['surname'] . "</p>"; } } ?> <?php function getboss() { global $db; $sql = "select * test_boss order name, surname"; $rows = array(); if ($tab = $db->query($sql)) { while ($row = $tab->fetch_array()) { $rows[] = $row; } return $elenco; } else { die("errore sql: " . $tab->getmessage() . ":" . $sql); } } ?> </body> </html> that in facts works pretty well, had change fetch_row fetch_array. not use anymore method of posting data $rows value.
<?= $row->name ?> because there error
notice: trying property of non-object
but had use
print "<p>".$row['name']."</p>"; what can in order use method fetch_row corretly?
you can type cast array object tweaking code slight.
if want retrieve object in form of $row->name code be.
while ($row= $tab->fetch_array()) { $rows[] = (object)$row; } or
while ($row= $tab->fetch_object()) { $rows[] = $row; }
Comments
Post a Comment