PDA

View Full Version : my query seems its wrong



garevn
04-14-2011, 04:37 PM
Hello i am trying to make a simple ajax database search like it explain Here
http://www.w3schools.com/php/php_ajax_database.asp
But when i am trying to display records it pops that i have an error on my query line which is this:

"SELECT * FROM products WHERE productid = '".$q."'"
The 'q' is the user option.
Did i use an invalid format or what?
Soz i am new in programming

sunfighter
04-14-2011, 07:17 PM
Your missing and end ;

Otherwise it look ok.


I hope your query looks like this in its entirety.

$query = "SELECT * FROM products WHERE productid = '" . $q . "'";
$result = mysql_query($query);

Old Pedant
04-14-2011, 08:20 PM
And if sunfighter's guess isn't right, then show your *REAL* code. The whole thing. Not a fragment.

garevn
04-14-2011, 08:26 PM
<?php require_once('Connections/Mysitedb.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}

$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}

mysql_select_db($database_Mysitedb, $Mysitedb);
$query_Recordset1 = "SELECT * FROM products WHERE productid = '" . $q . "'";
$Recordset1 = mysql_query($query_Recordset1, $Mysitedb) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<table>
<tr>
<td width="132">price</td>
<td width="138"><?php echo $row_Recordset1['price']; ?></td>
</tr>
<tr>
<td>name</td>
<td><?php echo $row_Recordset1['productname']; ?></td>
</tr>
<tr>
<td>man</td>
<td><?php echo $row_Recordset1['manufacturer']; ?></td>
</tr>
</table>
</body>
</html>
<?php
mysql_free_result($Recordset1);
?>

garevn
04-14-2011, 08:47 PM
oO ok i forgot to add

$q=$_GET["q"];
at the top, it seems start working now

maniakk
04-14-2011, 09:10 PM
Actually, when yo do the query, you don't actually have to do "SELECT * FROM table WHERE column='" . $request . "'". You can do "SELECT * FROM table WHERE column='$request'"..... I'm new here, but I know a lot of languages! Ask me for help sometime! :)

Old Pedant
04-14-2011, 09:48 PM
I'm new here, but I know a lot of languages! Ask me for help sometime! :)

Okay: Чего должны вы сделать если вы имеете слона в вашем карманн?

Or if you meant computer languages, how do you exit early from an expression in Prolog?

[Just kidding! Welcome. But those who post here don't ask *specific* people for help. They just post a question and anybody who can answers.]

Old Pedant
04-14-2011, 09:51 PM
oO ok i forgot to add

$q=$_GET["q"];
at the top, it seems start working now

LOL! It's always the really HUGE elephants that seem to vanish when you look for them.

munkeyboy
04-15-2011, 12:06 AM
oO ok i forgot to add

$q=$_GET["q"];
at the top, it seems start working now

You should run $q through the mysql_real_escape_string (http://php.net/manual/en/function.mysql-real-escape-string.php) function before using it in the select statement. Otherwise you leave yourself wide open to a SQL Injection attack.

maniakk
04-15-2011, 12:42 AM
What should you do if you have an elephant in your pocket?!?!?!?!? cpu languages. and i think either return or break.?...

Old Pedant
04-15-2011, 12:58 AM
LOL! Xorosho!

No, Prolog is a weird language. No such thing as return or break. The answer is: You don't. But you don't need to, if you wrote the code right. <grin/>

*******

Postscript: I admit to using Babelfish to do that translation simply because I didn't want to hunt and peck the Cyrillic characters.

If I'd written it myself I would have said Что должно делать если у вас слон в кармане?

And Babelfish translates that as "What must make if in you elephant in the pocket?"

Which tells me that Babelfish doesn't understand idiomatic language. My Russian is rusty, but I think "у вас" clearly means "you have" in normal speech.

Ahhh...interesting. If you reverse the condition (which probably is more common speech) to "если у вас слон в кармане, Что делать ?" then Babelfish comes up with "if you have elephant in the pocket, what to make?" which I'd accept as close enough. Fun stuff!