Jump to content

Recommended Posts

Ok, well make sure you have to correct permissions for your mysql user (though I believe the query would throw an error if you didnt have the permissions) try either surrounding the tablename with backticks, or writing the table name directly in the query (with and without backticks). The query is valid (i just tested it in my table, but with a different table name of course) so if it isn't returning anything than the table is empty (or doesn't exist) I don't really see any other way for it to be returning nothing, and not doing any errors.

 

and also post the data in your table (leave out info as you see fit of course)

 

also, if you are going to have a field with 0 or 1, I believe that a boolean column would be best as those can only have either 0 or 1 as values (as opposed to varchar columns, which can be any character)

 

EDIT: oh, well that $ character can't be right, what does your query string look like again? try concatenating the variables to the string instead of writing them in there

 

like

$query = "SELECT ".$warning1.", ".$banned1." FROM ".$table.;

  • Replies 60
  • Created
  • Last Reply

Top Posters In This Topic

var_dump($query) returned:

 

string(37) "SELECT `warning`$ FROM users0001"

 

I believe there's your problem. The second variable (I think $banned1 appears to be null or perhaps not being set right.. ) looking at the posted code, I think you made more changes... >.< Can anyone see anything diff?

code:

 

<?
$conn = "localhost";
$user = "myuser";
$pass = "mypass";
$dbname = "metaldetect01";
$tbl = "users0001";
$warning1 = "`warning`";
$banned1 = "`banned`";

$con = mysql_connect($conn,$user,$pass);
if (!$con)
  {
  die('Could not connect to database: "' . $dbname . '" because ' . mysql_error());
  }

mysql_select_db($dbname, $con);

$query = "SELECT $warning1$ FROM $tbl";
var_dump($query."<br>\n");
//$query2 = "SELECT $banned1$ FROM $tbl";
//var_dump($query2);

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

//$result = mysql_query($query) or mysql_query($query2) or die(mysql_error());
$rows = mysql_num_rows($result);

