无法将正确的结果输出到变量

时间:2014-09-06 00:36:56

标签: php mysql

现在我试图通过mysql查询将结果附加到php中的变量,这是使用的代码:

<?php
$con=mysqli_connect("localhost","root","","ezsnip");
// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysql_query("SELECT Snip FROM snips WHERE ID='1'");

$row = mysql_fetch_array($result);

$snip = $row['Snip'];
$snip = str_replace('<?php', '&lt;?php', $snip);
$snip = str_replace('?>', '?&gt;', $snip);
?>
    <div id="editor"><?php echo $snip; ?></div>

当我运行代码时,它返回:mysql_fetch_array()期望参数1为资源,布局在 C:\ xampp \ htdocs \ home.php 中给出 170

第170行是:

$row = mysql_fetch_array($result);

1 个答案:

答案 0 :(得分:0)

创建mysql_query资源

后,您正在进行mysqli
<?php
$con=mysqli_connect("localhost","root","","ezsnip");
// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"SELECT Snip FROM snips WHERE ID='1'");

$row = mysqli_fetch_array($result);

$snip = $row['Snip'];
$snip = str_replace('<?php', '&lt;?php', $snip);
$snip = str_replace('?>', '?&gt;', $snip);
?>
    <div id="editor"><?php echo $snip; ?></div>