Page 1 of 2
beginner @ mySQL
Posted: Tue Dec 23, 2003 11:50 am
by vigge89
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"
Posted: Tue Dec 23, 2003 11:54 am
by microthick
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"];
Posted: Tue Dec 23, 2003 11:58 am
by vigge89
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);
Posted: Tue Dec 23, 2003 12:40 pm
by microthick
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´");
Posted: Tue Dec 23, 2003 12:56 pm
by vigge89
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"
Posted: Tue Dec 23, 2003 1:01 pm
by microthick
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");
Posted: Tue Dec 23, 2003 1:05 pm
by vigge89
1: yep it does
2: id
3: yes, there are two records, 1 & 2
4: ill try now
edit: still no result
Posted: Tue Dec 23, 2003 1:13 pm
by microthick
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?
Posted: Tue Dec 23, 2003 1:22 pm
by microthick
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());
Posted: Tue Dec 23, 2003 1:51 pm
by vigge89
still won't work :/
Posted: Tue Dec 23, 2003 1:57 pm
by microthick
Remove the @ signs from the first two lines. The @ signs block any error messages from showing.
Posted: Tue Dec 23, 2003 2:01 pm
by vigge89
I get the same error, nothing else
Posted: Tue Dec 23, 2003 2:01 pm
by Draco_03
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());
Posted: Tue Dec 23, 2003 2:05 pm
by vigge89
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
Posted: Tue Dec 23, 2003 2:10 pm
by Draco_03
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