Forum Moderators: coopster

Message Too Old, No Replies

PHP Query in MySQLi

converting to MySQLi

         

actolearn

2:29 am on Apr 23, 2016 (gmt 0)

10+ Year Member



I am converting to MySQLi. I've created a new DB and table to experiment with. My log in does work as I was able to update records in my DB.

I'm having trouble with my query though. I'm hoping I can use the same type of query as I did before with just a few changes but so far not working.

//select items from db
$items = mysqli_query ("SELECT myNo, pgN, prNm, amount, current FROM tableName")

//extract every item
while($item = mysqli_fetch_array($items))
{
?>

<!-- start -->
<?php echo ($item["myNo"]); ?><br>
<?php echo ($item["pgN"]); ?><br>
<?php echo ($item["prNm"]); ?><br>
<?php echo ($item["amount"]); ?><br>
<?php echo ($item["current"]); ?><br>
<!-- end -->

<?php
}

?>

bhukkel

6:42 am on Apr 23, 2016 (gmt 0)

10+ Year Member



You are missing the link parameter in your mysqli_query statement.

It has to be something like this:

$items = mysqli_query ($link, "SELECT myNo, pgN, prNm, amount, current FROM tableName")

actolearn

1:03 pm on Apr 23, 2016 (gmt 0)

10+ Year Member



I tried your $link suggestion but it didn't work. I also tried $con and that didn't work either.

actolearn

1:14 pm on Apr 23, 2016 (gmt 0)

10+ Year Member



This is what is in my error log:
PHP Parse error: syntax error, unexpected T_WHILE in

actolearn

4:51 pm on Apr 23, 2016 (gmt 0)

10+ Year Member



Ok ~ moving slowly along. Below works but there is so much sqli code out there I can't tell if this is a good way to convert things over to MySQLi or a bad way.

I like to use "item" instead of "row" and I'm trying to keep things as close to my old MySQL as possible but I need opinions. I'll incorporate my styling in there once I'm more organized...

<?php
//select items from db
$sqli = "SELECT * FROM tableNme";
$result = $conn->query($sqli);

if ($result->num_rows > 0) {
// output data of each item
while($item = $result->fetch_assoc()) {
?>

<!-- start -->
<?php echo ($item["field1"]); ?><br>
<?php echo ($item["field2"]); ?><br>
<?php echo ($item["field3"]); ?><br><br>
<!-- end -->

<?php
}
} else {
echo "0 results";
}

$conn->close();
?>

Thank you - AC