SRL Archive - Web Development

[HELP] MySQL/PHP


Negaal

2008-02-09 17:29:50

This shows only Sucessfully connnected to MySQL db

[PHP]
<?
$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++;
}

?>
[/PHP]

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

Wizzup?

2008-02-09 17:48:32

Try using mysql_close() at the end of your script.

[php]@mysql_select_db($database) or die( "Unable to select database"); [/php]
Remove the @

Try using something like
[php]while ($myobj= mysql_fetch_object($res))
{
echo $myobj->name;
}[/php]

or

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

Also, remember that PHP variables are case sensitive.

Negaal

2008-02-09 17:54:31

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?

2008-02-09 18:50:00

Hehe. Thats fine.
Also

[php]<?php
echo 'hoi';
?>[/php]

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

Negaal

2008-02-10 03:30:13

Ok…I keep getting confused :confsued:

This is all I get

Sucessfully connnected to MySQL db
Error: Query was empty


from:
[PHP]
<?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();

?>[/PHP]

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

Wizzup?

2008-02-10 04:20:12

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

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:

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

Or:

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

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:

[php]$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"; [/php]

Either do

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

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

OR

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

$result = mysql_query($sql);

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

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:

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

Negaal

2008-02-10 06:04:55

Now...Ub3r Pwn4g3!

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]<?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();

?>[/PHP]

Negaal

2008-02-10 09:46:49

lololol, i didn't edited beacuse this post is different, i know where the problem is :)

See below
[PHP]
<?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();

?>[/PHP]

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

2008-02-10 13:41:06

you could just leave the form out and let scar send the score with AddPostVariable and stuff :o

A G E N T

2008-02-16 10:26:42

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

2008-02-16 13:15:49

From: A G E N T 83
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?

2008-02-16 13:22:54

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

2008-02-17 05:14:34

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.