Jump to content
php.lv forumi

mysql nedod ārā datus


nakedmika

Recommended Posts

Sveiki!

nu jau kādu 3 stundu mokos ar vienu mazu knifu manā kodā. Gribu lai nospiežot pogu, man parādās dati no datubāzes, bet lai kā nespaidītos, man met ārā šo >>

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 ''navigacija' WHERE 'id' = 'undefined'' at line 1

 

un pats php kods ir šāds >>

<?php
$q=$_GET["q"];

$con = mysql_connect('localhost', 'root', 'pass');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("portfolio", $con);

$sql="SELECT * FROM 'navigacija' WHERE 'id' = '".$q."'";

$result = mysql_query($sql) or die(mysql_error());

echo "<table border='1'>
<tr>
<th>id</th>
<th>name</th>
<th>content</th>
</tr>";

while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['content'] . "</td>";
echo "</tr>";
}
echo "</table>";

mysql_close($con);
?>

Link to comment
Share on other sites

Nu tak lasi ko tev raksta

 

SELECT * FROM 'navigacija' WHERE 'id' = '".$q."'";

 

jabut

 

$q = mysql_escape_stirng($q);

SELECT * FROM `navigacija` WHERE `id` = '".$q."'"; /* savadakas pedinas */

Link to comment
Share on other sites

nekas nemainās.. tas pats error. :(

bet vai tas ko manītu, ja piebilstu , ka poga darbojās ar ajax?

- pogas kods -

<div class="main">
<div id="list">
	<img src="sakums.png" alt="sākums" value="1" onclick="showUser(this.value)"/><br>
	<img src="darbi.png" alt="mani darbi" value="2" onclick="showUser(this.value)"/><br>
	<img src="kontakti.png" alt="kontakti" value="3" onclick="showUser(this.value)"/><br>
</div>
<div id="show">
		<!-- te nāk ajax sarazotie dati. -->
</div>
</div>

 

- ajax kods -

 

var xmlHttp;

function showUser(str)
{ 
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request");
return;
}
var url="get.php";
url=url+"?q="+str;
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}

function stateChanged() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{ 
document.getElementById("show").innerHTML=xmlHttp.responseText;
} 
}

function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
//Internet Explorer
try
 {
 xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
 }
catch (e)
 {
 xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
 }
}
return xmlHttp;
}

 

- nu un protams jau augstāk minētais php kods -

 

<?php
$q=$_GET["q"];

$con = mysql_connect('localhost', 'root', 'pass');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("portfolio", $con);

$sql="SELECT * FROM 'navigacija' WHERE 'id' = '".$q."'";

$result = mysql_query($sql) or die(mysql_error());

echo "<table border='1'>
<tr>
<th>id</th>
<th>name</th>
<th>content</th>
</tr>";

while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['content'] . "</td>";
echo "</tr>";
}
echo "</table>";

mysql_close($con);
?>

Link to comment
Share on other sites

Es tak skaidri un gaishi pateicu kur ir errors SQL'a, ta ka tu uzrakstiji nestradas un vis

 

 

Otrs, izmanto php.lv/paste shitik lieliem koda blokiem

 

Treshkart, tev nemaz uz sql'u id salidzinashanas vertiba nenonak JS ar apskaties velreizs

Edited by cucumber
Link to comment
Share on other sites

SQL vai nu nelieto pēdiņas ap tabulu un lauku nosaukumiem vispār, vai arī lieto "backticks" (tās nav ne pēdiņas, ne apostrofs!)

 

SELECT * FROM table_name WHERE id = ...

SELECT * FROM `table_name` WHERE `id` = ...

/* Nepareizi */ SELECT * FROM 'table_name'

 

RTFM!!! RTFM!!! RTFM!!!

Edited by Mr.Key
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.

Loading...
×
×
  • Create New...