最后尝试将mysql转换为mysqli,当我这样做时,我得到了一堆错误。这就是我没有转换的原因。以下是我的一些错误。另外,我正在按照PHP手册来帮助我进行转换,现在它还没有做好。
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\wamp\www\pyrostudio\checklogin.php on line 16
Undefined index: myusername in C:\wamp\www\pyrostudio\checklogin.php on line 19
mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in C:\wamp\www\pyrostudio\checklogin.php on line 32
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="PyroServer"; // Database name
$tbl_name="members"; // Table name
$tbl_info="info"; // Table name
// Connect to server and select databse.
$db = mysqli_connect("$host", "$username", "$password","$db_name")or die("cannot connect");
//news
$sql="SELECT * FROM $tbl_name";
$result=mysqli_query($db,$sql);
$rows=mysqli_fetch_array($result);
// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysqli_query($sql,$sql);
// Mysql_num_row is counting table row
$count=mysqli_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
// Register $myusername, $mypassword and redirect to file "home.php"
session_start();
$rows = mysqli_fetch_array($result);
$_SESSION['UserID'] = $rows["id"];
$_SESSION['UserUsername'] = $rows["username"];
header("location:home.php");
}
else {
echo '
<table border="0" width="100%" cellspacing="0" cellpadding="3">
<tr>
<td bgcolor="#FFFFCC"><h1 align="center" class="sansserif">PyroStudio</h1></td>
<td bgcolor="#FFFFCC" align="right"> </td>
</tr>
</table><br />
<head>
<div class="SystemAlert">
<div id="ctl00_cphBanner_SystemAlertTextColor" class="SystemAlertText" style="background-color:orange;">
<div class="Exclamation">
</div>
<div id="ctl00_cphBanner_LabelAnnouncement" align="center"><b>Site Status: </b><b><i >You are now able to puchase the site currency called `$PyroCoins$`. Check it out!</i></b></div>
</div>
</div><br />
<title>Welcome to PyroStudio - Log in, sign up or learn more</title>
<style type="text/css">
h1.sansserif{font-family:Arial,Helvetica,sans-serif;}
td.username{font-family:Arial,Helvetica,sans-serif;}
td.password{font-family:Arial,Helvetica,sans-serif;}
td.login{font-family:Arial,Helvetica,sans-serif;}
div.analcorm{color: white;}
body{
background: grey;
margin: 15px;
}
h1.sanserif{
font-family:Arial,Helvetica,sans-serif;
}
div#menubar1{
padding: 24px;
}
div#menubar1>a{
font-family:Arial,Helvetica,sans-serif;
font-size: 17px;
background: #333;
padding: 12px 15px;
color: #999;
margin-right: 10px;
text-decoration: none;
border-radius: 3px;
transition: background 0.5s linear 0s, color 05s linear 0s;
}
div#menubar1>a:hover{
background: #6F8A00;
color: #FFF;
}
strong#pagination>a{
font-family:Arial,Helvetica,sans-serif;
font-size: 12px;
background: #333;
padding: 10px 10px;
color: #999;
margin-right: 10px;
text-decoration: none;
border-radius: 3px;
transition: background 0.5s linear 0s, color 05s linear 0s;
}
strong#pagination>a:hover{
background: #6F8A00;
color: #FFF;
}
mainbutton#mainB>a{
font-family:Arial,Helvetica,sans-serif;
font-size: 17px;
background: #333;
padding: 10px 10px;
color: #999;
margin-right: 10px;
text-decoration: none;
border-radius: 3px;
transition: background 0.5s linear 0s, color 05s linear 0s;
}
mainbutton#mainB>a:hover{
background: #6F8A00;
color: #FFF;
}
div#footer>a{
font-family:Arial,Helvetica,sans-serif;
font-size: 10px;
background: #333;
padding: 10px 12px;
color: #999;
margin-right: 10px;
text-decoration: none;
border-radius: 3px;
transition: background 0.5s linear 0s, color 05s linear 0s;
}
div#footer>a:hover{
background: #6F8A00;
color: #FFF;
}
a.Register{
font-family:Arial,Helvetica,sans-serif;
font-size: 12px;
background: #333;
padding: 1px;
color: #999;
margin-right: 10px;
text-decoration: none;
border-radius: 10px;
transition: background 0.5s linear 0s, color 05s linear 0s;
}
a.Register:hover{
background: #6F8A00;
color: #FFF;
}
a.Admin{
font-family:Arial,Helvetica,sans-serif;
font-size: 12px;
background: #333;
padding: 1px;
color: #999;
margin-right: 10px;
text-decoration: none;
border-radius: 10px;
transition: background 0.5s linear 0s, color 05s linear 0s;
}
a.Admin:hover{
background: #6F8A00;
color: #FFF;
}
gorilla.CopyRight{
font-family:Arial,Helvetica,sans-serif;
background: #333;
color: #999;
}
</style>
<link rel="icon" type="image/ico" href="css/favico.ico" />
<td colspan="3">
<table cellpadding="1" align="center" bgcolor="#cccc99"><tr><td>
<table cellpadding="2" bgcolor="#FFFFCC"><tr><td>Invalid username or password. Please re-enter your user information. </td></tr></table>
</td></tr></table>
</td><br />
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#708090">
<tr>
<!-- <i>Login Closed Due To Maintenance</i> -->
<form name="form1" method="post" action="checklogin.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#778899">
<tr>
<td class="login" colspan="3"><strong>Member Login </strong></td>
</tr>
<tr>
<td class="username" width="78">Username</td>
<td width="6">:</td>
<td width="294"><input name="myusername" type="text" id="myusername"></td>
</tr>
<tr>
<td class="password">Password</td>
<td>:</td>
<td><input name="mypassword" type="password" id="mypassword"></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" name="Submit" value="Login"> <strong>|</strong> <a class="Register" href="register.php">Register<a><strong>|</strong> <a class="Admin" href="admin/index.php">Admin</a></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
';
echo '
<html>
<h1 align="center">News Updates</h1>
<hr>
';
$db = mysqli_connect("$host", "$username", "$password","$db_name")or die("cannot connect");
$per_page = 4;
$pages_query = mysqli_query($db,"SELECT COUNT(`id`) FROM `news` ORDER BY id DESC");
$pages = ceil(mysql_result($pages_query, 0) / $per_page);
$page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
$start = ($page - 1) * $per_page;
$query = mysqli_query($db,"SELECT `title`,`body`,`user`,`date` FROM `news` ORDER BY id DESC LIMIT $start, $per_page");
while($query_row = mysqli_fetch_assoc($query))
{
echo '<b>', $query_row['title'] ,'</b><br />';
echo '<br />';
echo '', nl2br($query_row['body']) ,'<br />';
echo '<br />';
echo 'Posted By: <b>', $query_row['user'] ,'</b>';
echo '<br />';
echo 'Posted On: <b>', $query_row['date'] ,'</b>';
//echo nl2br($query_row['body']);
echo "<hr>
";
}
if($pages >= 1 && $page <= $pages)
{
for($x=1; $x<=$pages; $x++)
{
echo ($x == $page) ? '<strong><a href="?page='.$x.'">'.$x.'</a></strong> ' : '<a href="?page='.$x.'">'.$x.'</a>';
}
}
}
echo '
<hr>
</body>
</form>
<div id="footer" align="center"><gorilla class="CopyRight">PyroStudio © 2015</gorilla> | <a href="Privacy.php">Privacy Policy</a> | <a href="Contact.php">Contact Us</a> | <a href="About.php">About Us</a> | <a href="TaC.php">Terms and Conditions</a> | <a href="#">Activate SSL</a> | <a href="AboutSSL.php">About SSL</a></div>
</html>
';
?>
答案 0 :(得分:0)
$result=mysqli_query($db,$sql) or
die("Insert failed: " . mysqli_error($db));
试试这个,告诉我们结果
答案 1 :(得分:0)
没有变量的测试:
$sql="SELECT * FROM members";
并使用mysqli_real_escape_string()
和mysql中的i