Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.
Moderator: General Moderators
vigge89
Forum Regular
Posts: 875 Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden
Post
by vigge89 » Tue Dec 23, 2003 11:50 am
How come this doesn not work?:
Code: Select all
<?php
$conn = @mysql_connect('localhost', 'user', 'password') or die("Couldn't connect to MySQL database, error returned: ".mysql_error());
$db = @mysql_select_db('vigge') or die("Couldn't select MySQL, error returned: ".mysql_error());
$query = mysql_query("SELECT ´name´ FROM ´one´ WHERE id = ´2´", $db);
// ive tried w/o " ´ "'s & with " ' "'s, but i can't get it to work
echo $query;
?>
I'm a complete newbie at mySQL, so dont kill me or something
I get "Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in C:\xampp\htdocs\test\mysql\intro.php on line 7"
microthick
Forum Regular
Posts: 543 Joined: Wed Sep 24, 2003 2:15 pm
Location: Vancouver, BC
Post
by microthick » Tue Dec 23, 2003 11:54 am
You can't just echo out the contents of $query, because it's not a simple object like a string.
If you know you are just gonna return one record from the database, you could employ this method:
$query = mysql_query("SELECT ´name´ FROM ´one´ WHERE id = ´2´", $db);
// ive tried w/o " ´ "'s & with " ' "'s, but i can't get it to work
$row = mysql_fetch_array($query);
echo $row["name"];
vigge89
Forum Regular
Posts: 875 Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden
Post
by vigge89 » Tue Dec 23, 2003 11:58 am
im getting
Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in C:\xampp\htdocs\test\mysql\intro.php on line 7
&
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\test\mysql\intro.php on line 9
errors, what is wrong in the query?
$query = mysql_query("SELECT name FROM one WHERE id = '2'", $db);
microthick
Forum Regular
Posts: 543 Joined: Wed Sep 24, 2003 2:15 pm
Location: Vancouver, BC
Post
by microthick » Tue Dec 23, 2003 12:40 pm
vigge,
Have you tried removing the , $db from the $query line, so that just the query itself is contained within the brackets?
$query = mysql_query("SELECT ´name´ FROM ´one´ WHERE id = ´2´");
vigge89
Forum Regular
Posts: 875 Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden
Post
by vigge89 » Tue Dec 23, 2003 12:56 pm
i did, and now i got error saying;
"Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\test\mysql\intro.php on line 9"
microthick
Forum Regular
Posts: 543 Joined: Wed Sep 24, 2003 2:15 pm
Location: Vancouver, BC
Post
by microthick » Tue Dec 23, 2003 1:01 pm
So there exists columns name and id in your table called one, right?
Is id a VARCHAR or an INT?
Does there even exist a record in the table where id is 2?
If id is an INT, try removing the quotes around the number 2.
(I also personally don't quote column names or table names.)
$query = mysql_query("SELECT name FROM one WHERE id = 2");
vigge89
Forum Regular
Posts: 875 Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden
Post
by vigge89 » Tue Dec 23, 2003 1:05 pm
1: yep it does
2: id
3: yes, there are two records, 1 & 2
4: ill try now
edit: still no result
microthick
Forum Regular
Posts: 543 Joined: Wed Sep 24, 2003 2:15 pm
Location: Vancouver, BC
Post
by microthick » Tue Dec 23, 2003 1:13 pm
vigge,
Let's just try and narrow down what's wrong by doing a very vague query first, ok? This will tell us if it's the query that's wrong, or the connection to the database.
Change the query to this:
$query = mysql_query("SELECT * FROM one");
then below it, comment out the mysql_fetch_array line, etc and replace that temporarily with
echo "Our SELECT statement returned ".mysql_num_rows($query)." records";
Does this generate any errors?
microthick
Forum Regular
Posts: 543 Joined: Wed Sep 24, 2003 2:15 pm
Location: Vancouver, BC
Post
by microthick » Tue Dec 23, 2003 1:22 pm
Also, when I connect to my database, I typically state the connection link as well.
$db = @mysql_select_db('vigge', $conn) or die("Couldn't select MySQL, error returned: ".mysql_error());
vigge89
Forum Regular
Posts: 875 Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden
Post
by vigge89 » Tue Dec 23, 2003 1:51 pm
still won't work :/
microthick
Forum Regular
Posts: 543 Joined: Wed Sep 24, 2003 2:15 pm
Location: Vancouver, BC
Post
by microthick » Tue Dec 23, 2003 1:57 pm
Remove the @ signs from the first two lines. The @ signs block any error messages from showing.
vigge89
Forum Regular
Posts: 875 Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden
Post
by vigge89 » Tue Dec 23, 2003 2:01 pm
I get the same error, nothing else
Draco_03
Forum Regular
Posts: 577 Joined: Fri Aug 15, 2003 12:25 pm
Location: Montreal, Canada
Post
by Draco_03 » Tue Dec 23, 2003 2:01 pm
just to test try it this way
if you have an error tell me what it is...
Code: Select all
$host = "yourhost"
$user = "user"
$pswd = "password"
$connect = mysql_connect($host, $user, $pswd)
or die("Could not connect: " . mysql_error());
$database = mysql_select_db('name_of_database')
or die(MySQL_Error());
$sql = "SELECT * FROM table";
$result = mysql_query($sql) or die(MySQL_Error());
vigge89
Forum Regular
Posts: 875 Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden
Post
by vigge89 » Tue Dec 23, 2003 2:05 pm
Code: Select all
<?php
$host = "localhost";
$user = "user";
$pswd = "pass";
$connect = mysql_connect($host, $user, $pswd)
or die("Could not connect: " . mysql_error());
$database = mysql_select_db('test')
or die(MySQL_Error());
$sql = "SELECT * FROM one";
$result = mysql_query($sql) or die(MySQL_Error());
echo $result;
?>
outputs (no error): Resource id #3
Draco_03
Forum Regular
Posts: 577 Joined: Fri Aug 15, 2003 12:25 pm
Location: Montreal, Canada
Post
by Draco_03 » Tue Dec 23, 2003 2:10 pm
you can't echo $result...
instead add this at the end
Code: Select all
$rows = mysql_fetch_row($result);
echo "$rows[0]";
you might want to look into
this topic