Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6646615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:26:17+00:00 2026-05-26T00:26:17+00:00

I have finally graduated from MS Access to MySQL. Unfortunately, I can’t even get

  • 0

I have finally graduated from MS Access to MySQL. Unfortunately, I can’t even get a query right. Can someone please tell me what’s wrong? I keep getting the ‘invalid query’ message that I specified in the php.

<?php
@ $db = mysql_connect('localhost', 'root', 'root', 'newdatabase');
if (!$db) {
    die ('Failed to connect to the database.');
}


$query = "SELECT first FROM demographics";
$result = mysql_query($query);

if (!$result) {
    die('invalid query');
}

while ($row = mysql_fetch_assoc($result)) {
    echo $row['first'];
}

?>

Also, the book I am reading tells me to use:

new mysqli('localhost', 'root', 'root', 'newdatabase')

to connect as opposed to the

mysql_connect

I used in the above code. I haven’t been able to connect to the db with new mysqli. Does it matter which one I use?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T00:26:17+00:00Added an answer on May 26, 2026 at 12:26 am

    The fourth parameter to mysql_connect() is not the database name. It is a boolean specifying whether or not to establish an additional new connection or use an existing one. Most often, it is omitted. Use mysql_select_db() to choose the database. Since you have not selected a database in your code, your query is likely failing.

    $db = mysql_connect('localhost', 'root', 'root');
    if (!$db) echo mysql_error();
    else mysql_select_db("database");
    

    Note I have removed the @ from the mysql_connect() call. @ suppresses errors, and was probably preventing you from seeing what was going wrong with your connection.

    When testing the success or failure of a query, don’t die(). Instead echo out mysql_error() while developing your application. In production, you can replace the echo by writing to error_log() instead.

    if (!$result) {
      echo mysql_error();
    }
    

    As for using MySQLi instead of the basic mysql_* functions, MySQLi can be used in an object oriented fashion and also offers prepared statements, which can be both more efficient to use and more secure. As mentioned in comments above, PDO is often recommended as the most flexible API for interacting with an RDBMS from PHP.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have finally worked out how to get stdin and stdout to pipe between
I have finally managed to get javan-whenever gem working on my site5 server, and
I have finally ventured away from the land of PowerShell to C# stuff for
I'm new to C++, and have finally given up on trying to get this
I have finally migrated our CVS repository to git. Unfortunately we didn't use branches
I have finally managed to get core data working and beginning to understand it.
I have finally been able to get my app updated with constraints and Autolayout.
I have finally stumbled across a problem that I can't already find the answer
I have finally started upgrading my Rails apps from 2.3.8 to 3.1.0. I was
I have finally switched from % to the .format() string formatting operator in my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.