简体   繁体   English

为什么会出现这些错误? PHP和MySQL

[英]Why am I getting these errors? php and mysql

ERROR CODE: 错误代码:

Notice: Undefined variable: blog_array in /home/willconnor/public_html/index.php on line 69 注意:未定义的变量:第69行/home/willconnor/public_html/index.php中的blog_array

* create the blog array * /$blog_array = array(); *创建博客数组* /$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';

?>

ERROR CODE: 错误代码:

Warning: mysqli_select_db() expects parameter 1 to be mysqli, string given in /home/willconnor/public_html/includes/conn.php on line 16 警告: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);

?>

try as follows 尝试如下

  /*** 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();
}

and if you want to change the selected database "blog" then only you need to write following line 如果要更改选定的数据库“博客”,则只需编写以下行

mysqli_select_db($con,"replace_selected_db");

Actually you could pass the name of the database as the fourth parameter in mysqli_connect() function, like this: 实际上,您可以将数据库的名称作为第四个参数传递给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();
   }
?>

You connection should code like: 您的连接应如下所示:

<?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();
}

?> 

After this you can check for your $blog_array . 之后,您可以检查$blog_array

Hope this help! 希望对您有所帮助!

You need to use connection parameter($link) as first parameter for mysqli_select_db() function. 您需要使用连接参数($ link)作为mysqli_select_db()函数的第一个参数。 Just replace your code with below. 只需用下面的代码替换即可。

<?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');

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM