MySQL_Query Issue

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
PulpJuice
Forum Newbie
Posts: 2
Joined: Tue Apr 19, 2011 3:04 pm

MySQL_Query Issue

Post by PulpJuice »

Firstly, I apologize for my newbness.

Alright, haha, so I've been hammering at some basic stuff and I'm just simply at a loss...

<?php
$con = mysql_connect("localhost", "X", "X");
$db = "X";
$conn = mysql_select_db($db, $con);
$sql = 'SELECT * FROM users';
$userlist = mysql_query($sql, $conn);
echo $userlist;
mysql_close($con);
?>

I get this

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/pulpjuic/public_html/learning/index.php on line 14

I realize there's a lot of junk, it's the result of brute force when tutorial examples produced the same problem as the warning or fatal errors, etc. Before I had issues with database select

Your help is appreciated :drunk:
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: MySQL_Query Issue

Post by requinix »

mysql_select_db() only returns whether it was successful or not. It's not the MySQL connection resource that $con is.

Keep using $con and disregard $conn (except for telling whether the call was successful, of course).
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: MySQL_Query Issue

Post by califdon »

Requinix is precisely correct. But let me suggest that you take this initial opportunity for you to learn mysqli ("improved mysql") rather than mysql. If you first learn mysql, you will be tempted to not trouble to learn mysqli, and that would be a mistake. If you begin by learning mysqli, it will be no harder to learn than mysql, but mysqli is strongly recommended.
PulpJuice
Forum Newbie
Posts: 2
Joined: Tue Apr 19, 2011 3:04 pm

Re: MySQL_Query Issue

Post by PulpJuice »

@Requinix
Thank you, that was extremely helpful :)

@Califdon
I will certainly look into that. Thank you for pointing me in the right direction (y)
temidayo
Forum Contributor
Posts: 109
Joined: Fri May 23, 2008 6:17 am
Location: Nigeria

Re: MySQL_Query Issue

Post by temidayo »

if you have a successful query, the following line

Code: Select all

echo $userlist;
will not work. You cannot just echo the result of a query!
Post Reply