0

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 got 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);
?>
2
  • 1
    Post the output of mysqli_error() after the failed query. It should tell you exactly what's going wrong. Commented Jul 20, 2011 at 15:02
  • Why is your code halfway across my screen? Commented Jul 20, 2011 at 15:03

5 Answers 5

4

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

Sign up to request clarification or add additional context in comments.

Comments

4

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

Comments

3

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.

6 Comments

Thankyou so much. I will stop with the tabs :o)
Sorry to bother you but I have implemented mysqli_error($dbc) which revealed that I hadn't selected the database and so I altered my code accordingly, however now I still do not get any results... no error message and nothing visible within my page. Am I missing something?
@Richard: My first bullet point, perhaps. Or maybe there are just no results.
Thanks for getting back to me. I have amended the API to mysql which has not solved the problem. I can't see how there can be no results as I have a populated table in place. Im stuck. Probably some schoolboy error... I'll keep playing. Thanks anyway :o)
@Richard: At the risk of encouraging you to write a new question where you can't reproduce the issue for us to diagnose, perhaps give that a go anyway.
|
1

Try:

... or die(mysqli_error());

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.