Jump to content

My SQL Noob at work

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
11 replies to this topic

#1
phpforfun

phpforfun

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,236 posts
So I just started MySQL and SQL today, using SQLyog. Pretty cool stuff. Using a tutorial I found online, however im getting an error.. Here is the code.
<?

	$host		=	'localhost';

	$user		=	'root';

	$pass		=	'password';

	$database	=	'roscripts';

	$connect = @mysql_connect ( $host, $user, $pass ) ;


	$sql = "SELECT * FROM `articles` WHERE `ID` = " . mysql_real_escape_string ( $_GET['ID'] );


	mysql_select_db ( $database, $connect );

	if ( @mysql_query ( $sql ) )

	{

		$query = mysql_query ( $sql );

		$row = mysql_fetch_assoc ( $query );


		echo $row['ID'] . '<br />' . $row['article_title'] . '<br />' . $row['article_content'];

	}

	else {

		die ( mysql_error () );

	}

?>

When I put that in the page, I get this error..

Quote

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

Whats the right suntax? I have the latest version of everything, I am using Apache2triad.

Thanks guys!

#2
phpforfun

phpforfun

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,236 posts
Anyone wanna take a shot?

#3
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
$sql = "SELECT * FROM `articles` WHERE `ID` = '" . mysql_real_escape_string ( $_GET['ID'] ) . "'";

If that doesn't work, remove the error suppression operator [ @ ]

#4
Guest_Jordan_*

Guest_Jordan_*
  • Guests
With MySQL I generally leave out most of the ` symbols:

$sql = "SELECT * FROM articles WHERE ID= '" . mysql_real_escape_string ( $_GET['ID'] ) . "'";  

If that doesn't work do

echo $sql;

right after that line. Then copy/paste that info into MySQL directly. You will generally get a more descriptive error (you can use PHPMyAdmin or MySQL from the console for this).

#5
phpforfun

phpforfun

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,236 posts
Now it doesnt show anything when I do any of the above :-\

#6
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
I find the back ticks [ ` ] to be a good practice. Prior to using back ticks I had a problem with one of my SQL queries, which took me several hours to debug. The problem ended up being that one of the column names in my query was a reserved word, using the back ticks solved the problem. So thats why I find it a good practice...

#7
phpforfun

phpforfun

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,236 posts
How does a back tick solve it? Is it a substitute for something?

Go to this:

http://ffe.no-ip.org/mysql/

Selecting.php is the one were looking at. It now wont show.. anything

#8
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
The back tick explicitly says "whats between me is a column name."


Have you removed the @ symbol yet?...

#9
phpforfun

phpforfun

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,236 posts

John said:

The back tick explicitly says "whats between me is a column name."


Have you removed the @ symbol yet?...

Yes sir.

<?PHP

	$host		=	'localhost';

	$user		=	'root';

	$pass		=	'private1';

	$database	=	'roscripts';

	$connect = @mysql_connect ( $host, $user, $pass ) ;


	$sql = "SELECT * FROM `articles` WHERE `ID` = '" . mysql_real_escape_string ( $_GET['ID'] ) . "'";    


	mysql_select_db ( $database, $connect );

	if ( @mysql_query ( $sql ) )

	{

		$query = mysql_query ( $sql );

		$row = mysql_fetch_assoc ( $query );


		echo $row['ID'] . '<br />' . $row['article_title'] . '<br />' . $row['article_content'];

	}

	else {

		die ( mysql_error () );

	}

?>

Thats the code now, which will output nothing.

#10
phpforfun

phpforfun

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,236 posts
So then, what code do you guys use to echo whats in a db?

#11
TkTech

TkTech

    The Crazy One

  • Moderators
  • 1,396 posts
[HIGHLIGHT="PHP"]
<?
$user = 'root';
$pass = 'password';
$database = 'roscripts';
$table = 'Name of the table';

mysql_connect ( "localhost", $user, $pass ) ;
mysql_select_db ( $database);
$result = mysql_query("SELECT * FROM `$table`");

for($i=0;$i<mysql_num_rows($result);$i++){
$row = mysql_fetch_row($result);
echo "#" . ($i+1) . ".";
for($x=0;$x<mysql_num_fields($result);$x++){
echo $row[$x] . " | ";
}
echo "<br />";
}
?>
[/HIGHLIGHT]

That should do for just printing everything in a table. I don't know if that will work right out of the box, Windows Mobile doesn't have PHP yet ^^

#12
phpforfun

phpforfun

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,236 posts
good news is that works like a charm. bad news I have NO idea what all that code means, but I learn fast ;-) Thanks guys