Tech Support Forum banner
Status
Not open for further replies.
1 - 18 of 18 Posts

· Registered
Joined
·
207 Posts
Discussion Starter · #1 ·
Hey guys i can not get my Select statement to work. It looks correct to me. I have tried it sevral diffrent ways and still can't get it could you tell me the problem?

Code:
mysql_query("SELECT * FROM $table_name WHERE name='Jeremy'") ;
 

· Registered
Joined
·
207 Posts
Discussion Starter · #3 ·
the table name is testing_script wich is set as $table_name but i figured out the problem. I set the select statement to a veriable and then echo the veriable.
 

· Registered
Joined
·
41 Posts
Show more of the script. You may have forgot ta use mysql_connect or more. Mysql searches take about five lines total to get something useful. sometimes you need to use dbasename.tablename in the search. Depends how you connected, username, password, and some other things.
 

· Registered
Joined
·
899 Posts
It's probably because you single quote "name" but not the table name. MySQL likes consistency. Try this:
PHP:
$table_name = "test_tbl";
$name = "Jeremy";

mysql_query("SELECT * FROM $table_name WHERE name=$name") ;
Not that it's any concern...but this is how I personally do it now:
PHP:
$table_name = "test_tbl";
$name = "Jeremy";

mysql_query("SELECT * FROM '".$table_name."' WHERE name='".$name."'") ;
This way just helps you to see the variables rather than turning them all red if you're editing with a program that recognizes syntax highlighting.
 

· Registered
Joined
·
207 Posts
Discussion Starter · #6 · (Edited)
Thanks guys. I upadted the code and now i am getting Fatal error: Call to undefined function: () in /home/kelvoron/public_html/testing/select.php on line 15 --- and there is no line 15. anyway heres the updated code (i decided to go your way redcore. i also went out and bought a couple books to teach me about the built in functions in php any way here it is

--------Removed old code ----------------------


I also tried it as echo result; it did not work eaither

wait i think i may have found a problem let me check it befor you guys reply

Ok now i dont get the error but It is not returning any information heres the code

PHP:
<?
$host="localhost";
$username="-------";
$password="******";
$db_name="kelvoron_testscripts";
$table_name = "test_tbl"; 
$name = "Jeremy"; 

mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");


$sql="SELECT * FROM '".$table_name."' WHERE name='".$name."'" ;

$result=mysql_query($sql);


echo ($result);





mysql_close();
?>
 

· Registered
Joined
·
41 Posts
Well, it shouldn't error, but $result won't echo useful info. I think $result is an object, not a variable. You need to extract info from the $result using fetch_row or fetch_assoc or other options.
Following makes more sense:

$result=mysql_query($sql); //fetches object
$row=mysql_fetch_row($result); // fetches one row at a time (many could be in object)
print " $row[0] , $row[1] , $row[2] , etc..."; // displays column 0, 1, and 2.

Pretty sure that is the syntax exactly. Usually it is good to use a loop while $row is not false.
 

· Registered
Joined
·
207 Posts
Discussion Starter · #9 ·
ok i tried the fetch code and it gave me an error, however i belive the fault is on my end. I am going to check my book for fetch and see how to properly use it. Thank you both for all of your help.
 

· Registered
Joined
·
899 Posts
Bah, books? BOOKS? What is this books nonsense? Google + PHP.net > all books!

:p

What is the specific error you're getting though? Just plug the code into Google (minus specific things like the page/line the error is on) and 99% of the time you'll find what needs to be fixed.
 

· Registered
Joined
·
207 Posts
Discussion Starter · #11 ·
I got two errors:

Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in /home/kelvoron/public_html/testing/select.php on line 17

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/kelvoron/public_html/testing/select.php on line 20

I plugd them both in google, it the first three things listed there said i must have made a typo in my sql statement. I then put in the error code and it returns

Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''users' WHERE user='damion'' at line 1

and this is how i did the fetch function

$rows=mysql_fetch_row($result);
echo $rows['name'];
 

· Registered
Joined
·
207 Posts
Discussion Starter · #12 · (Edited)
(sorry ofr double post it won't let me edit the first)

Ok i got rid of most of the errors I know that the problem is with the line:

echo (row['user']);

because the error changes as i work on that line the error is:

Parse error: syntax error, unexpected '[' in /home/kelvoron/public_html/szabo/select.php on line 16

PHP:
<?
$host="localhost";
$username="kelvoron_damion";
$password="muddog";
$db_name="kelvoron_genxusers";
//$table_name = "users"; 
//$name = "damion"; 

mysql_connect($host, $username, $password)or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * From users";
$result=mysql_query($sql)or die("query failed: ".mysql_error());
$row=mysql_fetch_array($result);

echo (row['user']);

mysql_close();
?>

I am hopeing i can fix this myself but if some one knows whats wrong before i post that i fixed it please let me know. Thanks.

EDIT: I got it you must make it: echo ($row['user']); <- forgot the $

thanks to every one for the help
 

· Registered
Joined
·
41 Posts
How did query (line 20) end up after fetch (line 17)?

The second error. Could be an sql syntax error. Or maybe a wrong column name, table name, database name...

Also, I'm not sure you can use $rows['name'] after mysql_fetch_row($result). You might need to use mysql_fetch_assoc($result). Other wise stick to $row[number]. Probly depends on php version.


// Here is what I think you should have.

mysql_connect('something','something','something'); // needs perfect
$table_name='the exact table name case sensitive'; //needs perfect
$search="Damien"; // case sensitive and exact match
$column='some column to search';
$sql="SELECT * FROM '$table_name' WHERE $column='$search' "; // varies per database
// somecolumn is whatever you named the column your searching, needs exactness
$query=mysql_query($sql); //don't forget semicolon
while ( $row=mysql_fetch_assoc($query) ) {
// no semicolons in while()
print $row['somecolumn'];
print " <br> \n "; // html break, and php return

}
// if there were 10 Damien then it would print all of them.
// if there is 10 damien then it would print none of them.
// copy and paste all of this and change the 'somethings'
// hahahaha. computers love to waste time.
//
//By the way, once you get it to work. Save it to a txt file so you can cut and paste //wherever you need to hit the database. Then forget about it forever, or it will plague
// you again and again. Muhahaha
 

· Registered
Joined
·
207 Posts
Discussion Starter · #17 ·
no worrys i keep five copys of my toolbox 1 on a pen drive used only for toolbox wich is the one i keep with me, one on my external hard drive 1 on my secondary hard drive in my pc and one on my web host
 
1 - 18 of 18 Posts
Status
Not open for further replies.
Top