-1
我想重写我的代码从SQL到SQli,但我似乎无法弄清楚如何重写我使用的下面一段代码。用户点击一个更新链接,将它们带到一个表单中,然后使用下面的php获取要更新的字段的id,连接到数据库并找到id,然后将该id传递给php提交代码。如何从sql重写代码到mysqli?
<?php
// check if the 'id' variable is set in URL, and check that it is valid
if (isset($_GET['cd']) && is_numeric($_GET['cd']))
// get id value
$id = $_GET['cd'];
$results = $id;
$username = "some username";
$password = "some password";
$hostname = "localhost";
$databasename = "some database";
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
@mysqli_select_db($databasename,$dbhandle);
//select a database to work with
$selected = mysql_select_db("mydeos_lep",$dbhandle)
or die("Could not select mydeos_lep");
//execute the SQL query and return records
$result = mysql_query("SELECT * FROM `lep` WHERE id='$id'");
$num=mysql_numrows($result);
mysql_close();
// see if any rows were returned
if (mysql_num_rows($result) > 0) {
// print them one after another
echo "<table cellpadding=10 cellspacing=3>";
while($row = mysql_fetch_row($result)) {
echo '<td><a><input type="submit" formaction="update.php?id=' . $row['0'] . '"></span></a></td>';
echo "</tr>";
}
echo "</table>";
}
?>
奏效最终代码
<?php
// check if the 'id' variable is set in URL, and check that it is valid
if (isset($_GET['cd']) && is_numeric($_GET['cd']))
// get id value
$id = $_GET['cd'];
$results = $id;
//database connection and selecting database
$con = new MySQLi('localhost', 'some username', 'some password', 'some database');
//running query
$result = $con->query("SELECT * FROM `lep` WHERE id='$id'");
// getting number of rows
$num = $result->num_rows;
if($num > 0){
// print them one after another
echo "<table cellpadding=10 cellspacing=3>";
while($row = $result->fetch_array(MYSQLI_NUM)){
echo '<td><a><input type="submit" formaction="update.php?id=' . $row['0'] . '"></span></a></td>';
echo "</tr>";
}
echo "</table>";
}
?>
您是否尝试过只是做一个查找和'mysql'替换 - > 'mysqli'?我认为所有这些功能都可以在'mysqli' – HPierce
@YourCommonSense这篇文章无关重复,你的重复标记是一个错误。 – peterh