Jump to content

mysql_query, non error?


zimmy52420

Recommended Posts

I know absolutely NOTHING about MySQL, other than the fact it is a database, and I'm using PHP to connect to it, it could be that I'm a complete idiot, (Could be, I am!) and this is what mysql_query's supposed to produce, or it could be that I'm still a complete idiot, and there's a problem with my code

<?phpmysql_connect('host', 'user', 'pass') or die( "Error #1 - Couldn't connect to Server");mysql_select_db('db') or die( "Error #2 - Couldn't connect to Database");$query="SELECT * FROM table";$result=mysql_query($query) or die( "Error #3 - Couldn't collect Table Data");mysql_close;echo $result;?>

Obviously, I've done the obvious and set host user pass etc. etc. But whenever I run the script I get the following output.Resource id #3This doesn't seem to be an error to me, due to the 'die' bits, and it looks nothing like a PHP default error to me, now someone can come along and point out what I've done wrong. ;pThanks in advance

Link to comment
Share on other sites

Its up to you but instead of using a die() with your own message you should use mysql_error() so you know a better detail of what the problem is something like this

<?php$con = mysql_connect("localhost", "username", "password");if(!$con){ die( mysql_error() ); }mysql_select_db("the_db", $con);$result = mysql_query("SELECT * FROM `no_value`", $con) or die( mysql_error() );while($row = mysql_fetch_array($result)){$var = $row['column_1'];echo $var;}mysql_close($con);?>

Link to comment
Share on other sites

MySQL query returns a resource ID which you can pass into other mysql functions like mysql_num_rows and mysql_fetch_assoc. also, mysql_close is a function, and you need to specify a variable. kinda like what mihalism posted, its just a safety practice just in case your server and MySQL are weird like that.Other than that, good job.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...