为什么我会收到这些错误? php和mysql

时间:2016-01-09 05:21:20

标签: php mysql mysqli

错误代码:

  

注意:未定义的变量:blog_array in   第69行/home/willconnor/public_html/index.php

*创建博客数组* /$blog_array = array();

<?php

    if(sizeof($blog_array) > 0)
    {
        /*** loop over the blog array and display blogs ***/
        foreach($blog_array as $blog)
        {
            echo '<div class="blog_entry">';
            echo '<p><span class="category">'.$blog['blog_category_name'].': </span>
            <span class="blog_date">Added by '.$blog['blog_user_name'].' on '.$blog['blog_content_date'].'</p>';
            echo '<h2>'.$blog['blog_content_headline'].'</h2>';
            echo '<p>'.$blog['blog_content_text'].'</p>';
            echo '</div>';
        }
    }
    else
    {
        echo 'No Blogs Here';
    }

    /*** include the footer file ***/
    include 'includes/footer.php';

?>

错误代码:

  

警告:mysqli_select_db()期望参数1为mysqli,第16行/home/willconnor/public_html/includes/conn.php中给出的字符串

<?php

/*** mysqli hostname ***/
$hostname = 'localhost';

/*** mysqli username ***/
$username = 'username';

/*** mysqli password ***/
$password = 'password';

/*** connect to the database ***/
$link = mysqli_connect($hostname, $username, $password);

/*** select the database ***/
$db = mysqli_select_db('blog', $link);

?>

4 个答案:

答案 0 :(得分:0)

尝试如下

  /*** mysqli hostname ***/
  $hostname = 'localhost';

 /*** mysqli username ***/
 $username = 'username';

 /*** mysqli password ***/
 $password = 'password';

$con=mysqli_connect($hostname,$username,$password,"blog");

if (mysqli_connect_errno())
{
   echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

如果您想更改所选数据库“博客”,那么只需要编写以下行

mysqli_select_db($con,"replace_selected_db");

答案 1 :(得分:0)

实际上你可以将数据库的名称作为mysqli_connect()函数中的第四个参数传递,如下所示:

<?php
   $hostname = 'localhost';
   $username = 'username';
   $password = 'password';
   $dbname = 'blog';
   $link = mysqli_connect($hostname, $username, $password, $dbname);

   if (!$link) {
      die("Connection failed: " . mysqli_connect_error();
   }
?>

答案 2 :(得分:0)

您的连接应该像:

<?php

/*** mysqli hostname ***/
$hostname = 'localhost';

/*** mysqli username ***/
$username = 'username';

/*** mysqli password ***/
$password = 'password';

/*** database ***/
$my_db = 'blog';

/*** connect to the mysql and select database ***/
$link = mysqli_connect($hostname, $username, $password, $my_db);

/*** Check connection ***/ 
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

?> 

在此之后,您可以检查$blog_array

希望这有帮助!

答案 3 :(得分:0)

您需要使用连接参数($ link)作为mysqli_select_db()函数的第一个参数。只需用下面的代码替换。

<?php

/*** mysqli hostname ***/
$hostname = 'localhost';

/*** mysqli username ***/
$username = 'username';

/*** mysqli password ***/
$password = 'password';

/*** connect to the database ***/
$link = mysqli_connect($hostname, $username, $password);

/*** select the database ***/
$db = mysqli_select_db($link, 'blog');

?>