PDA

View Full Version : [HELP] MySQL/PHP



Negaal
02-09-2008, 10:29 PM
This shows only Sucessfully connnected to MySQL db



<?
$user="mw_1557497";
$password="lololoo- i wont tell you";
$database="mw_1557497_Score";
$host="sql106.my-webs.org";

$Name=$_POST['Name'];
$Score=$_POST['Score'];

mysql_connect($host,$user,$password);
@mysql_select_db($database) or die( "Unable to select database");
echo "Sucessfully connnected to MySQL db";
//$query = "INSERT INTO scores VALUES ('',$name, $Score)";
$query="SELECT * FROM Scores";
$result=mysql_query($query);
$num=mysql_numrows($result);

mysql_close();

$i=0;
while ($i < $num) {

$Name=mysql_result($result,$i,"Name");
$last=mysql_result($result,$i,"Score");

echo "Name: " . $Name. " "; // . "<b />";
echo "Score: " . $Score . " "; // . "<b />";

$i++;
}

?>


I'v been doing something wrong...need php masters now!:p

Wizzup?
02-09-2008, 10:48 PM
Try using mysql_close() at the end of your script.


@mysql_select_db($database) or die( "Unable to select database");
Remove the @

Try using something like

while ($myobj= mysql_fetch_object($res))
{
echo $myobj->name;
}

or


while (list($name, $last) = mysql_fetch_array($res)))
{
echo $name;
}
EDIT: Also you assign $last but try to print out $Score.

Also, remember that PHP variables are case sensitive.

Negaal
02-09-2008, 10:54 PM
Also, remember that PHP variables are case sensitive

Gosh, hate that, thanks for your quick answer...problably i'll post more in this thread...I started with php yesterday:p

Wizzup?
02-09-2008, 11:50 PM
Hehe. Thats fine.
Also


<?php
echo 'hoi';
?>

Always use <?php, not just <?. In the newer PHP versions that longer works. :)

Negaal
02-10-2008, 08:30 AM
Ok...I keep getting confused :confsued:

This is all I get


Sucessfully connnected to MySQL db
Error: Query was empty

from:


<?php
$user="mw_1557497";
$password="In your Dreams:p";
$database="mw_1557497_Score";
$host="sql106.my-webs.org";

$Name=$_POST['Name'];
$Score=$_POST['Score'];

mysql_connect($host,$user,$password);
mysql_select_db($database) or die( "Unable to select database < br/>");
echo "Sucessfully connnected to MySQL db <br />"; //Displays this

$sql = "CREATE TABLE Scores
(
ScoreID int NOT NULL AUTO_INCREMENT,
PRIMARY KEY(ScoreID),
Name varchar(15),
Score int
)";

mysql_query($sql);

