Jump to content
php.lv forumi

mysql_fetch_assoc()


homers

Recommended Posts

errors:

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\cup\pages\turniri.php on line 177

176-178rinda:

$d1 = $_POST['komentars'];
$data = mysql_fetch_assoc(mysql_query("SELECT komanda FROM turniri WHERE komanda = '$d1'"));
$d2 = $data['komentars'];

Edited by homers
Link to comment
Share on other sites

Kāda vispār šim kvērijam ir jēga?

 

1) Nekad neliec kvērijā datus, no lietotāja, pa tiešo. Apskati kas ir mysql_real_escape_string()

2) Rakstīt visu vienā rindā ne vienmēr ir forši. Labāk šādi:

$sql = sprintf("SELECT komanda FROM turniri WHERE komanda = '%s'", mysql_real_escape_string($_POST["komentars"]));
$result = mysql_query($sql) or die("Kļūda: " . mysql_error());

//ja sagaidi vienu rezultātu, tad...
$row = mysql_fetch_assoc($result);
$komanda = $row["komanda"];

//ja sagaidi vairākus rezultātus, tad...
while($row = mysql_fetch_assoc($result)) {
 $komanda = $row["komanda"];
}

3) Ja kļūda būs pašā kvērijā un radīsies kļūda, tad par to tiks paziņots.

4) Lieto aprakstošākus mainīgo nosaukumus. Nākotnē, ja kaut kas būs jālabo, tad šādi būs vieglāk saprast kas ir kas.

 

Bet kā jau teicu - kāda vispār tam kvērijam ir jēga?

Edited by rATRIJS
Link to comment
Share on other sites

Tādā gadījumā var izmantot vai nu:

$sql = "SELECT COUNT(*) FROM ...";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
if($row[0] > 0) {
//piedalās
} else {
//nepiedalās
}

 

vai arī:

$sql = "SELECT id FROM ...";
$result = mysql_query($sql);
if(mysql_num_rows($result) > 0) {
//ir
} else {
//nav
}

 

Pirmais variants, manuprāt, ir labāks...

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...