Jump to content

Need Help In Getting Data From Mysql Bd


Recommended Posts

Hello

 

i am still wondering about my code that always show same error

after i am made some changes.

actually i am run the codes in my localhost in my android phone

with version: php 5.4 and MySQL 5.1

and then will be uploaded to my helio account.

(coding with text editor more easy than with ftp site i think :) )

 

 

this is my code

 

if i am run this code:

 

<?php
$db = new mysqli("localhost", "root", "", "default_db");
if ($db->connect_error)
{
	die("Connection error");
}
else
{
	$sql = "SELECT * FROM 'users' WHERE 'nick' = '$u'";

	if ($db->query($sql) === TRUE)
	{
		$data = $db->query($sql)->fetch_array();
		echo $data['email'];
		
	}
	else
	{
		echo "Query error: " . $db->error;
	}

}
?>
it will show an error like this:

 

Query 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 'nick' = ''' at line 1

please anyone tell me which the part that wrong

i am confuse for the last some days up to now because this :(

 

Thanks in advance.. :)

Link to comment
Share on other sites

SELECT * FROM 'users' WHERE 'nick' = '$u'

Should be:

SELECT * FROM users WHERE `nick` = '$u'

You used the wrong type of quotes. You need the ` symbol (the one to the left of the number 1, above the tab key), not the ' (apostrophe). around the field names. If you want something around the table names, you should use these there too, though the code will work without anything around the table name.

Link to comment
Share on other sites

This should be closer to correct, though I didn't test it and there may be syntax errors.

 

Your issues were both the SQL syntax described above, and also your IF statement was wrong. mysqli::query() doesn't return a boolean TRUE when you run a SELECT, it returns a mysqli_result object instead. Your IF was explicitly looking for a Boolean true, but since the function doesn't return true when a SELECT succeeds, the else section always runs (there's no database error, hence why $db->error was empty).

 

The quick way to check if a mysqli::query() was successful is to check if the returned value is empty(). Boolean TRUE (the result of an INSERT, UPDATE, etc.), mysqli_result objects (the result of SELECT), etc. are considered to be non-empty. Boolean false from a failed query is always considered empty.

<?php
$db = new mysqli("localhost", "root", "", "default_db");
if ($db->connect_error)
{
	die("Connection error");
}
else
{
	$sql = "SELECT * FROM users WHERE `nick` = '$u'";
        $result = $db->query($sql);

	if (!empty($result)) //Check that it's not empty. Non-empty queries are successful.
	{
		$data = $result->fetch_array(MYSQLI_ASSOC);
		echo $data['email'];
		
	}
	else
	{
		echo "Query error: " . $db->error;
	}

}
?>
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...