echo "$rows rows returned!<br />";
//now lets go through every row returned.
while($row = mysql_fetch_assoc($result)){

$warning = $row[$warning1];
$banned = $row[$banned1];

echo "Warning: $warning<br />";
echo "Banned: $banned<br />";

if(isset($warning)){

$warning1 = "`warning`";

$banned1 = "`banned`";

 

return `warning` and `banned`

 

not warning and banned..

 

i need to create a variable that pulls a value from the database for both of those.

 

i need to make $warning and $banned use a mysql_query()..

 

so what can i do?

well of course they return `warning` and `banned`.. thats what you set them as.

 

And you are using them in the query..

 

the variable that pulls the values of those columns in the database is the $row variable. However, the problem is that your query isn't returning any rows for some reason. Do a var dump on your query as it stands currently, and run that same query in PHPmyadmin or SQL itself.

HERE's the problem.

 

$query = "SELECT $warning, $banned FROM $tbl";
//var_dump($query."<br>\n");
$result = mysql_query($query) or die(mysql_error());

//now lets go through every row returned.
while($row = mysql_fetch_assoc($result)){

$warning = $row[$warning1];
$banned = $row[$banned1];

$rows = mysql_num_rows($result);

echo "$rows rows returned!<br />";

echo "Warning: $warning<br />";
echo "Banned: $banned<br />";

if(isset($warning)){

 

the problem is no matter which side of "while" its located, $row is undefined, OR $warning and $banned are undefined.

I'm guessing you want a where clause, otherwise you're pulling all the rows from the table, and since you're only asking for the warning and banned and not the usernames/id/whatever.

 

$query = "SELECT warning, banned FROM table WHERE user = 'user'";

 

Even though there is no real advantage to putting it this way.... heres into variable form:

$select = array('warning','banned'); // get the columns you want
$table = 'table'; // select the table
$user = 'someuser'; // of course you would want to sanitize this
$query = "SELECT ".implode(', ',$select)." FROM {$table} WHERE user = '{$user}'";

var_dump($query);
echo '<br>';

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

$rows = mysql_num_rows($result);

echo "{$rows} rows returned!<br />";

 

 

now it returns:

 

string(57) "SELECT warning, banned FROM users0001 WHERE user = 'root'" 
Unknown column 'user' in 'where clause'

 

code:

 

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

//now lets go through every row returned.
while($row = mysql_fetch_assoc($result)){

$warning = $row[$warning1];
$banned = $row[$banned1];

$rows = mysql_num_rows($result);

echo "$rows rows returned!<br />";

echo "Warning: $warning<br />";
echo "Banned: $banned<br />";
*/

$select = array('warning','banned'); // get the columns you want
$table = $tbl; // select the table
// of course you would want to sanitize this
$query = "SELECT ".implode(', ',$select)." FROM {$table} WHERE user = '{$user}'";

var_dump($query);
echo '<br>';

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

$rows = mysql_num_rows($result);

echo "{$rows} rows returned!<br />";

if(isset($warning)){

Hi all. I have a small problem w/ mysql writer i made.

 

according to this post:

 

You are missing a fundemental understanding of how databases work.

You can build a table that has NAME, ADDRESS, CITY, STATE, and AGE.

Once you do that, you have a table with ZERO records in it. It's your responsibility to put data in it. So you might add some records like this:

John, Main St, Hartford, CT, 36
Betty, Route 4, Albany, NY, 40
Chris, 23 Bee Road, San Francisco, CA, 78

Now you have a table with three records. And then you can perform select statements that will extract that data from the database.

Just defaulting "warning" to 0 means that whenever you add a record to the table and don't define a value for the warning field, then the system will automatically use "0" for that record. But... You have not added a record to your table yet. So there is NO data in your table. If there is no data, then there is no data to have defaulted to "0".

A table by itself has no data. You need to add records to it.

 

How can I add not only a default value to warning and banned, but a regular value of 0 as well?

 

code:

 

<?php 
$conn = "localhost"; 
$user = "myuser"; 
$pass = "mypass"; 
$dbname = "metaldetect01"; 
$tbl = "users0001"; 
$con = mysql_connect($conn,$user,$pass); 
if (!$con) 
  { 
  die('Could not connect to database "'.$dbname. '" because ' . mysql_error()); 
  } 

// Drop database 
mysql_query("DROP DATABASE IF EXISTS $dbname"); 

// Create database 
if (mysql_query("CREATE DATABASE $dbname")) 
  { 
  echo "Database \"".$dbname."\" created"; 
  } 
else 
  { 
  echo "Error creating database: \"".$dbname."\"\nError msg: " . mysql_error(); 
  } 

// Create table 
mysql_select_db($dbname, $con); 
$sql = "CREATE TABLE $tbl 
( 
id int(11) NOT NULL auto_increment, 
number varchar(16) NOT NULL default '', 
Firstname varchar(32) NOT NULL default '', 
Lastname varchar(32) NOT NULL default '', 
email varchar(50) NOT NULL default '', 
statement varchar(255) NOT NULL default '', 
warning varchar(1) NOT NULL default '0', 
banned varchar(1) NOT NULL default '0', 
PRIMARY KEY (id) 
)TYPE=MyISAM;"; 

// Execute query 
mysql_query($sql,$con); 

mysql_close($con); 
?>

 

ANY help is GREATLY apprciated!

I would suggest that you really try to work hard on your problem-describing skills. I think that the reason we're on the third page of trying to help you is solely because you haven't been able to describe your problems, or your requested solution. Obviously nothing personal, just something you should think about.

As I mentioned, it's nothing personal, and I wasn't trying to be rude.... so please don't take it personally. I'm just saying that you'll get an answer much quicker that way. If you don't understand what we're saying... ask. We would rather teach you than just give you the answer.

 

We're volunteering to help you. As of the time of this post, we will have posted 20 replies in all. I'm shocked that you aren't more appreciative. Nobody is getting paid to help you. We could have just ignored your post (and that would have been mucheasier).

 

Being pissy with me because I gave you advice? Sure, as I assume you don't have the mindset of being thankful for help. Being pissy with them because you're asking higher-level questions than you understand? Unacceptable. They're volunteering their time, and you're being rude. I'd be more appreciative if I were you. Just saying.

Guest
This topic is now 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.