$sql=mysql_query("INSERT INTO (Name, Score)
VALUES ($Name, $Score)");

mysql_query($sql);

if (!mysql_query($sql))
{
die('Error: ' . mysql_error()); //and this!
}
echo "1 record added";

while($row = mysql_fetch_assoc($sql))
{
echo "Name :{$row['Name']} <br>" .
"Score : {$row['Score']} <br><br>";
}

echo "Closing connection <br />";
mysql_close();

?>

I'v done like thousand edits...still "Query is empty", thats not good:S

Wizzup?
02-10-2008, 09:20 AM
$sql = "CREATE TABLE Scores
(
ScoreID int NOT NULL AUTO_INCREMENT,
PRIMARY KEY(ScoreID),
Name varchar(15),
Score int
)";


You should give score a length, too.
Also, you use mysql_query way too much.
If you want to do it the easy way do it like this:



$sqlq = "INSERT INTO MyTable (Name, Score) VALUE ('Negaal', 50)";
$result = mysql_query($sqlq);
if (!$result) Die(mysql_error());


Or:



$sqlq = "INSERT INTO MyTable (Name, Score) VALUE ('Negaal', 50)";
$result = mysql_query($sqlq); Or Die(mysql_error());


So, if you want to keep the 'result' of an INSERT query, you have to assign a variable to it's result.

Which means you cannot do this:


$sql=mysql_query("INSERT INTO (Name, Score)
VALUES ($Name, $Score)");

mysql_query($sql);

if (!mysql_query($sql))
{
die('Error: ' . mysql_error()); //and this!
}
echo "1 record added";

Either do


$sql=mysql_query("INSERT INTO (Name, Score)
VALUES ($Name, $Score)");

if (!$sql)
{
die('Error: ' . mysql_error()); //and this!
}
echo "1 record added";

OR


$sql="INSERT INTO (Name, Score)
VALUES ($Name, $Score)";

$result = mysql_query($sql);

if (!$result)
{
die('Error: ' . mysql_error()); //and this!
}
echo "1 record added";

EDIT:
In PHP, try to refrain from using double quotes.

When you start adding variables into MySQL databases, probably $_POST[] ones,
you must use string escaping:


$sql = 'INSERT INTO `MyTable` (name, price, cat_id, lev_id) VALUES (\'' . $name . '\',\'' . $price . '\', \'' . $cid . '\', \'' . $lid . '\')';

Negaal
02-10-2008, 11:04 AM
Now...Ub3r Pwn4g3! (http://neg.my-webs.org/insert.php)

But how to sort them by score...
And how to use scar instead of insert.php, e.g how to send data with scar?

edit:

Markus told me something about PostHTTPPageEx, but im not sure


edit:
oh well, error 500, internal server error

is anything wrong here what may cause it?
and in outputting part how to echo the "i"?


<?php

$user="mw_1557497";
$password="LOLOLOLOLOLOLOLOLOLOLOLOOLOLOLOLOLOLOL";
$database="mw_1557497_Score";
$host="sql106.my-webs.org";

$Name=$_POST['Name']; //text
$Score=$_POST['Score']; //int
$Date=Date("d/m/y"); //text

mysql_connect($host,$user,$password);
mysql_select_db($database) or die( "Unable to select database < br/>");
echo "Sucessfully connnected to ScarQuiz highscores <br /><br />";

$sql='INSERT INTO Scores (Name, Score, Date)
VALUES (\'' . $Name . '\',\'' . $Score . '\',\'' . $Date . '\')';

$result = mysql_query($sql);

if (!$result)
{
die('Error: ' . mysql_error());
}

$select = mysql_query("SELECT Name, Score, Date FROM Scores
ORDER BY Score ASC");

echo
"<table border='1'>
<tr>
<th>Place</th>
<th>Date</th>
<th>Name</th>
<th>Score</th>
</tr>";

$i=0;
while($row = mysql_fetch_assoc($select))
{
$i++:
echo "<tr>";
//echo "<td>" . '$i' . "</td>";
//echo "<td>" . $row['Date'] . "</td>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['Score'] . "</td>";
echo "</tr>";

}
echo "</table>";
mysql_close();

?>

Negaal
02-10-2008, 02:46 PM
lololol, i didn't edited beacuse this post is different, i know where the problem is :)

See below


<?php

$user="mw_1557497";
$password="54675675675676";
$database="mw_1557497_Score";
$host="sql106.my-webs.org";

$Name=$_POST['Name'];
$Score=$_POST['Score'];
$Date=Date("d/m/y");

mysql_connect($host,$user,$password);
mysql_select_db($database) or die( "Unable to select database < br/>");
echo "ScarQuiz highscores <br /><br />";

if (!$Name="") and (!$Score=0)//this
{//this
$sql='INSERT INTO Scores (Name, Score, Date)
VALUES (\'' . $Name . '\',\'' . $Score . '\',\'' . $Date . '\')';
}//this

$result = mysql_query($sql);

if (!$result)
{
die('Error: ' . mysql_error());
}

$select = mysql_query("SELECT * FROM Scores");

echo
"<table border='1'>
<tr>
<th>Place</th>
<th>Date</th>
<th>Name</th>
<th>Score</th>
</tr>";

$i=0;
while($row = mysql_fetch_assoc($select))
{
$i= $i + 1;
echo "<tr>";
echo "<td>" . $i;
echo "<td>" . $row['Date'] . "</td>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['Score'] . "</td>";
echo "</tr>";

}
echo "</table>";
mysql_close();

?>

without these it works well

Edit, it was because of it's length, I emptyed table and now it's working, pretty annoyning:S

Dumpin
02-10-2008, 06:41 PM
you could just leave the form out and let scar send the score with AddPostVariable and stuff :o

A G E N T
02-16-2008, 03:26 PM
To use SCAR:


$Name=$_POST['Name'];
$Score=$_POST['Score'];


Then you need to use AddPOSTVariable for each one:

AddPostVariable(Client,"Name","PUTVALUEHERE");
AddPostVariable(Client,"Score","123456789");
PostHTTPPageEx(Client,"http://www.yoururl.com/insert.php");


As to your first problem, it gave me SOOO much trouble when I first started. Unlike in SCAR, where the comparison operator is "=" and the assignment operator is ":=", for some reason, in PHP, the assignment operator is "=" and the comparison is "==", and the negative comparison operator is "!=". Lastly, I think the proper "and" operator is "&&".
So:

if (!$Name="") and (!$Score=0)//this
Should become:

if ($Name != "" && $Score != 0)
{
Do stuff
}


Hopefully that helps! :)

Negaal
02-16-2008, 06:15 PM
To use SCAR:


$Name=$_POST['Name'];
$Score=$_POST['Score'];


Then you need to use AddPOSTVariable for each one:

AddPostVariable(Client,"Name","PUTVALUEHERE");
AddPostVariable(Client,"Score","123456789");
PostHTTPPageEx(Client,"http://www.yoururl.com/insert.php");


As to your first problem, it gave me SOOO much trouble when I first started. Unlike in SCAR, where the comparison operator is "=" and the assignment operator is ":=", for some reason, in PHP, the assignment operator is "=" and the comparison is "==", and the negative comparison operator is "!=". Lastly, I think the proper "and" operator is "&&".
So:

if (!$Name="") and (!$Score=0)//this
Should become:

if ($Name != "" && $Score != 0)
{
Do stuff
}


Hopefully that helps! :)

Thanks man couldn't figure out the "&&". thank you:)

Wizzup?
02-16-2008, 06:22 PM
Add a dummy POST Variable in SCAR. SCAR seems to mess up the last one, so if you add one that you do not use, you should be fine... :)
Like this:

Function SendSound(Recipient, Sound: String): Boolean;

Var
s: String;
Client: Integer;

Begin
WriteLn('Sending ' + Sound + ' to ' + Recipient + '.');
Client := InitializeHTTPClient(False, False);
ClearPostData(Client);
AddPostVariable(Client, 'snd', Sound);
AddPostVariable(Client, 'recip', Recipient);
AddPostVariable(Client, 'a', 'lol'); // HTTP adds a newline and enter
s := PostHTTPPageEx(Client, Link + 'AddSound.php');
FreeHTTPClient(Client);
Result := True;
End;

Negaal
02-17-2008, 10:14 AM
Thanks everyone for their advice. My highscore database seems to be finished.
You can have a look in "other scripts" at "The radar". Works well.

Anyways, would be cool if some close or leave this thread, I got all my help I wanted.

Thanks.