开发者

Database connection issue

开发者 https://www.devze.com 2023-03-22 04:33 出处:网络
The following query is not executing and I am stumped as to why. The query is returning my die clause of \'error querying database\' yet all of my info is exact.

The following query is not executing and I am stumped as to why. The query is returning my die clause of 'error querying database' yet all of my info is exact.

Have I g开发者_如何学运维ot a syntax error in my query variable?

<?php
$db_host     = 'localhost';
$db_user     = 'root';
$db_pass     = 'root';
$db_database = 'bbg_db_2'; 

$dbc = mysql_connect($db_host,$db_user,$db_pass) or die('Unable to establish a database connection');

$query = "SELECT category_name, category_desc FROM categories";
$result = mysqli_query($dbc, $query) or die ('error querying database');

while ($row = mysqli_fetch_array($result)) {

    $catname = $row['category_name'];
    $catdesc = $row['category_desc'];

    echo "<li>$catname</br><span>$catdesc</span></li>";
}

mysqli_close($dbc);
?>


You have missed mysql_select_db($db_database) in your code.


mysql_connect should be mysqli_connect, cause you use mysqli_query after that and use mysqli_select_db to select database


Two problems:

  • You connected with the mysql API, but then tried to query with the mysqli API;
  • You never selected the database with mysqli_select_db.

Performing some basic error checking with mysqli_error($dbc) would have revealed this to you pretty quickly.


Also, as a style note, ARGH tabs inside lines! Bad! Wrong! Tabs are for indentation, not alignment.


Try:

... or die(mysqli_error());

instead, which will output the exact reason the query's failing.


You are mixing up mysql and mysqli. You will need to pick one.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号