/ / Verwendung von mysqli anstelle von mysql Zugriff auf die DB - mysql, phpmyadmin

Verwenden von mysqli anstelle von mysql Zugriff auf den DB - mysql, phpmyadmin

    please find below a script in php that that fetch data from my database company, the database is in phpMyadmin

Ich verstehe, dass es sichere Wege gibt, Daten abzurufen. Meine Frage ist Kann ich den folgenden Code verbessern, indem Sie mysqli anstelle von mysql_query verwenden.

    <?php
get results from database. Can I improve the code below using mysqli to fetch instead of mysql_query in the following line?.

$result = mysql_query("SELECT * FROM customer")
or die(mysql_error());
tables begins here with the customer id and customer name
echo "<table >";
echo "<tr>
<th>ID</th>   the customer id
<th>Name</th> the customer name
</tr>";

// loop through results of database query, displaying them in the table
the table below display the columns in html using mysql fetch array. all data is displayed.

while($row = mysql_fetch_array( $result )) {

// echo out the contents of each row into a table
my purpose is embed the php in html in another way using msqli
echo "<tr>";
echo "<td>" . $row["CustomerID"]."</td>";
echo "<td>" . $row["Name"]. "</td>";
echo "</tr>";
}
echo "</table>";
?>
my purpose is embed the php in html in another way using msqli

Antworten:

0 für die Antwort № 1

Ersetzen Sie einfach alle mysql mit mysqli

      $result = mysqli_query("SELECT * FROM customer");

while($row = mysqli_fetch_array( $result )) {

Wenn du es einfach halten willst

Oder mach es so ...

  /*Your connection info goes here...*/
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

if (mysqli_connect_errno()) {
printf("Connect failed: %sn", mysqli_connect_error());
exit();
}

$query = "SELECT * FROM customer";

if ($result = $mysqli->query($query)) {

echo "<table ><tr><th>ID</th><th>Name</th></tr>";

/* fetch results by row */
while ($row = $result->fetch_row()) {
echo "<tr><td>" . $row["CustomerID"]."</td><td>" . $row["Name"]. "</td></tr>";
}

/* free the result set */
$result->close();
}

/* close the connection */
$mysqli->close();