Jump to content

PHP Error


neex1233

Recommended Posts

I made a script (Below).

 

<?php
$con = mysql_connect("localhost","Username","Password");
mysql_select_db("Database", $con);
$username = mysql_real_escape_string($_POST["username"]);
$sql = 'SELECT * FROM `users` WHERE username = \'$username\' LIMIT 0, 30 '; 
$echo = mysql_query($sql)
or die (mysql_error());
echo "$echo";
?>

 

But I get this error:

Resource id #3

 

Can somebody help me? By the way, the $username variable is always set through a form.

 

Link to comment
https://forums.phpfreaks.com/topic/163689-php-error/
Share on other sites

<?php

$con = mysql_connect("localhost", "Username", "Password") or die('Could not connect: ' . mysql_error());
mysql_select_db("Database", $con);

$username = mysql_real_escape_string($_POST["username"]);

$sql = "
SELECT * 
FROM `users` 
WHERE username = '$username' 
LIMIT 0, 30
"; 
$result = mysql_query($sql) or die (mysql_error());

while ($row = mysql_fetch_array($result))
{
print_r($row);
}

Link to comment
https://forums.phpfreaks.com/topic/163689-php-error/#findComment-863720
Share on other sites

That works but it shows the result as: (I hid the username and stuff...)

 

 

Don't Look [2] => Don't Look [password] => Don't Look [3] => Don't Look [userlevel] => Don't Look [4] => Don't Look => Don't Look [5] => Don't Look [tokens] => Don't Look [6] => Don't Look [name] => Don't Look [7] => Don't Look[iP] => Don't Look )

Link to comment
https://forums.phpfreaks.com/topic/163689-php-error/#findComment-863730
Share on other sites

thats because you are echoing an array. you have to use foreach or something to echo every element of the array

 

foreach ($echo as $key => $value) {
echo "Key: ".$key." Value: ".$value."<br />";
}

 

that would show each entry in the array, plus its corresponding key

Link to comment
https://forums.phpfreaks.com/topic/163689-php-error/#findComment-863738
Share on other sites

Ok, well this is the code I'm using:

 

<?php

$con = mysql_connect("localhost", "Username", "Password") or die('Could not connect: ' . mysql_error());
mysql_select_db("Database", $con);

$username = mysql_real_escape_string($_POST["username"]);

$sql = "
   SELECT * 
   FROM `users` 
   WHERE username = '$username' 
   LIMIT 0, 30
"; 
$result = mysql_query($sql) or die (mysql_error());

while ($row = mysql_fetch_array($result))

echo $row['id'];
echo $row['username'];
echo $row['password'];
echo $row['userlevel'];
echo $row['email'];
echo $row['tokens'];
echo $row['name'];
echo $row['IP'];
?>

 

But it only displays the first echo, which is id

Link to comment
https://forums.phpfreaks.com/topic/163689-php-error/#findComment-863746
Share on other sites

<?php

$con = mysql_connect("localhost", "Username", "Password") or die('Could not connect: ' . mysql_error());
mysql_select_db("Database", $con);

$username = mysql_real_escape_string($_POST["username"]);

$sql = "
   SELECT * 
   FROM `users` 
   WHERE username = '$username' 
   LIMIT 0, 30
"; 
$result = mysql_query($sql) or die (mysql_error());

while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo $row['id'];
echo $row['username'];
echo $row['password'];
echo $row['userlevel'];
echo $row['email'];
echo $row['tokens'];
echo $row['name'];
echo $row['IP'];
}
?>

 

If you dont use curly braces in the statement it assumes the statement to end at the first semicolon after the closing bracket. Also you are selecing more data than needed, I added the MYSQL_ASSOC but I wouldnt use SELECT * unless you need all or most of the data from the table.

Link to comment
https://forums.phpfreaks.com/topic/163689-php-error/#findComment-863750
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.