SQL: Query Not Working

Joseph Longo
Joseph Longo used Ask the Experts™
on
Hello,

For whatever reason, I cannot get this query to work:
<?php
	$sql = "SELECT * FROM states ORDER BY state ASC";
	$result = mysql_query($db_connect, $sql);
	if (mysql_num_rows($result) > 0) {
	while($row = mysql_fetch_assoc($result)){
?>
	<option value="<?php echo  $row["state"]; ?>" ><?php echo $row["state"]; ?></option>
<?php
	}
	}
?>
</select>

Open in new window


Any ideas as to why?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Mukesh YadavFull Stack Developer

Commented:
Are you getting any error?
HuaMin ChenProblem resolver

Commented:
Hi,
Does the table exist? Is the connection working fine? Please remove ASC in your query.
Most Valuable Expert 2011
Top Expert 2016
Commented:
Please refer to this article for two important points.
https://www.experts-exchange.com/articles/11177/PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html

First, it will show you how to visualize error information.  When database extensions fail, they fail silently, and you must use PHP to tell them to give you the error information.  This should become part of your standard coding practice.  Look through the article code samples for the exact phrase, "LOG AND SHOW THE ERROR" to see how to test for failure and get the error messages.

Second, it will show you how to get off of the MySQL extension and make the required move to MySQLi or PDO.  There is no current support for MySQL in PHP -- it has been removed.  You must upgrade  your script in order to keep it running!

You might want to add error_reporting(E_ALL) to the top of your PHP scripts, too.  This will help you catch situations that rely on undefined variables.

Author

Commented:
Apparently, for whatever reason the query doesn't work on my computer at home. I have an old MacBook Pro ---like 11 years old, which is running an older version of PHP. However, once I upload my scripts from my older machine to a newer machine and use mysqli, the script works as is. So, no worries. Thanks everyone for their assistance.
Most Valuable Expert 2011
Top Expert 2016

Commented:
I would love to have an old MacBook Pro!

Glad things are looking up.   Best of luck with your project, ~Ray

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial