Jump to content
xisto Community
shadow-x

Need Syntax Help With MySQL And PHP

Recommended Posts

I've been working on this for hours now which is really embarrasing really considering I've got it to work perfectly in the past but I cant find the script I used then :lol: Here is the script im working on:

1] mysql_connect("localhost", "root") or die( "Unable to connect to database");2] echo "connected \n ";
3] mysql_select_db("users") or die("Unable to select database");
4] echo "selected ";
5] $query = "SELECT pass FROM 'login' WHERE name = 'chris'";
6] $result = mysql_query( "SELECT pass FROM login WHERE name=chris" ) or die ("Bad query");
7] echo "$result";



By using the "or die" function on line 6 it shows that the query is wrong, this is supposing that "or die" works with the "MYSQL_QUERY" function, I just improvised there and hoped it would work to show me where the problem is. The script connects to the database and selects the right table so what else could be wrong? I dont see anything wrong with the query. I was also trying for an hour or so to get a variable array when selecting the entire row but that didnt work either :lol: Is there anything I can do?

Thanks

PS. No error is actually dislayed other than the "or die" in line 6

Share this post


Link to post
Share on other sites

Try this

$result = mysql_query( $query ) or die ("Bad query".mysql_error());
You have already defined the query in
$query = "SELECT pass FROM login WHERE name = 'chris'";
in that line so the top line will send the query that you defined and attempt to execute it and if not will state Bad query and give an error from the MySQL server.

Share this post


Link to post
Share on other sites

I've been working on this for hours now which is really embarrasing really considering I've got it to work perfectly in the past but I cant find the script I used then :lol: Here is the script im working on:


By using the "or die" function on line 6 it shows that the query is wrong, this is supposing that "or die" works with the "MYSQL_QUERY" function, I just improvised there and hoped it would work to show me where the problem is. The script connects to the database and selects the right table so what else could be wrong? I dont see anything wrong with the query. I was also trying for an hour or so to get a variable array when selecting the entire row but that didnt work either :lol: Is there anything I can do?

Thanks

PS. No error is actually dislayed other than the "or die" in line 6

Ok ! wait for me .....................

Share this post


Link to post
Share on other sites

PS. No error is actually dislayed other than the "or die" in line 6

 


Looking at the codes..

The only line that may cause the error is line 6.

Houdini is right, you have already assigned the query, why not use it anyway..

 

5] $query = "SELECT pass FROM 'login' WHERE name = 'chris'";6] $result = mysql_query( "SELECT pass FROM login WHERE name=chris" ) or die ("Bad query");

I think this can be written like this much better. Assuming that you will not use line 5.

 

6] $result = mysql_query( "SELECT pass FROM 'login' WHERE name='chris'" ) or die ("Bad query");

notice the ' part. sometimes the database parser engines fails to find tables or coloumns and the easiest way to fix that is to put those nasty ' in the right place.

Share this post


Link to post
Share on other sites

Reason why line 6 errors, is because chris is a string 'chris', quote it. Also what's the point in creating the $query if you're not going to use it?

I don't use root with no passwords, so I think you should set a password for root, and create a new user to use instead with only the privilleges needed, in this case only SELECT.

if(!$dbconnect = mysql_connect('localhost', 'user1', 'password')) exit('Unable to connect to database');if(!mysql_select_db('users')) exit('Unable to select database');$query =  "SELECT pass FROM login WHERE name = 'chris'";$result = @mysql_query($query, $dbconnect);echo $result;

This isn't foolproof code, it needs work.


Cheers,


MC

Share this post


Link to post
Share on other sites

6] $result = mysql_query( "SELECT pass FROM 'login' WHERE name='chris'" ) or die ("Bad query");
notice the ' part. sometimes the database parser engines fails to find tables or coloumns and the easiest way to fix that is to put those nasty ' in the right place.

 

Is that a quote or a tic? Seems like it's usually a ` rather than a quote to enclose a table. I might be confused on this. Anyways exactly what I was going to say mastercomputers. Make sure you enclose all your strings with quotes. Usually a single quote is what the database requires, although sometimes it can be different I'm told.

Share this post


Link to post
Share on other sites

Is that a quote or a tic? Seems like it's usually a ` rather than a quote to enclose a table. I might be confused on this. Anyways exactly what I was going to say mastercomputers. Make sure you enclose all your strings with quotes. Usually a single quote is what the database requires, although sometimes it can be different I'm told.


its no always necesary to enclose a table name with qoutes or tics, if you use for example phpMyAdmin with the option to view generated php code, its Use it always, bUt if you create a SQL statement using the SQL Tab phpMyAdmin you can or cannot inclUde the tics.

I usually dont use the tics caUse it is not necesary, i think that your problem is with the sql statement, or you use the variable in line 5 o use the statement in line 6, both are legal. I use the first form when the statements are too long and with a lot of variables i gonna use, and the second when they are short and simple sql queries.

other thing i think is making problem is the way you show $result in line 7, i never use that kind of echo and i dont have the time in this moment to test it, please try this instead:

$result=mysql_query("SELECT pass FROM login WHERE name='chris'") 	or die ("Bad query" . mysql_errno(). ": " . mysql_error() );if (mysql_num_rows($result)> 0) {	$row=mysql_fetch_array($result);	echo $row["pass"];}

best regards,

Share this post


Link to post
Share on other sites

Is that a quote or a tic? Seems like it's usually a ` rather than a quote to enclose a table.


Actually, it seems very strange to me that if I work directly with mySQL, it requires me to use a tic rather than the single quote [it is a quote by the way] and on some clients, it requires me to use the quote.

and depending on how the server was compiled, it will make the tic/quote optional.

-----

@shadow-x
if you remove the tic/quote part.. the string must not have spaces else the tic/quote is required. :lol:

maybe i can help..

Share this post


Link to post
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...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.